Spring and mybatis are integrated

Refer to the document from Crazy God Notes
Steps:
1. Import the relevant jar package
. junit
. mybatis
. mysql database
. spring related
. aop woven into
. mybatis-spring [new]
2. Write configuration files

3. Test

Memories mybatis

1. Write entity class
2. Write core configuration file
3. Write interface
4. Write Mapper.xml
5. Test

Mybatis-spring

Official document

1. Write the data source

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!--DataSource : 使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    这里使用Spring提供的JDBC-->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;rewriteBatchedStatements=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    </beans>

2.sqlFactory

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!--DataSource : 使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    这里使用Spring提供的JDBC-->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;rewriteBatchedStatements=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/zhou/mapper/*.xml"/>
     </bean>
</beans>

3.sqlSessionTemplate

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!--DataSource : 使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    这里使用Spring提供的JDBC-->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;rewriteBatchedStatements=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/zhou/mapper/*.xml"/>
     </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession
        只能使用构造器注入属性 sqlSessionFactory,因为它们有set方法-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"  >
         <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

4. Need to add implementation class to the interface【】

<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
    <!--DataSource : 使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid
    这里使用Spring提供的JDBC-->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false&amp;rewriteBatchedStatements=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--绑定Mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="mapperLocations" value="classpath:com/zhou/mapper/*.xml"/>
     </bean>
    <!--SqlSessionTemplate:就是我们使用的sqlSession
        只能使用构造器注入属性 sqlSessionFactory,因为它们有set方法-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"  >
         <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    <!---->
   <bean id="userMapper" class="com.zhou.mapper.UserMapperImpl">
       <property name="sqlSessionTemplate" ref="sqlSession"/>
   </bean>
</beans>

5. Inject your own implementation class into Spring

package com.zhou.mapper;

import com.zhou.pojo.User;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class UserMapperImpl implements UserMapper{
    
    
//我们所有操作,都使用sqlSession来执行,在原来,现在都使用SqlSessionTemplate
    private SqlSessionTemplate sqlSessionTemplate;
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
    
    
        this.sqlSessionTemplate=sqlSessionTemplate;
    }
    @Override
    public List<User> selectUser() {
    
    
        return sqlSessionTemplate.getMapper(UserMapper.class).selectUser();
    }
}

5. Test and use. The
above is the direct operation of mybatis, and the following is the operation after the integration of mybatis and spring.

    @Test
    public void test1() throws IOException {
    
    
        String resource="mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = build.openSession(true);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
        for (User user : users) {
    
    
            System.out.println(user);
        }
    }
    @Test
    public void test2(){
    
    
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapperImpl userMapper = classPathXmlApplicationContext.getBean("userMapper", UserMapperImpl.class);
        List<User> users = userMapper.selectUser();
        for (User user : users) {
    
    
            System.out.println(user);
        }

    }

The implementation class of another writing interface goes to the extends SqlSessionDaoSupport class. The
SqlSessionDaoSupport class has the getSqlSession() method to obtain the SqlSession instance. Of course, when using the spring Ioc, you need to inject the attribute sqlSessionFactory
UserMapperImpl2.java

public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper{
    
    
    @Override
    public List<User> selectUser() {
    
    
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        return mapper.selectUser();
    }

}
<?xml version="1.0" encoding="UTF8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       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">
     <import resource="spring-dao.xml"/>
    <!---->
    <bean id="userMapper" class="com.zhou.mapper.UserMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
    <bean id="userMapper2" class="com.zhou.mapper.UserMapperImpl2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

MyTest.java

    @Test
    public void test3(){
    
    
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapperImpl2 userMapper2 = classPathXmlApplicationContext.getBean("userMapper2", UserMapperImpl2.class);
        List<User> users = userMapper2.selectUser();
        for (User user : users) {
    
    
            System.out.println(user);
        }
    }

Summary: After integrating into Spring, you can completely eliminate mybatis configuration files. In addition to these ways to achieve integration, we can also use annotations to achieve this. This will be tested when we learn SpringBoot later!

Guess you like

Origin blog.csdn.net/qq_44788518/article/details/112232755