spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)相关的知识,希望对你有一定的参考价值。
一.原始dao的开发的整合方式
1.引入jar包
spring的jar包
mybatis的jar包
由mybatis提供的mybatis和spring的整合jar包(mybatis-spring-xxx.jar)
mysql的驱动jar包(mysql-connector-java-5.1.22-bin.jar)
c3p0数据源的jar(c3p0-0.9.5.1.jar)
2.引入配置文件和dao的实现代码
结构如下:
-
UserDao接口的代码
package com.si.demo.dao; import com.si.demo.entity.User; public interface UserDao { public User findUserById(Integer id); }
-
UserDaoImpl实现类的代码
-
package com.si.demo.dao; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.si.demo.entity.User; public class UserDaoImpl implements UserDao { private SqlSessionFactory sqlSessionFactory; public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public User findUserById(Integer id) { SqlSession sqlSession = sqlSessionFactory.openSession(); User user = sqlSession.selectOne("test.findUserById", id); return user; } }
-
mybatis-config.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> <mappers> <mapper resource="user.xml"/> </mappers> </configuration>
-
spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- ========================================配置数据源========================================= --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///mybatis2" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> <!-- ========================================分隔线========================================= --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="userDao" class="com.si.demo.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
-
user.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="test"> <select id="findUserById" parameterType="int" resultType="com.si.demo.entity.User"> select * from t_user where id = #{id} </select> </mapper>
3.Junit测试
@Test public void findUserByIdTest(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml"); UserDao userDao = (UserDao) ctx.getBean("userDao"); User user = userDao.findUserById(1); System.out.println(user); }
二.Mapper代码开发的整合方式
1.引入jar包
spring的jar包
mybatis的jar包
由mybatis提供的mybatis和spring的整合jar包(mybatis-spring-xxx.jar)
mysql的驱动jar包(mysql-connector-java-5.1.22-bin.jar)
c3p0数据源的jar(c3p0-0.9.5.1.jar)
※和上面的方式jar一样
2.引入配置文件和dao的实现代码
结构如下:
-
UserMapper接口的代码
package com.si.demo.mapper; import com.si.demo.entity.User; public interface UserMapper { public User findUserById(Integer id); }
-
UserMapper.xml配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.si.demo.mapper.UserMapper"> <!-- mapper代理开发方式,namespace的值必须是对应接口的全类名 --> <select id="findUserById" parameterType="int" resultType="com.si.demo.entity.User"> select * from t_user where id = #{id} </select> </mapper>
-
spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- ========================================配置数据源========================================= --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql:///mybatis2" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> <!-- ========================================分隔线========================================= --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.si.demo.mapper.UserMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>
-
mybatis-config.xml
注意:当UserMapper.xml和对应的UserMapper接口在同一个目录下面的时候,此时这个配置文件可以省略
如果不在同一目录,这里需要在mapper里面配上其目录
<?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> <mappers> <mapper resource="UserMapper.xml的目录"/> </mappers> </configuration>
3.Junit测试
@Test public void findUserByIdTest2(){ ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml"); UserMapper userDao = (UserMapper) ctx.getBean("userMapper"); User user = userDao.findUserById(1); System.out.println(user); }
注意:上面的mapper代理开发的整合方式,存在一些问题
实际的开发过程中,不可能只有一个userMapper,可能还有orderMapper,等等,不可能将所有的xxMapper都配置一遍,那样也太麻烦了,
mybatis的整合包给我们提供了一个扫描类MapperScannerConfigurer,用它就可以了
值需要在上面的spring-mybatis.xml中做一丢丢改变就可以了,如下
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.si.demo.mapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <!-- 将下面的mapper的配置,改成上面的扫描方式,通过来扫描mapper接口所在的报包名,当有多个包的时候,用半角逗号分隔即可
然后再次强调下,这种方式的配置,UserMapper.xml必须和对应的接口放在同一个目录下面
--> <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.si.demo.mapper.UserMapper"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> -->
以上是关于spring和mybatis的整合(包含原始dao的整合方式和mapper代理整合方式)的主要内容,如果未能解决你的问题,请参考以下文章