首頁 >Java >java教程 >SpringBoot怎麼整合tkMapper

SpringBoot怎麼整合tkMapper

王林
王林轉載
2023-05-12 12:58:061265瀏覽

SpringBoot整合tkMapper

搭建一個SpringBoot的項目,怎麼搭建網路教學一大堆,這裡就不描述了直接進入正題,首先先看一下整體的項目結構

SpringBoot怎麼整合tkMapper

pom檔案引入依賴(注意依賴衝突)

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--lombok省略代码-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- h3嵌入式数据-->
        <dependency>
            <groupId>com.h3database</groupId>
            <artifactId>h3</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- tk-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>3.4.5</version>
        </dependency>

    </dependencies>

在resources目錄下新建db資料夾,用來存放資料文件,其中schema-h3.sql為資料庫Schema 腳本,data-h3.sql為Data腳本

schema-h3.sql

#
DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT &#39;主键ID&#39;,
    name VARCHAR(30) NULL DEFAULT NULL COMMENT &#39;姓名&#39;,
    age INT(11) NULL DEFAULT NULL COMMENT &#39;年龄&#39;,
    email VARCHAR(50) NULL DEFAULT NULL COMMENT &#39;邮箱&#39;,
    PRIMARY KEY (id)
);

data-h3.sql

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, &#39;red&#39;, 18, &#39;test1@tian.com&#39;),
(2, &#39;yll&#39;, 20, &#39;test2@tian.com&#39;),
(3, &#39;putty&#39;, 22, &#39;test3@tian.com&#39;),
(4, &#39;ele&#39;, 24, &#39;test4@tian.com&#39;),
(5, &#39;tom&#39;, 26, &#39;test5@tian.com&#39;);

#application.yml

spring:
  datasource:
    driver-class-name: org.h3.Driver
    schema: classpath:db/schema-h3.sql
    data: classpath:db/data-h3.sql
    url: jdbc:h3:mem:root
    username: root
    password: root

在產生的專案路徑下新建一個tk 套件,在tk 套件下新建bean套件(存放實體類別),dao 套件(存放Mapper 介面類別),config 套件(存放設定類別)

tk 套件下新建通用Mapper介面MyMapper

繼承Mapper和MySqlMapper,以後我們的業務dao直接整合MyMapper即可

此介面不能被掃描到,否則會報錯

MyMapper.java

package com.tian.tkmapper;

import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

@Repository
public interface MyMapper<T> extends Mapper<T> ,MySqlMapper<T> {
}

bean套件下新User類,此處使用了Lombok省略程式碼,@Data註解包含get/設定等方法

User.java

package com.tian.tkmapper.tk.bean;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

#dao套件下新建業務介面UserDao

UserDao.java

package com.tian.tkmapper.tk.dao;

import com.tian.tkmapper.MyMapper;
import com.tian.tkmapper.tk.bean.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao extends MyMapper<User> {
}

config套件下新配置類別MybatisConfigurer

MybatisConfigurer.java

#
package com.tian.tkmapper.tk.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class MybatisConfigurer {
    @Resource
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.tian.tkmapper.tk.bean");

        //添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        bean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        return bean.getObject();
    }

    @Configuration
    @AutoConfigureAfter(MybatisConfigurer.class)
    public static class MyBatisMapperScannerConfigurer {

        @Bean
        public MapperScannerConfigurer mapperScannerConfigurer() {
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
            mapperScannerConfigurer.setBasePackage("com.tian.tkmapper.tk.dao.*");
            //配置通用mappers
            Properties properties = new Properties();
            properties.setProperty("mappers", "com.tian.tkmapper.MyMapper");
            properties.setProperty("notEmpty", "false");
            properties.setProperty("IDENTITY", "MYSQL");
            mapperScannerConfigurer.setProperties(properties);
            return mapperScannerConfigurer;
        }
    }
   }

在啟動類別中新增@MapperScan

package com.tian.tkmapper;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan(basePackages = "com.tian.tkmapper.tk.dao")//mapper接口的路径
public class TkmapperApplication {

    public static void main(String[] args) {
        SpringApplication.run(TkmapperApplication.class, args);
    }
}

測試程式碼進行測試

package com.tian.tkmapper;

import com.tian.tkmapper.tk.bean.User;
import com.tian.tkmapper.tk.dao.UserDao;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TkmapperApplicationTests {
    @Autowired
    private UserDao userDao;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userDao.selectAll();
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }
   }

最後看到顯示輸出為如下即成功

SpringBoot怎麼整合tkMapper

#8 遇到的坑

    a> MyMapper不能被扫描到,解决方案很多,不让启动类启动时扫描到就可以
    b> 启动类中@MapperScan要引tk包下面的 import tk.mybatis.spring.annotation.MapperScan;
    c>pom文件依赖冲突

以上是SpringBoot怎麼整合tkMapper的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除