您现在的位置是:首页 >技术教程 >Spring:用 Spring 整合 MyBatis(Spring-MyBatis)代码整理网站首页技术教程

Spring:用 Spring 整合 MyBatis(Spring-MyBatis)代码整理

Sun 3285 2024-06-26 12:01:02
简介Spring:用 Spring 整合 MyBatis(Spring-MyBatis)代码整理

Spring:Day 05

Spring - MyBatis

用 Spring 整合 MyBatis 代码整理

1. 依赖:pom.xml

<!-- mysql 数据库驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>
</dependency>

<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.7</version>
</dependency>

<!-- spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.18</version>
</dependency>

<!-- aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.6</version>
</dependency>

<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.23</version>
</dependency>

<!-- mybatis-spring -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>2.0.6</version>
</dependency>

<!-- junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.24</version>
</dependency>

2. 外部配置文件:db.properties

jdbc.drive=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring01?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=1142553864qq

注意一定要是 jdbc.xxx

3. MyBatis 核心配置文件:mybatis-config.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- configuration 核心配置文件 -->
<configuration>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>

    <typeAliases>
        <typeAlias type="com.Sun3285.pojo.xxx" alias="xxx"/>
        <!--<package name="com.Sun3285.xxx"/>-->
    </typeAliases>

</configuration>

注意:在 MyBatis 核心配置文件中进行设置以及别名管理,其余设置在 Spring 配置文件中配置。

4. 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class 实体类 implements Serializable {
    private int xxx;
    private String xxx;
}

注意:实体类需要实现序列化

5. 接口:xxxMapper.java

public interface xxxMapper {
    
    // 方法
    返回值类型 方法名(@Param("xxx") int xxx);
}

6. 实现类:xxxMapper.xml

<?xml version="1.0" encoding="UTF8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.Sun3285.dao.xxxMapper">

    <cache/>

    <select id="方法名" parameterType="参数类型" resultType="返回值类型">
        sql 语句
    </select>

</mapper>

注意:实现类在 namespace 中需要绑定实现的接口

7. Spring 通用配置文件: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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 外部配置文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>

    <!-- 注册 dataSource -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.drive}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </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/Sun3285/dao/*.xml"/>
    </bean>

    <!-- 注册 sqlSessionTemplate -->
    <bean id="sqlSessionTemplate" 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">
        <tx:attributes>
            <!-- 给哪些方法配置事务、事务的传播特性(默认 REQUIRED) -->
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置事务切入 -->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.Sun3285.dao.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

注意:需要按照实际情况对文件路径进行修改

8. 实现类:xxxMapperImpl.java

public class xxxMapperImpl implements xxxMapper{

    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    // 方法
    public 返回值类型 方法名() {
        xxxMapper mapper = sqlSession.getMapper(xxxMapper.class);
        return mapper.方法名();
    }
}

注意:这个实现类相当于之前 MyBatis 中的测试代码,用来得到 mapper,最终还是由 mapper 来执行第 6 步中实现类中的方法。

9. Spring 配置文件: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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 导入 Spring 通用配置文件 -->
    <import resource="spring-dao.xml"/>

    <!-- 注册第 8 步的实现类 -->
    <bean id="对象名" class="com.Sun3285.dao.xxxMapperImpl">
        <property name="sqlSession" ref="sqlSessionTemplate"/>
    </bean>

</beans>

10. 测试

public class Test01 {
    @Test
    public void test01() {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 从容器中得到对象,执行方法
        context.getBean("对象名").方法();
    }
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。