dongxurr 2019-12-17
目的:
使用 Spring 容器用单例模式管理 MyBatis 的 sqlSessionFactory ; 使用 Spring 管理连接池、数据源等; 将 Dao / Mapper 动态代理对象注入到 Spring 容器中,使用时直接获取;
整合的三种方式:链接??
https://www.jianshu.com/p/412051d41d73
● 导入所需的Jar包
MyBatis+Spring整合包:mybatis-spring-1.3.2.jar MyBatis核心包:mybatis-3.4.6.jarSpring三方及依赖:下载数据库连接驱动:mysql-connector-java-5.1.46-bin.jar、ojdbc7.jarC3P0连接池:c3p0-0.9.5.2.jar、mchange-commons-java-0.2.11.jar
● 创建Mybatissql主配置文件 - MapConfig.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="com.sikiedu.bean" /> </typeAliases> <mappers> <package name="com.sikiedu.mapper" /> </mappers> </configuration>
● 创建Spring主配置文件 - applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!-- 读取properties文件 --> <context:property-placeholder location="db.properties" /> <!-- 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置MyBatis sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource" /> <!-- 引用mybatis配置文件中的配置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml" /> <!-- 配置SQL映射文件信息 --> </bean> <!-- UserDao --> <bean id="userDaoImpl" class="com.sikiedu.dao.UserDaoImpl"> <!-- 将工厂注入到dao的父类 SQLSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- Mapper动态代理开发 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 注入 SQLSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <!-- 配置接口 --> <property name="mapperInterface" value="com.sikiedu.mapper.UserMapper" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sikiedu.mapper" /> </bean> </beans>
applicationContext.xml
① 读取db.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/games_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true jdbc.user=root jdbc.password=123456
db.properties
<context:property-placeholder location="db.properties" />
② 配置C3P0连接池
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
③ 配置sqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用数据源组件 --> <property name="dataSource" ref="dataSource" /> <!-- 引用mybatis配置文件中的配置 --> <property name="configLocation" value="classpath:sqlMapConfig.xml" /> <!-- 配置SQL映射文件信息 --> </bean>
④ 测试
package com.sikiedu.bean; import java.util.Date; import java.util.List; public class User { private Integer id; private String username; private String userpassword; private Long balance; private Date grgisterdate; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getUserpassword() { return userpassword; } public void setUserpassword(String userpassword) { this.userpassword = userpassword == null ? null : userpassword.trim(); } public Long getBalance() { return balance; } public void setBalance(Long balance) { this.balance = balance; } public Date getGrgisterdate() { return grgisterdate; } public void setGrgisterdate(Date grgisterdate) { this.grgisterdate = grgisterdate; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", userpassword=" + userpassword + ", balance=" + balance + ", grgisterdate=" + grgisterdate + "]"; } }
User.java
package com.sikiedu.dao; import com.sikiedu.bean.User; public interface UserDao { public User selectByPrimaryKey(Integer id); }
UserDao.java
package com.sikiedu.dao; import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.support.SqlSessionDaoSupport; import com.sikiedu.bean.User; public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User selectByPrimaryKey(Integer id) { SqlSession session = getSqlSession(); return session.selectOne("com.sikiedu.mapper.UserMapper.selectByPrimaryKey", id); } }
UserDaoImpl.java
<bean id="userDaoImpl" class="com.sikiedu.dao.UserDaoImpl"> <!-- 将工厂注入到dao的父类 SQLSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 传递ID - ac.getBean("userDaoImpl"); UserDaoImpl userDao = ac.getBean(UserDaoImpl.class);// 传递接口 User user = userDao.selectByPrimaryKey(1); System.out.println(user);
♦ Mapper动态代理开发
- Mapper接口与Mapper.xml映射文件必须放在同一目录中,Mapper.xml的namespace必须为全路径包名。
- sqlMapConfig.xml配置 - 以包的形式扫描映射器的位置
<mappers> <package name="com.sikiedu.mapper" /> </mappers>
- spring主配置文件 - 将MapperFactoryBean交给Spring管理
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 注入 SQLSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> <!-- 配置接口 --> <property name="mapperInterface" value="com.sikiedu.mapper.UserMapper" /> </bean>
- 测试
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // 传递ID - ac.getBean("userMapper"); UserMapper userMapper = ac.getBean(UserMapper.class);// 传递接口 User user = userMapper.selectByPrimaryKey(2); System.out.println(user);
♦ Mapper动态扫描开发
- Mapper接口与Mapper.xml映射文件必须放在同一目录中,Mapper.xml的namespace必须为全路径包名。
- sqlMapConfig.xml配置 - 以包的形式扫描映射器的位置
<mappers> <package name="com.sikiedu.mapper" /> </mappers>
- applicationContext.xml配置 - 使用MapperScannerConfigurer进行扫描
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.sikiedu.mapper" /> </bean>
- 测试
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); UserMapper userMapper = ac.getBean(UserMapper.class); User user = userMapper.selectByPrimaryKey(3); System.out.println(user);