Spring整合Mybatis详解
整合过程
1、导包
(1).Spring的包
(2).mybatis的包
(3).再导入一个结合的包
2、项目搭建
3、整合mybatis
1、原始整合
不管那种方式都依赖于SessionFactory。SqlSessionFactory需要依赖于读取mybatis核心配置文件中的信息。
分成三层:
1、 别名等属性设置
2、 数据库的连接信息
3、 读取映射文件
其中的第二层可以使用连接池来取代。实际上,需要依赖两个,一个是dataSource还有是核心配置文件。
2、mapper整合
(1).Spring配置bean —》MapperFactoryBean类
依赖于两个 SQLSessionFactory和接口的位置
直接进入测试
(2).配置一个扫描类,自动扫描某个位置下所有的mapper,并自动起名为所写类名对应的小写。
applicationContext.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--读取db.properties--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean name="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--读取sqlMapperConfig.xml--> <property name="configLocation" value="classpath:resource/mybatis/sqlMapConfig.xml"></property> </bean> <!--原始整合--> <!--配置UserDao--> <bean name="userDao" class="cn.hd.dao.impl.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlsessionFactory"></property> </bean> <!--mapper整合--> <!--<bean name="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlsessionFactory"></property> <property name="mapperInterface" value="cn.hd.mapper.UserMapper"></property> </bean>--> <!--自动扫描包下面的所有Mapper,当出现多个Mapper时,不用多次书写bean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.hd.mapper"></property> </bean> </beans>
UserDao.xml
package cn.hd.dao; import cn.hd.entity.User; public interface UserDao { /*没有写事务只能写查询,增删改要提交事务才有用*/ User findUserById(Integer id); }
UserDaoImpl.xml
package cn.hd.dao.impl; import cn.hd.dao.UserDao; import cn.hd.entity.User; import org.mybatis.spring.support.SqlSessionDaoSupport; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User findUserById(Integer id) { User user = getSqlSession().selectOne("test.findUserById", id); return user; } }
SqlMapperConfig.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> <package name="cn.hd.entity"></package> </typeAliases> <!--读取UserMapper.xml--> <mappers> <mapper resource="cn/hd/mapper/UserMapper.xml"></mapper> </mappers> </configuration>
User.java
package cn.hd.entity; public class User { private Integer id; private String name; private String sex; private String address; private Integer balance; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getBalance() { return balance; } public void setBalance(Integer balance) { this.balance = balance; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", address='" + address + '\'' + ", balance=" + balance + '}'; } }
UserMapper.JAVA接口
package cn.hd.mapper; import cn.hd.entity.User; public interface UserMapper { 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="cn.hd.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM t_user WHERE id = #{id} </select> </mapper>
测试类Demo
<?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="cn.hd.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="user"> SELECT * FROM t_user WHERE id = #{id} </select> </mapper>