五、Spring整合MyBatis

一、目標(biāo)與思路

1.1、 目標(biāo)

adminMapper通過(guò)IOC容器裝配到當(dāng)前組件中后,就可以直接調(diào)用它的方法,享受到框架給我們提供的方便。

package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mapper.AdminMapper;
import com.atguigu.crowd.service.api.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Admin getAdminList(Integer adminId) {
        // adminMapper 通過(guò)IOC容器裝配到當(dāng)前組件中后,就可以直接調(diào)用它的方法,享受框架給我們提供的方便。
        return adminMapper.selectByPrimaryKey(adminId);
    }
}

1.2、思路

image.png

二、操作清單

在子工程中加入搭建環(huán)境所需要的具體依賴(lài)
準(zhǔn)備 jdbc.properties
創(chuàng)建 Spring 配置文件專(zhuān)門(mén)配置 Spring 和 MyBatis 整合相關(guān)
在 Spring 的配置文件中加載 jdbc.properties 屬性文件
配置數(shù)據(jù)源
測(cè)試從數(shù)據(jù)源中獲取數(shù)據(jù)庫(kù)連接
配置 SqlSessionFactoryBean
裝配數(shù)據(jù)源
指定 XxxMapper.xml 配置文件的位置
指定 MyBatis 全局配置文件的位置(可選)
配置 MapperScannerConfigurer
測(cè)試是否可以裝配 XxxMapper 接口并通過(guò)這個(gè)接口操作數(shù)據(jù)庫(kù)

2.1、在子工程中加入搭建環(huán)境所需的具體依賴(lài)

image.png

<!-- Spring 依賴(lài) -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
</dependency>
<!-- MySQL 驅(qū)動(dòng) -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 數(shù)據(jù)源 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
</dependency>
<!-- MyBatis 與 Spring 整合 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
</dependency>
<!-- MyBatis 分頁(yè)插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
</dependency>
<!-- Spring 進(jìn)行 JSON 數(shù)據(jù)轉(zhuǎn)換依賴(lài) -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
<!-- JSTL 標(biāo)簽庫(kù) -->
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

2.2、數(shù)據(jù)庫(kù)連接信息

image.png
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/project_crowd?useUnicode=true&characterEncoding=UTF-8
jdbc.driver=com.mysql.jdbc.Driver

2.3、mybatis-config.xml

image.png
<?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>
</configuration>

2.4、創(chuàng)建 spring-persist-mybatis.xml

創(chuàng)建 Spring 配置文件專(zhuān)門(mén)配置 Spring 和 MyBatis 整合相關(guān)


image.png
<?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: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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
</beans>

2.4.1、配置數(shù)據(jù)源

在 spring-persist-mybatis.xml 配置文件進(jìn)行配置

    <!-- 加載外部屬性文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置數(shù)據(jù)源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="driverClassName" value="${jdbc.driver}"/>
    </bean>

創(chuàng)建 Spring 的 Junit 測(cè)試類(lèi)


image.png
package com.atguigu.crowd.test;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mapper.AdminMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// 指定 Spring 給 Junit 提供的運(yùn)行器類(lèi)
@RunWith(SpringJUnit4ClassRunner.class)
// 加載 Spring 配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class CrowdSpringTest {
    @Autowired
    private DataSource dataSource;
    @Test
    public void testDataSource() throws SQLException {
        // 1.通過(guò)數(shù)據(jù)源對(duì)象獲取數(shù)據(jù)源連接
        Connection connection = dataSource.getConnection();
        // 2.打印數(shù)據(jù)庫(kù)連接
        System.out.println(connection);
    }
}
image.png

2.4.2、配置 SqlSessionFactoryBean

    <!-- 配置SqlSessionFactoryBean整合MyBatis -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定MyBatis全局配置文件位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>

        <!-- 指定Mapper.xml配置文件位置 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"/>

        <!-- 裝配數(shù)據(jù)源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

2.4.3、配置 MapperScannerConfigurer

    <!-- 配置MapperScannerConfigurer來(lái)掃描Mapper接口所在的包 -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.atguigu.crowd.mapper"/>
    </bean>

測(cè)試

package com.atguigu.crowd.test;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.mapper.AdminMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

// 指定 Spring 給 Junit 提供的運(yùn)行器類(lèi)
@RunWith(SpringJUnit4ClassRunner.class)
// 加載 Spring 配置文件的注解
@ContextConfiguration(locations = {"classpath:spring-persist-mybatis.xml"})
public class CrowdSpringTest {
    @Autowired
    private DataSource dataSource;
    @Autowired
    private AdminMapper adminMapper;
    @Test
    public void testAdminMapperAutowired() {
        Admin admin = new Admin(null,"zhangsan","123123","張三","zhangsan@163.com",null);
        int count = adminMapper.insert(admin);
        System.out.println("受影響的行數(shù):"+count);
    }
    @Test
    public void testDataSource() throws SQLException {
        // 1.通過(guò)數(shù)據(jù)源對(duì)象獲取數(shù)據(jù)源連接
        Connection connection = dataSource.getConnection();
        // 2.打印數(shù)據(jù)庫(kù)連接
        System.out.println(connection);
    }
}
image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容