欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring与Mybatis整合的配置笔记

程序员文章站 2022-06-24 18:50:25
maven配置在这里插入代码片Mybatis-Spring、Mybatis、Spring的对应关系...

目录结构

Spring与Mybatis整合的配置笔记

Mybatis-Spring、Mybatis、Spring的对应关系

Spring与Mybatis整合的配置笔记

maven配置

<dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
        <!--Spring操作数据库需要的包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!--整合Mybatis和Spring的包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

spring-dao.xml

Mybatis的SqlSessionFactory 、SqlSession对象、XXXMapper对象交给Spring管理

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!--DataSource:使用Spring的数据源替换Mybatis的配置-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.72.142:3306/ssm?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--1. SqlSessionFactory   相当于:SqlSessionFactory factory = builder.build(inputStream);-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <!--关联Mybatis-->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="mapperLocations" value="classpath:com/jarvis/dao/*.xml"/>
    </bean>
    <!--2. SqlSessionTemplate就是之前我们使用的SqlSession  相当于:SqlSession session = factory.openSession();-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!--无set方法,只能使用构造器注入-->
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
    
    <!--配置声明式事务  **把事务看成一个切面类-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>

    <!--结合AOP实现事务的织入 -->
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--1. 给哪些方法配置事务  2. 配置事务的传播特性,默认为REQUIRED-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的织入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(public * com.jarvis.dao.StudentMapperImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
</beans>

sqlMapConfig.xml

Mybatis中一些设置

<?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>
    <settings>
        <!--是否开启驼峰命名自动映射-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!--懒加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
        <!--显示开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>

applicationContext.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.jarvis"/>
    <context:annotation-config/>
    <!--开启基于注解的AOP功能:aop名称空间-->
    <aop:aspectj-autoproxy/>

    <import resource="spring-dao.xml"/>
    
    <!--相当于:StudentMapper studentMapper = session.getMapper(StudentMapper.class);-->
    <bean id="studentMapper" class="com.jarvis.dao.StudentMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"/>
    </bean>
</beans>

dao

StudentMapper

package com.jarvis.dao;
import com.jarvis.pojo.Student;
import java.util.List;

public interface StudentMapper {
    List<Student> findAll();
}

StudentMapperImpl

该实现类也由Spring管理了

package com.jarvis.dao;
import com.jarvis.pojo.Student;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;

public class StudentMapperImpl implements StudentMapper {
    private SqlSessionTemplate sqlSession; //原先我们的操作都用SqlSession来完成,现在使用SqlSessionTemplate
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSession = sqlSessionTemplate;
    }
    public List<Student> findAll(){
        StudentMapper mapper = this.sqlSession.getMapper(StudentMapper.class);
        return mapper.findAll();
    }
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jarvis.dao.StudentMapper">
    <!--开启二级缓存-->
    <cache/>

    <select id="findAll" resultType="com.jarvis.pojo.Student">
        select * from student
    </select>
</mapper>

测试代码

public class test {
    public static void main(String[] args) throws IOException {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentMapper mapper = context.getBean("studentMapper", StudentMapper.class);
        List<Student> all = mapper.findAll();
        System.out.println(all);
    }
}

方式2—简化版

StudentMapperImpl

实现类继承SqlSessionDaoSupport ,通过getSqlSession()方法得到SqlSession对象,所以无需在spring-dao.xml中配置SqlSessionTemplate了。

package com.jarvis.dao;
import com.jarvis.pojo.Student;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import java.util.List;

public class StudentMapperImpl extends SqlSessionDaoSupport implements StudentMapper {
    public List<Student> findAll(){
        return getSqlSession().getMapper(StudentMapper.class).findAll();
    }
}

spring-dao.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--DataSource:使用Spring的数据源替换Mybatis的配置-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://192.168.72.142:3306/ssm?useUnicode=true&amp;characterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--SqlSessionFactory   相当于:SqlSessionFactory factory = builder.build(inputStream);-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <!--关联Mybatis-->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        <property name="mapperLocations" value="classpath:com/jarvis/dao/*.xml"/>
    </bean>
    
    <!--配置声明式事务  **把事务看成一个切面类-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
    </bean>

    <!--结合AOP实现事务的织入 -->
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--1. 给哪些方法配置事务  2. 配置事务的传播特性,默认为REQUIRED-->
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置事务的织入-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(public * com.jarvis.dao.StudentMapperImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
    </aop:config>
    
</beans>

applicationContext.xml

spring-dao中不配做SqlSession,但是需要将实现类和SqlSessionFactory关联起来

<?xml version="1.0" encoding="UTF-8"?>
<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:p="http://www.springframework.org/schema/p"
        xmlns:c="http://www.springframework.org/schema/c"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <context:component-scan base-package="com.jarvis"/>
    <context:annotation-config/>
    <!--开启基于注解的AOP功能:aop名称空间-->
    <aop:aspectj-autoproxy/>

    <import resource="spring-dao.xml"/>

    <bean id="studentMapper" class="com.jarvis.dao.StudentMapperImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean>
</beans>

本文地址:https://blog.csdn.net/weixin_43283513/article/details/109625962