SpringBoot整合mybatis-generator-maven-plugin的⽅法前⾔:
SpringBoot版本 : 2.2.6
mybatis-generator-maven-plugin版本: 1.4.0
plugin 使⽤ mysql版本: 5.1.47
本次主要记录⽇和使⽤该插件⾃动⽣成pojo mapper xml⽂件(单表)
1 创建SpringBoot⼯程
这⾥不做具体记录,创建⼯程往常教程很多,
下⾯贴出重要⽂件和注意事项
1.l
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId&kay</groupId>
<artifactId>boot-mybatis-generator</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!--⽇志-->
<!--因为是基于AOP实现的所以这⾥必须要调⼊aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--门⾯-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<!--log4j12-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<!--插件-->
<plugins>
<!--springboot的maven插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<!--插件设置-->
<configuration>
<!--允许移动⽣成的⽂件-->
<verbose>true</verbose>
<!--启⽤覆盖-->
<overwrite>true</overwrite>
<!--⾃动⽣成配置如果名字是l可以省略配置-->
<!--<configurationFile>src/main/l</configurationFile>-->
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
1.l
server:
port: 8080
spring:
datasource:
driver-class-name: sql.jdbc.Driver
url: jdbc:mysql://localhost:3306/generaltor?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
username: root
password: lk0313
druid:
db-type: com.alibaba.druid.pool.DruidDataSource
#  下⾯不配置就是采⽤默认配置
#  filters: stat
#  maxActive: 20
#  initialSize: 1
#  maxWait: 60000
#  minIdle: 1
#  timeBetweenEvictionRunsMillis: 60000
#  minEvictableIdleTimeMillis: 300000
#  validationQuery: select 'x'
#  testWhileIdle: true
#  testOnBorrow: false
#  testOnReturn: false
#  poolPreparedStatements: true
#  maxOpenPreparedStatements: 20
mybatis:
# 该配置项配置了MyBatis配置⽂件保存路径
mapper-locations: classpath*:show/makey/dao/*.xml
# 配置扫描对应实体所在包,将mapper的实现类注⼊容器我们在启动器类上加了@MapperScan注解这⾥就不需要了
logging:
file:
path: /logs
# type-aliases-package: kay.dao
1.l
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否⽣成注释代时间戳-->
<property name="suppressDate" value="true"/>
<!-- 是否去除⾃动⽣成的注释 true:是: false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接地址账号密码-->
<jdbcConnection driverClass="sql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/generaltor?serverTimezone=Asia/Shanghai" userId="root" password="lk0313">
</jdbcConnection>
<javaTypeResolver>
<!--该属性可以控制是否强制DECIMAL和NUMERIC类型的字段转换为Java类型的java.math.BigDecimal,默认值为false,⼀般不需要配置。-->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--⽣成Model类存放位置-->
<javaModelGenerator targetPackage="kay.pojo" targetProject="src/main/java">
<!--enableSubPackages:如果true,MBG会根据catalog和schema来⽣成⼦包。如果false就会直接⽤targetPackage属性。默认为false。-->
<property name="enableSubPackages" value="true"/>
<!--trimStrings:是否对数据库查询结果进⾏trim操作,如果设置为true就会⽣成类似这样public void setUsername(String username) {this.username = username == null ? null : im();}的setter⽅法。默认值为false。-->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--⽣成映射xml⽂件存放位置-->
<sqlMapGenerator targetPackage="kay.dao" targetProject="src/main/resources/">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--⽣成Dao类存放位置(*Mapper.java)-->
<!-- 客户端代码,⽣成易于使⽤的针对Model对象和XML配置⽂件的代码
type="ANNOTATEDMAPPER",⽣成Java Model 和基于注解的Mapper对象
type="MIXEDMAPPER",⽣成基于注解的Java Model 和相应的Mapper对象
type="XMLMAPPER",⽣成SQLMap XML⽂件和独⽴的Mapper接⼝
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="kay.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--⽣成对应表及类名-->
<table tableName="student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
<!--useActualColumnNames:如果设置为true,那么MBG会使⽤从数据库元数据获取的列名作为⽣成的实体对象的属性。如果为false(默认值),MGB将会尝试将返回的名称转换为驼峰形式。在这两种情况下,可以通过元素显⽰指定,在这种情况下将会忽略这个      <property name="useActualColumnNames" value="true"/>
<!-- 数据库表主键 -->
<generatedKey column="id" sqlStatement="Mysql" identity="true" />
</table>
</context>
</generatorConfiguration>
步骤:
1导⼊依赖
2创建*l配置⽂件
3配置yml配置⽂件
注意:
pom中generalto-maven-plugs中必须指定mysql驱动,并且明确版本
标签中需要指定tableName和⽣成的实体名字
2 创建数据库表
-- 建表语句:
/*
SQLyog Enterprise v12.09 (64 bit)
MySQL - 5.5.40 : Database - generaltor
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
/*!40101 SET SQL_MODE=''*/;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`generaltor` /*!40100 DEFAULT CHARACTER SET utf8 */;
/*Table structure for table `student` */
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`name` varchar(15) DEFAULT NULL,
`sex` varchar(2) DEFAULT NULL,
`phone` int(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `student` */
insert into `student`(`id`,`name`,`sex`,`phone`) values (1,'lk','男',12345578);
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/
*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
3 运⾏
如果使⽤IDEA 我们只需要到右边侧栏中的maven(没有记得调出了)
到plugins–>mybatis-generaltor–>mybatis-generaltor:generaltor 之后双击即可,此时刷新⼀下项⽬就⾃动⽣成我们想要的,mapper和xml以及pojo
4 测试
创建穿点controller和service 来完成⼀次测试,
ller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.JstlUtils;
kay.dao.StudentMapper;
kay.pojo.Student;
kay.service.UserService;
springframework jar包下载import java.util.List;
/**
* @ClassName: UserController
* @description: ⽤户Controller
* @Author: MrKay
* @Date: 2020/11/8
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/alllist")
public List<Student> selectAll(){
return userService.selectAll();
}
}
kay.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
kay.dao.StudentMapper;
kay.pojo.Student;
import java.util.List;
/**
* @ClassName: UserService
* @description: UserService
* @Author: MrKay
* @Date: 2020/11/8
*/
@Service
public class UserService {
@Autowired
private StudentMapper studentMapper;
public List<Student> selectAll(){
List<Student> students = studentMapper.selectAll();
return students;
}
}
kay;
batis.spring.annotation.MapperScan;
batis.spring.annotation.MapperScans;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("kay.dao")
public class GeneratorApplication {
public static void main(String[] args) {
SpringApplication.run(GeneratorApplication.class, args);
}
}
注意:
启动器类中需要加⼊@MapperScan(“mapper接⼝所在包”)注解,这样我们就不需要⼿动创建实现类了,mybatis会⾃动帮我们创建
启动之后:
地址栏输⼊ localhost:8080/user/alllist
得到查询结果:
[{"id":1,"name":"lk","sex":"男","phone":12345578}]
5 完整代码
6 总结
便于⾃⼰以后使⽤,做的⼀个demo
到此这篇关于SpringBoot整合mybatis-generator-maven-plugin的⽂章就介绍到这了,更多相关SpringBoot整合mybatis-generator-maven-plugin内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家
以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。