SpringBoot | 集成mybatis代码生成器
首先,创建一个数据库表demo,添加一些数据。
drop table if exists `demo`;
create table `demo` (
`id` bigint not null comment 'id',
`name` varchar(50)comment'名称',
primary key (`id`)
)engine=innodb default charset=utf8mb4 comment='测试';
insert into `demo`(id,name)values(1,'测试');
分析:自动生成器会根据表生成对应的实体类、mapper接口、mapper映射文件。
1.添加插件
<!-- mybatis generator 自动生成代码插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>src/main/resources/generator/generator-config.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
</plugin>
2.创建文件 generator-config.xml
在src/main/resources/generator
下创建generator-config.xml
。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat">
<!-- 自动检查关键字,为关键字增加反引号 -->
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--覆盖生成XML文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 生成的实体类添加toString()方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 不生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/wiki?characterEncoding=UTF8&autoReconnect=true&serverTimezone=UTC"
userId="wiki"
password="wiki">
</jdbcConnection>
<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
targetPackage="com.nathan.wiki.domain"/>
<!-- mapper xml的位置 -->
<sqlMapGenerator targetProject="src\main\resources"
targetPackage="mapper"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
targetPackage="com.nathan.wiki.mapper"
type="XMLMAPPER"/>
<!--<table tableName="demo" domainObjectName="Demo"/>-->
<!--<table tableName="ebook"/>-->
<!--<table tableName="category"/>-->
<!--<table tableName="doc"/>-->
<!--<table tableName="content"/>-->
<!--<table tableName="user"/>-->
<table tableName="demo" domainObjectName="Demo" />
</context>
</generatorConfiguration>
其中,这两处的包名(targetPackage)根据实际情况修改:
<!-- domain类的位置 -->
<javaModelGenerator targetProject="src\main\java"
targetPackage="com.nathan.wiki.domain"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src\main\java"
targetPackage="com.nathan.wiki.mapper"
type="XMLMAPPER"/>
其中的connectionURL
、userId
和wiki
也根据实际情况修改:
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/wiki?characterEncoding=UTF8&autoReconnect=true&serverTimezone=UTC"
userId="wiki"
password="wiki">
</jdbcConnection>
上述代码会将数据库表生成对应的四个文件:实体类、案例类、mapper接口、mapper映射。
3.编译运行
1、配置maven命令。
2、编译生成。
启动命令,将会自动生成上述四个文件。
4.案例测试
案例-使用generator生成的文件,获取数据库表(demo)的所有信息。
1、创建service接口。
package com.nathan.wiki.service;
import com.nathan.wiki.domain.Demo;
import com.nathan.wiki.mapper.DemoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class DemoService {
@Resource
private DemoMapper demoMapper;
public List<Demo> list() {
// selectByExample 是 自动生成的Mapper接口中的方法、
return demoMapper.selectByExample(null);
}
}
分析: selectByExample
对应sql语句位于自动生成的DemoMapper.xml
中。
<select id="selectByExample" parameterType="com.nathan.wiki.domain.DemoExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
<include refid="Base_Column_List" />
from demo
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
更多方法如下。
long countByExample(DemoExample example);
int deleteByExample(DemoExample example);
int deleteByPrimaryKey(Long id);
int insert(Demo record);
int insertSelective(Demo record);
List<Demo> selectByExample(DemoExample example);
Demo selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") Demo record, @Param("example") DemoExample example);
int updateByExample(@Param("record") Demo record, @Param("example") DemoExample example);
int updateByPrimaryKeySelective(Demo record);
int updateByPrimaryKey(Demo record);
2、创建Controller。
package com.nathan.wiki.controller;
import com.nathan.wiki.domain.Demo;
import com.nathan.wiki.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
//使用restController
@RestController
@RequestMapping("/demo")
public class DemoController {
@Resource
private DemoService demoService;
@GetMapping("/list")
public List<Demo> list(){
return demoService.list();
}
}
3、http测试
在demo.http
中。
GET http://localhost:8080/demo/list
结果:
来源: 博客园
作者: 茶哩哩
文章: 转载请注明原文链接:https://www.cnblogs.com/martin-1/p/16242039.html