Java開發學習(十四)—-Spring整合Mybatis及Junit

一、Spring整合Mybatis思路分析

1.1 環境準備

步驟1:準備資料庫表

Mybatis是來操作資料庫表,所以先創建一個資料庫及表

create database spring_db character set utf8;
use spring_db;
create table tbl_account(
    id int primary key auto_increment,
    name varchar(35),
    money double
);

插入測試數據如下

步驟2:創建項目導入jar包

項目的pom.xml添加相關依賴

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.10.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.16</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.6</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
</dependencies>
步驟3:根據表創建模型類
public class Account implements Serializable {
​
    private Integer id;
    private String name;
    private Double money;
    //setter...getter...toString...方法略    
}
步驟4:創建Dao介面
public interface AccountDao {
​
    @Insert("insert into tbl_account(name,money)values(#{name},#{money})")
    void save(Account account);
​
    @Delete("delete from tbl_account where id = #{id} ")
    void delete(Integer id);
​
    @Update("update tbl_account set name = #{name} , money = #{money} where id = #{id} ")
    void update(Account account);
​
    @Select("select * from tbl_account")
    List<Account> findAll();
​
    @Select("select * from tbl_account where id = #{id} ")
    Account findById(Integer id);
}
步驟5:創建Service介面和實現類
public interface AccountService {
​
    void save(Account account);
​
    void delete(Integer id);
​
    void update(Account account);
​
    List<Account> findAll();
​
    Account findById(Integer id);
​
}
​
@Service
public class AccountServiceImpl implements AccountService {
​
    @Autowired
    private AccountDao accountDao;
​
    public void save(Account account) {
        accountDao.save(account);
    }
​
    public void update(Account account){
        accountDao.update(account);
    }
​
    public void delete(Integer id) {
        accountDao.delete(id);
    }
​
    public Account findById(Integer id) {
        return accountDao.findById(id);
    }
​
    public List<Account> findAll() {
        return accountDao.findAll();
    }
}
步驟6:添加jdbc.properties文件

resources目錄下添加,用於配置資料庫連接四要素

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_db?useSSL=false
jdbc.username=root
jdbc.password=123456

useSSL:關閉MySQL的SSL連接

步驟7:添加Mybatis核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "//mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--讀取外部properties配置文件-->
    <properties resource="jdbc.properties"></properties>
    <!--別名掃描的包路徑-->
    <typeAliases>
        <package name="com.itheima.domain"/>
    </typeAliases>
    <!--數據源-->
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"></property>
                <property name="url" value="${jdbc.url}"></property>
                <property name="username" value="${jdbc.username}"></property>
                <property name="password" value="${jdbc.password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!--映射文件掃描包路徑-->
    <mappers>
        <package name="com.itheima.dao"></package>
    </mappers>
</configuration>
步驟8:編寫應用程式
public class App {
    public static void main(String[] args) throws IOException {
        // 1. 創建SqlSessionFactoryBuilder對象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 2. 載入SqlMapConfig.xml配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 3. 創建SqlSessionFactory對象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        // 4. 獲取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5. 執行SqlSession對象執行查詢,獲取結果User
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
​
        Account ac = accountDao.findById(1);
        System.out.println(ac);
​
        // 6. 釋放資源
        sqlSession.close();
    }
}
步驟9:運行程式

1.2 整合思路分析

Mybatis的基礎環境我們已經準備好了,接下來就得分析下在上述的內容中,哪些對象可以交給Spring來管理?

  • Mybatis程式核心對象分析

    從圖中可以獲取到,真正需要交給Spring管理的是SqlSessionFactory

  • 整合Mybatis,就是將Mybatis用到的內容交給Spring管理,分析下配置文件

    說明:

    • 第一行讀取外部properties配置文件,Spring有提供具體的解決方案@PropertySource,需要交給Spring

    • 第二行起別名包掃描,為SqlSessionFactory服務的,需要交給Spring

    • 第三行主要用於做連接池,Spring之前我們已經整合了Druid連接池,這塊也需要交給Spring

    • 第四行是Mapper介面和映射文件[如果使用註解就沒有該映射文件],這個是在獲取到SqlSession以後執行具體操作的時候用,所以它和SqlSessionFactory創建的時機都不在同一個時間,可能需要單獨管理。

二、Spring整合Mybatis

前面我們已經分析了Spring與Mybatis的整合,大體需要做兩件事,

第一件事是:Spring要管理MyBatis中的SqlSessionFactory

第二件事是:Spring要管理Mapper介面的掃描

具體該如何實現,具體的步驟為:

步驟1:項目中導入整合需要的jar包

<dependency>
    <!--Spring操作資料庫需要該jar包-->
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>
<dependency>
    <!--
        Spring與Mybatis整合的jar包
        這個jar包mybatis在前面,是Mybatis提供的
    -->
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.0</version>
</dependency>

步驟2:創建Spring的主配置類

//配置類註解
@Configuration
//包掃描,主要掃描的是項目中的AccountServiceImpl類
@ComponentScan("com.itheima")
public class SpringConfig {
}
​

步驟3:創建數據源的配置類

在配置類中完成數據源的創建

public class JdbcConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String userName;
    @Value("${jdbc.password}")
    private String password;
​
    @Bean
    public DataSource dataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(userName);
        ds.setPassword(password);
        return ds;
    }
}

步驟4:主配置類中讀properties並引入數據源配置類

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import(JdbcConfig.class)
public class SpringConfig {
}
​

步驟5:創建Mybatis配置類並配置SqlSessionFactory

public class MybatisConfig {
    //定義bean,SqlSessionFactoryBean,用於產生SqlSessionFactory對象
    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        //設置模型類的別名掃描
        ssfb.setTypeAliasesPackage("com.itheima.domain");
        //設置數據源
        ssfb.setDataSource(dataSource);
        return ssfb;
    }
    //定義bean,返回MapperScannerConfigurer對象
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.itheima.dao");
        return msc;
    }
}

說明:

  • 使用SqlSessionFactoryBean封裝SqlSessionFactory需要的環境資訊

    • SqlSessionFactoryBean是前面我們講解FactoryBean的一個子類,在該類中將SqlSessionFactory的創建進行了封裝,簡化對象的創建,我們只需要將其需要的內容設置即可。

    • 方法中有一個參數為dataSource,當前Spring容器中已經創建了Druid數據源,類型剛好是DataSource類型,此時在初始化SqlSessionFactoryBean這個對象的時候,發現需要使用DataSource對象,而容器中剛好有這麼一個對象,就自動載入了DruidDataSource對象。

  • 使用MapperScannerConfigurer載入Dao介面,創建代理對象保存到IOC容器中

    • 這個MapperScannerConfigurer對象也是MyBatis提供的專用於整合的jar包中的類,用來處理原始配置文件中的mappers相關配置,載入數據層的Mapper介面類

    • MapperScannerConfigurer有一個核心屬性basePackage,就是用來設置所掃描的包路徑

步驟6:主配置類中引入Mybatis配置類

@Configuration
@ComponentScan("com.itheima")
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MybatisConfig.class})
public class SpringConfig {
}

步驟7:編寫運行類

在運行類中,從IOC容器中獲取Service對象,調用方法獲取結果

public class App2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
​
        AccountService accountService = ctx.getBean(AccountService.class);
​
        Account ac = accountService.findById(1);
        System.out.println(ac);
    }
}
​

步驟8:運行程式

至此Spring與Mybatis的整合就已經完成了,其中主要用到的兩個類分別是:

  • SqlSessionFactoryBean

  • MapperScannerConfigurer

三、Spring整合Junit

整合Junit與整合Druid和MyBatis差異比較大,為什麼呢?Junit是一個搞單元測試用的工具,它不是我們程式的主體,也不會參加最終程式的運行,從作用上來說就和之前的東西不一樣,它不是做功能的,看做是一個輔助工具就可以了。

3.1 環境準備

這塊環境,大家可以直接使用上面的環境即可。

3.2 整合Junit步驟

步驟1:引入依賴

pom.xml

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
​
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.2.10.RELEASE</version>
</dependency>
步驟2:編寫測試類

在test\java下創建一個AccountServiceTest,這個名字任意

//設置類運行器
@RunWith(SpringJUnit4ClassRunner.class)
//設置Spring環境對應的配置類
@ContextConfiguration(classes = {SpringConfig.class}) //載入配置類
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})//載入配置文件
public class AccountServiceTest {
    //支援自動裝配注入bean
    @Autowired
    private AccountService accountService;
    @Test
    public void testFindById(){
        System.out.println(accountService.findById(1));
​
    }
    @Test
    public void testFindAll(){
        System.out.println(accountService.findAll());
    }
}

注意:

  • 單元測試,如果測試的是註解配置類,則使用@ContextConfiguration(classes = 配置類.class)

  • 單元測試,如果測試的是配置文件,則使用@ContextConfiguration(locations={配置文件名,...})

  • Junit運行後是基於Spring環境運行的,所以Spring提供了一個專用的類運行器,這個務必要設置,這個類運行器就在Spring的測試專用包中提供的,導入的坐標就是這個東西SpringJUnit4ClassRunner

  • 上面兩個配置都是固定格式,當需要測試哪個bean時,使用自動裝配載入對應的對象。

知識點1:@RunWith

名稱 @RunWith
類型 測試類註解
位置 測試類定義上方
作用 設置JUnit運行器
屬性 value(默認):運行所使用的運行期

知識點2:@ContextConfiguration

名稱 @ContextConfiguration
類型 測試類註解
位置 測試類定義上方
作用 設置JUnit載入的Spring核心配置
屬性 classes:核心配置類,可以使用數組的格式設定載入多個配置類 locations:配置文件,可以使用數組的格式設定載入多個配置文件名稱