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-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 
#通用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}

 

 

posted @ 2020-09-09 18:46  Amy清风  阅读(603)  评论(0编辑  收藏  举报