1, Gossip
The weekend is coming again. Today, finish the spring blog, and then start the review of mybatis. I read it in July last year. If I don't write a blog, I'll soon forget it. Ha ha
2, Review mybatis and introduce integration methods
1. mybatis review
mybatis has written a simple example before. You can take a look at the previous blog
Introduction to Mybatis and construction of the first simple demo
2. Integration method 1: use SqlSessionTemplate in mybatis spring
Can refer to Official document of mybatis spring
First, we import the related dependencies into the pom file
<dependencies> <!--MySQL drive--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!--Mybatis--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies>
In the original mybatis configuration file, we only put alias configuration. Of course, this configuration file can be completely omitted. All relevant configurations can be put into the spring configuration file
<?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.decade.entity"/> </typeAliases> </configuration>
Next, let's configure the relevant configuration files of spring management data source
Create a spring Dao XML to manage database connection information
<?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"> <!-- Configure data source, using spring Data source replacement for mybatis, c3p0 druid dbcp --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <!--Drive configuration, com.mysql.jdbc.driver --> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/decade_test?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- establish sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- binding mybatis Configuration file for --> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/decade/mapper/*.xml"/> </bean> <!--establish sqlSessionTemplate,Replace the original sqlSession --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <!--because sqlSessionTemplate No, set Method, so we use constructor injection --> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans>
Note: if you want to configure the SqlSessionTemplate instance through annotation, you can refer to this section
@Configuration public class MyBatisConfig { @Bean public SqlSessionTemplate sqlSession() throws Exception { return new SqlSessionTemplate(sqlSessionFactory()); } }
Then there is the general configuration file ApplicationContext XML, we just need to import the above configuration file
<?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-dao.xml"/> <bean id="userDao" class="com.decade.mapper.UserDaoImpl"> <property name="sqlSessionTemplate" ref="sqlSessionTemplate"/> </bean> </beans>
Different from mybatis, we need to add an implementation class of UserDao to obtain sqlSessionTemplate as a business class
package com.decade.mapper; import com.decade.entity.User; import org.mybatis.spring.SqlSessionTemplate; import java.util.List; public class UserDaoImpl implements UserDao{ // The sqlSessionTemplate here replaces the original sqlSession private SqlSessionTemplate sqlSessionTemplate; public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) { this.sqlSessionTemplate = sqlSessionTemplate; } @Override public List<User> getUserInfo() { UserDao mapper = sqlSessionTemplate.getMapper(UserDao.class); return mapper.getUserInfo(); } }
Finally, we create a new test class. We only need to get the bean of UserDao's implementation class from the spring context
import com.decade.mapper.UserDao; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserDaoTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = context.getBean("userDao", UserDao.class); userDao.getUserInfo().forEach(System.out::println); } }
The operation results are as follows
3. Integration method 2: use SqlSessionDaoSupport in mybatis spring
Mode 2 is simpler than mode 1
It does not need to be injected through the set method, but only needs to inherit an abstract SqlSessionDaoSupport class
package com.decade.mapper; import com.decade.entity.User; import org.mybatis.spring.support.SqlSessionDaoSupport; import java.util.List; public class UserDaoImpl2 extends SqlSessionDaoSupport implements UserDao { @Override public List<User> getUserInfo() { return getSqlSession().getMapper(UserDao.class).getUserInfo(); } }
Use spring Dao in this way The sqlsession in the XML configuration file does not need to be configured
applicationContext.xml is configured as follows
<?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-dao.xml"/> <bean id="userDao2" class="com.decade.mapper.UserDaoImpl2"> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> </beans>
Write test class
import com.decade.mapper.UserDao; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserDaoTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = context.getBean("userDao2", UserDao.class); userDao.getUserInfo().forEach(System.out::println); } }
Run the test class and the result is the same as that in mode 1
If there is any mistake, please correct it