Spring Framework (5) — Integration of Spring and Mybatis
Environment set up
Importing dependencies
1. junit
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>// Add group 1025684353 together blow water chat
</dependency>
Copy the code
2. Mybatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.52.</version>
</dependency>
Copy the code
3. mysql-connector-java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.147.</version>// Add group 1025684353 together blow water chat
</dependency>
Copy the code
4. spring
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.36.</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.36.</version>
</dependency>
Copy the code
5. aspectJ AOP
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.94.</version>
</dependency>
Copy the code
6. Mybatis – Spring integration
< the dependency > < groupId > org. Mybatis < / groupId > < artifactId > mybatis - spring < / artifactId > < version > 2.0.6 < / version > </dependency>Copy the code
7. Static resource filtering fails
<build>
<resources>// Add group 1025684353 together blow water chat
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties
**/
*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties
**/
*.xml</include>
</includes>// Add group 1025684353 together blow water chat
<filtering>true</filtering>
</resource>
</resources>
</build>
Copy the code
Writing entity classes (requires importing Lombok dependencies)
package com.study.pojo;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String pwd;
}
Copy the code
Write the UserMapper interface
package com.study.mapper;
import com.study.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> selectUser(a);
}
Copy the code
Write a UserMapper configuration file
<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE mapper PUBLIC"- / / mybatis.org//DTD Config / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.study.mapper.UserMapper">
<select id="selectUser" resultType="user">
select * from mybatis.user
</select>
</mapper>// Add group 1025684353 together blow water chat
Copy the code
Write myBtis configuration files
<? 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.study.pojo"/>
</typeAliases>
</configuration>
Copy the code
Integration mode 1
To use Mybatis with Spring, you need to define at least two things in the Spring application context: an SqlSessionFactory and at least a data mapping class.
Note: SqlSessionFactory requires a DataSource. This can be used on any DataSource and needs to be configured just like any other Spring database connection.
Create a spring-dao. XML configuration file in the resource directory
<? 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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <! - the DataSource: mybatis before - configuration in config, after integrated directly in the bean configuration - > < 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=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/> </bean> <! --sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <! --name: name of the dataSource ref= the applied dataSource, such as dataSource reference to the dataSource configured above --> <property name="dataSource" ref="dataSource"/ > <! -- bind myBatis profile (if there are multiple MyBatis profiles) --> <property name="configLocation" value="classpath:mybatis-config.xml"/ > <! -- Register the mapper: userMapper.xml --> <property name="mapperLocations" value="classpath:com/study/mapper/*.xml"/ > <! </bean> </bean> </bean> <! --SqlSessionTemplate: sqlSession--> <bean ID ="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <! > <constructor-arg index=. <constructor-arg index=. <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
Copy the code
Second,Add an implementation class for the Dao interface and privatize the sqlSessionTemplate
package com.study.mapper;
import com.study.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class UserMapperImpl implements UserMapper{
// Use SqlSessionTemplate for all our operations;
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}// Add group 1025684353 together blow water chat
@Override
public List<User> selectUser(a) {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
returnmapper.selectUser(); }}Copy the code
Register the bean implementation in the Spring configuration file (not recommended)
<bean id="userMapper" class="com.study.mapper.UserMapperImpl"> <! --name: name of sqlSession for this bean, ref: reference sqlSession configured --> <property name="sqlSession" ref="sqlSession"/>
</bean>
Copy the code
Create a new spring configuration class applicationContext.xml and inject the implementation class into Spring (recommended).
<? 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 http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>// Add group 1025684353 together blow water chat<! -- ID: bean nameclass: Implementation class to register --> <bean id="userMapper" class="com.study.mapper.UserMapperImpl"> <! --name: name of sqlSession for this bean, ref: reference sqlSession configured --> <property name="sqlSession" ref="sqlSession"/>
</bean>
</beans>
Copy the code
4. Write test classes
import com.study.mapper.UserMapper;
import com.study.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void test(a) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
/ / the name of the bean
UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);// Add group 1025684353 together blow water chat}}}Copy the code
5. Successful operation:
Integration mode 2: Inherit SqlSessionDataSupport
SqlSessionDataSupport is an abstract support class that provides you with SQLSessions. Call the getSqlSession() method and you get an SqlSessionTemplate that you can then use to execute SQL methods.
First, write the interface implementation class
package com.study.mapper;
import com.study.pojo.User;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
@Override
public List<User> selectUser(a) {// Add group 1025684353 together blow water chat
returngetSqlSession().getMapper(UserMapper.class).selectUser(); }}Copy the code
Register the implementation class in Spring
<bean id="userMapper2" class="com.study.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
Copy the code
Modify the above test class
public class MyTest {
@Test
public void test(a) throws IOException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// Change userMapper to userMapper2
UserMapper userMapper = context.getBean("userMapper2", UserMapper.class);
for (User user : userMapper.selectUser()) {
System.out.println(user);// Add group 1025684353 together blow water chat}}}Copy the code
4. The test runs successfully
Finally, I wish you all an early success in your studies, a satisfactory offer, rapid promotion and salary increase, and the peak of your life. If you can, please give me a triple support yo, we will see you next time
To collect data