整合mybatis
- mybatis实现对数据库操作的高效管理,而spring用一种新的方式实现对对象的创建和使用。
整合mybatis就是将mybatis的数据操纵和spring的控制反转结合起来。
-
导入相关jar包
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ssm-spring</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>spring_08</artifactId> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.10.RELEASE</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
改造mybatis
- 原本我们的mybatis是有User实体类和UserMapper接口和UserMapper.xml,还有mybatis-config.xml。
主要就是改造mybatis-config文件,我们需要把一些mybatis连接数据库的操作转移到spring中一起操作。
新建一个spring-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/lyj/mapper/*.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <bean id="userMapperImpl" class="com.lyj.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
在这个文件中,我们将Sqlsession的获取,对数据库的连接,对mybatis的一些配置整合在了一起。
-
操作sqlsession
-
在测试类中用getbean()方式来获取session
@Test public void test_03() { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); SqlSession sqlSession = (SqlSession) context.getBean("sqlSession"); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> allUsers = mapper.findAllUsers(); for (User u: allUsers) { System.out.println(u); } }
-
通过一个UserMapperImpl实现类将session注入。(规范)
public class UserMapperImpl implements UserMapper{ private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> findAllUsers() { UserMapper userMapper = sqlSession.getMapper(UserMapper.class); return userMapper.findAllUsers(); } }
官方文档的规范是使用第二种方式来进行实现的,同时这也更符合spring依赖注入的思想。
-
优化
-
通过导入的方式将配置部分和普通的bean注入分离
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="spring-config.xml"/> <bean id="userMapperImpl" class="com.lyj.mapper.UserMapperImpl"> <property name="sqlSession" ref="sqlSession"/> </bean> </beans>
-
将sqlsession获取进一步简化。
-
mybatis-spring1.2.3版以上才可以使用 .
-
继承SqlSessionDaoSupport, 直接利用 getSqlSession() 获得Sqlsession , 在bean管理就直接为UserMapperIml注入SqlSessionFactory(原来是注入sqlSession) 。
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{ @Override public List<User> findAllUsers() { return getSqlSession().getMapper(UserMapper.class).findAllUsers(); } }
将sqlSessionFactory注入即可。
<bean id="userMapperImpl2" class="com.lyj.mapper.UserMapperImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean>
-
-
将UserMapperImpl分离成service层
有一个UserMapper.xml后再实现一个UserMapperImpl的话逻辑会有些乱,可以将UserMapperImpl分离出来,单独建一个service层。将session的获取在这一个层次进行。
-
注入session类型
public class UserServiceImpl implements UserService{ private SqlSessionTemplate sqlSession; public void setSqlSession(SqlSessionTemplate sqlSession) { this.sqlSession = sqlSession; } @Override public List<User> findAllUsers() { return sqlSession.getMapper(UserMapper.class).findAllUsers(); } }
-
继承SqlSessionDaoSupport类
public class UserServiceImpl2 extends SqlSessionDaoSupport implements UserService{ @Override public List<User> findAllUsers() { return getSqlSession().getMapper(UserMapper.class).findAllUsers(); } }
-
-
获取sesission的方式进一步简化:
当我们的获取Session的操作更多的时候,我们希望以一种更加高效和更加便于管理的方式去获取session。
spring框架提供了一个自动扫描MapperScannerConfigurer的操作,它可以通过设置这个类的属性和自动扫描指定包来自动的将我们的session注入到包名下的所有mapper中。
我们可以直接获取mapper去操作,而将session的获取交给spring封装。
使用方法:
-
配置文件:
它需要手动的设置两个属性来为指定包名注入session.
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <property name="basePackage" value="com.lyj.mapper"/> </bean>
-
调用
private UserMapper userMapper;
直接调用即可使用。
-