SpringBoot集成通用Mapper
2021-04-23 本文已影响0人
WebGiser
参考地址:https://github.com/abel533/Mapper/wiki
通用mapper 可以极大的方便开发人员进行ORM,提供极其方便的单表增删改查。
项目结构
image.pngpom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.geovis</groupId>
<artifactId>springboot_study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_study</name>
<description>Spring Boot练习</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>
${basedir}/src/main/resources/generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
application.properties
# 应用程序的一些通用配置
spring.application.name=SpringBootStudy
server.port=8080
# spring日志配置(logging.file.name比logging.file.path的优先级高)
logging.file.name=E:/temp/${spring.application.name}.log
# spring日期设置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# mysql数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.41.140:3306/data_resource_manager?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# spring文件上传大小设置
spring.servlet.multipart.max-file-size = -1
spring.servlet.multipart.max-request-size = -1
主函数 SpringbootStudyApplication.java
package com.geovis.springboot_study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@MapperScan(basePackages = "com.geovis.springboot_study.dao")
@SpringBootApplication
public class SpringbootStudyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootStudyApplication.class, args);
}
}
resource/generator/generatorConfig.xml
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<properties resource="generator/config.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
<property name="caseSensitive" value="true"/>
</plugin>
<jdbcConnection driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.url}"
userId="${jdbc.user}"
password="${jdbc.password}">
</jdbcConnection>
<!-- java实体 -->
<javaModelGenerator targetPackage="com.geovis.springboot_study.entity"
targetProject="src/main/java"/>
<!-- mapper.xml文件 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="src/main/resources"/>
<!-- mapper接口 -->
<javaClientGenerator targetPackage="com.geovis.springboot_study.dao"
targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="{tableName}">
<generatedKey column="id" sqlStatement="JDBC"/>
</table>
</context>
</generatorConfiguration>
resource/generator/config.properties
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.41.140:3306/data_resource_manager?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
jdbc.user=root
jdbc.password=123456
tableName=youju
通用 Mapper 专用代码生成器
使用该插件可以很方便的生成实体类、Mapper接口以及对应的XML文件
命令行窗口执行 mvn mybatis-generator:generate即可 或 点击右侧maven插件,即可生成 dao/xxxMapper.java接口、entity/xxxMapper.java实体类、resources/mapper/xxxMapper.xml文件。
image.png
TestController.java
package com.geovis.springboot_study.controller;
import com.geovis.springboot_study.dao.YoujuMapper;
import com.geovis.springboot_study.entity.Youju;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
@RestController
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private YoujuMapper youjuMapper;
@GetMapping(value = "addYouju")
public void addYouju(){
Youju youju = new Youju();
youju.setId("111");
youju.setDizhi("aaa");
// 如果使用 inserSelective 就会只给有值的字段赋值(会对传进来的值做非空判断)
youjuMapper.insertSelective(youju);
}
@GetMapping(value = "deleteYouju")
public void deleteYouju(){
youjuMapper.deleteByPrimaryKey("111");
}
@GetMapping(value = "updateYouju")
public void updateYouju(){
Youju youju = new Youju();
youju.setId("111");
youju.setDizhi("bbb");
youjuMapper.updateByPrimaryKeySelective(youju);
}
@GetMapping(value = "/selectYouju")
public List selectYouju(){
return youjuMapper.selectAll();
}
@GetMapping(value = "/selectYouju2")
public List selectYouju2(){
Example example = new Example(Youju.class);
example.createCriteria().andLike("dizhi", "%建设大道%");
example.orderBy("id").desc();
return youjuMapper.selectByExample(example);
}
}