1. 程式人生 > 實用技巧 >springboot整合通用Mapper

springboot整合通用Mapper

1.加入通用mapper依賴

<!--mybatis通用mapper依賴-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>

完整pom.xml檔案

<?xml version="
1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.qingfeng</groupId> <artifactId>ExportPOI</artifactId> <version>1.0
-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1
.0</version> </dependency> <!--mybatis通用mapper依賴--> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>5.1.41</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!-- 這個外掛,可以將應用打成一個可以執行的jar包 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.建立通用的Mapper介面
package com.qingfeng.my.mapper;

import org.apache.poi.ss.formula.functions.T;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;

/**
 * 通用的Mapper介面
 * 注意:公用通用介面MyMapper<T>要單獨的存在,避免在啟動類掃描的的時候@MapperScan掃描到會報錯
 */
public interface MyMapper<T> extends Mapper<T>,MySqlMapper<T> {

}

3.application.yml的配置#埠配置

server:
  port: 9001
#jdbc配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test_user?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: wq
#mybatis配置 mybatis: #實體類所在包名 type-aliases-package: com.qingfeng.pojo #通用mapper配置 mapper: #公用通用Mapper介面類路徑,(公用通用介面MyMapper<T>要單獨的存在另一個包和普通dao分開,以免啟動類的@MapperScan掃描到會報錯) mappers: com.qingfeng.my.mapper.MyMapper not-empty: false identity: MYSQL

4.UserMapper介面要繼承我們自己的通用mapper
package com.qingfeng.mapper;

import com.qingfeng.my.mapper.MyMapper;
import com.qingfeng.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface UserMapper extends MyMapper<User> {


}

5.User實體類

package com.qingfeng.pojo;

import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;

@Table(name = "user")//指定資料庫中對應的表名
public class User implements Serializable {

@Id
private Integer id;

private String username;

private String password;

private String phone;

private Date createTime;

public User(Integer id, String username, String password, String phone, Date createTime) {
this.id = id;
this.username = username;
this.password = password;
this.phone = phone;
this.createTime = createTime;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public Date getCreateTime() {
return createTime;
}

public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", createTime=" + createTime +
'}';
}
}

  

6.測試

package com.qingfeng;

import com.qingfeng.mapper.UserMapper;
import com.qingfeng.pojo.User;
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;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTest {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void getUserList(){
        List<User> userList = userMapper.selectAll();
        for (User user : userList){
            System.out.println("user:"+user);
        }
    }


}

測試結果:

user:User{id=1, username='admin', password='12134', phone='123699996666', createTime=Wed Sep 09 16:28:25 CST 2020}
user:User{id=2, username='spring', password='123456', phone='13698746589', createTime=Wed Sep 09 16:28:28 CST 2020}
user:User{id=3, username='test', password='12134', phone='123699996666', createTime=Wed Sep 09 16:28:25 CST 2020}
user:User{id=4, username='springboot', password='123456', phone='13698746589', createTime=Wed Sep 09 16:28:28 CST 2020}
user:User{id=5, username='test1', password='12134', phone='123699996666', createTime=Wed Sep 09 16:28:25 CST 2020}