欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis如何使用(三)

程序员文章站 2024-03-13 10:13:57
在前边阐述了单独使用mybatis的方法,在实际开发过程中mybatis经常和spring一起使用,即mybatis和spring进行集成,现在我们来看如何集成。 m...

在前边阐述了单独使用mybatis的方法,在实际开发过程中mybatis经常和spring一起使用,即mybatis和spring进行集成,现在我们来看如何集成。

mybatis和spring进行集成需要用到集成包:mybatis-spring-1.1.1.jar,此包提供mybatis和spring集成的支持,把此包导入到项目的lib目录下。

我们先看mybatis单独使用的时候的过程,mybatis配置文件==》读取配置文件==》操作数据库,具体的使用方法可参照前两篇文章。

下面进行mybatis和spring的集成,

一、mybatis配置文件

在和spring做集成时mybatis的配置文件中有些配置不再需要了,spring会使用它自己的。如数据源,下面看下mybatis的配置文件,mybatisconfiguration.xml,

<?xml version="1.0" encoding="utf-8" ?>
<!doctype configuration public "-//mybatis.org//dtd config 3.0//en"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typealiases> 
<typealias alias="message" type="com.cn.imooc.entity.message"/> 
</typealiases> 
<mappers>
<mapper resource="com/cn/mappers/message.xml"/>
</mappers>
</configuration> 

上面的配置文件配置了别名和mappers映射文件,和之前的配置文件相比,可以看出没有了关于数据源的信息,这里在mybatis的配置文件中不再需要配置数据源,需要在spring的配置文件中配置。

二、spring配置文件

既然和spring做集成,那么必须导入spring的包,关于spring的包可以从前面的文章中获得;导入spring的包之后,就需要配置spring的配置文件,我们把spring的配置文件放在src下,名字为spring-application.xml,

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="address" class="com.cn.test.spring.address"></bean>
<!-- 引入jdbc配置文件 --> 
<!--
<context:property-placeholder location="jdbc.properties"/> 
-->
<!--1、创建jdbc数据源 --> 
<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource" > 
<property name="driverclassname" value="com.mysql.jdbc.driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/weixin?useunicode=true&characterencoding=utf-8" />
<property name="username" value="root"/>
<property name="password" value="123456"/> 
</bean> 
<!--2、sqlsessionfactorybean--> <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
<property name="datasource" ref="datasource"></property>
<property name="configlocation" value="classpath:mybatisconfiguration.xml"></property>
<!--
<property name="mapperlocations" value="classpath:com/cn/mappers/message.xml"></property>
-->
</bean>
<bean id="messagemapper" class="org.mybatis.spring.mapper.mapperfactorybean">
<property name="mapperinterface" value="com.cn.inter.imessageoperation" />
<property name="sqlsessionfactory" ref="sqlsessionfactory" />
</bean>
</beans> 

首先,我们配置了一个数据源,这里如果引入了context的命名空间,可以使用<context:property-placeholder location="jdbc.properties"/>,引入src下的配置文件。

其次,配置了sqlsessionfactorybean,这里使用sqlsessionfactorybean生成sqlsessionfactory(在mybatis中sqlsessionfactory由sqlsessionfactorybuilder生成)。要通过sqlsessionfactroybean生成sqlsessionfactroy有以下几个属性,datasource 即刚才配置的数据源,指定生成sqlsessionfactory使用的数据源

configlocation 这个属性指定mybatis的配置文件的路径,在本例中我们使用了src下的mybatisconfiguration.xml,如果在此文件中配置了mappers映射文件,则不需要第三个属性,如果没配置映射文件则需要第三个属性;假如,在mybatisconfiguration.xml文件中没有配置映射文件,也没有配置mapperlocations属性,则映射文件必须必须和映射器类在同一个包下,且映射文件和映射器类必须名字相同。

mapperlocations 指定mappers映射文件,这个属性可以配置为一个list的值

最后,使用动态代理生成访问数据库的代码,mapperfactorybean作为一个工厂类,可以用来生成访问数据库的动态代理,有两种方式可以生成一个动态代理,这里使用了mapperinterface和sqlsessionfactory两个属性,第一个指定映射器类的全限路径,第二个就是上面的sqlsessionfactory;另一种方式是使用注解的方式。

至此,spring的配置文件完成,可以进行测试,测试代码如下,

package com.cn.test.spring;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.mybatis.spring.support.sqlsessiondaosupport;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.cn.imooc.entity.message;
import com.cn.inter.imessageoperation;
public class testspringandmybatis {
public static void main(string[] args) {
// todo auto-generated method stub
//获得spring的配置
classpathxmlapplicationcontext cpxac=new classpathxmlapplicationcontext("spring-application.xml");
//获得imessageoperation接口类
imessageoperation imo=(imessageoperation)cpxac.getbean("messagemapper");
message m=imo.selectmessagebyid("2");
system.out.println(m);
}
} 

上边完成了mybatis和spring集成的一种方式,我们会发现在生成代理的时候如果有多个映射器类,则需要配置多次,比较麻烦,下篇文章使用另一种方式。

以上所述是小编给大家介绍的mybatis如何使用(三),希望对大家有所帮助