Mybatis-spring整合
程序员文章站
2022-07-15 10:29:51
...
1,导入spring,mybatis整合的jar包
mybatis-spring-1.2.2.jar
spring相关的jar包
2,由spring管理mybatis的sqlSessionFactory
applicationContext.xml
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="SqlMapConfig.xml"></property>
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
配置数据源DataSource
<!-- 数据源,与数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 数据库连接地址 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/>
<!-- 用户名 -->
<property name="user" value="root"/>
<!-- 密码 -->
<property name="password" value="123456"/>
<!-- 最小连接数 -->
<property name="minPoolSize" value="3"/>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="10"/>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="5"/>
</bean>
SqlMapConfig.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>
<settings>
<!-- 打开延迟加载开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 将积极加载改为消极加载即按需加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<!-- 加载映射文件 -->
<mappers>
<mapper resource="sqlmap/OrdersMapperCustom.xml"/>
<mapper resource="sqlmap/userMapper.xml"/>
</mappers>
</configuration>
3,用Dao来查询
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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
-->
<mapper namespace="mybatis.com.mapper.userMapper">
<!--
在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="mybatis.com.entity.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<!--
根据id查询得到一个user对象
-->
<select id="getUser" parameterType="int" resultType="mybatis.com.entity.User">
select * from user where id=#{id}
</select>
</mapper>
在applicationContext.xml中增加dao的bean
<!-- 原始dao接口 -->
<bean id="userDao" class="mybatis.com.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
UserDao.java
public class UserDao extends SqlSessionDaoSupport {
//果不继承SqlSessionDaoSupport spring需要通过构造方法注入
/*private SqlSessionFactory sqlSessionFactory ;
public UserDao(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}*/
public User findUserById(int id){
/*SqlSession sqlSession = sqlSessionFactory.openSession();*/
//继承SqlSessionDaoSupport,通过this.getSqlSession来获取sqlSession
SqlSession sqlSession = this.getSqlSession();
User user = sqlSession.selectOne("mybatis.com.mapper.userMapper.getUser",id);
System.out.println(user.toString());
return user;
}
}
测试
@Test
public void test(){
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
User user = userDao.findUserById(1);
System.out.println(user.toString());
}
4,用map接口查询
在applicationContext.xml中增加Mapper的bean
<bean id="userMap" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mybatis.com.mapper.userMapper"></property>
<property name="sqlSessionFactory" value="sqlSessionFactory"></property>
</bean>
userMapper.java
public interface userMapper {
/*
* 函数名与userMapper.xml中的id名相同
* 返回类型与参数类型与userMapper.xml中的resultType,parameterType相同
*/
public User getUser(int id) throws Exception;
}
测试
@Test
public void test02() throws Exception{
applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
userMapper userMapper = (mybatis.com.mapper.userMapper) applicationContext.getBean("userMap");
User user = userMapper.getUser(1);
System.out.println(user.toString());
}
mapper批量扫描
<!-- 批量扫描,从mapper包中扫描mapper接口,自动创建代理对象并且在spring容器中注册
规范:将mapper.java和mapper.xml映射文件名称一致,且在一个目录中
自动扫描出来的mapper的bean的id为mapper类(首字母小写)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定扫描包名 -->
<property name="basePackage" value="mybatis.com.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 数据源,与数据库连接 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库驱动 -->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!-- 数据库连接地址 -->
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8"/>
<!-- 用户名 -->
<property name="user" value="root"/>
<!-- 密码 -->
<property name="password" value="123456"/>
<!-- 最小连接数 -->
<property name="minPoolSize" value="3"/>
<!-- 最大连接数 -->
<property name="maxPoolSize" value="10"/>
<!-- 初始化连接数 -->
<property name="initialPoolSize" value="5"/>
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="SqlMapConfig.xml"></property>
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 原始dao接口 -->
<bean id="userDao" class="mybatis.com.dao.UserDao">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- Mapper代理 -->
<bean id="userMap" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="mybatis.com.mapper.userMapper"></property>
<property name="sqlSessionFactory" value="sqlSessionFactory"></property>
</bean>
</beans>