6. SpringBoot 集成Mybatis 生成逆序工具
📕 引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
📕 mybatis的yaml配置文件
#mysql数据库信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/knowrikc?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=jiejie01
#mybatis的配置信息
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.yjren.yjrenweb.model
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
📕 创建User.sql的脚本
create table `t_coun_user`(
`id` varchar(100) not null comment 'id';
`name` varchar(100) not null comment '名称';
) Engine = InnoDb Charset = utf8mb4 ;
📕 创建User.java实体类
package com.ruleyou1.model;
import lombok.Data;
@Data
public class User {
private String id;
private String name;
}
📕 创建UserMapper接口
package com.ruleyou1.mapper;
import com.ruleyou1.model.User;
import java.util.List;
public interface UserMapper {
public List<User> getAllUser();
}
📕 Resources/mapper目录下UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yjren.yjrenweb.mapper.UserMapper">
<select id="getAllUser" resultType="com.yjren.yjrenweb.model.User">
select * from t_coun_user;
</select>
</mapper>
📕 启动类添加Mapper扫描注解@MapperScan
package com.ruleyou1;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
@MapperScan("com.yjren.yjrenweb.mapper")
public class RuleYou1WebApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(YjrenWebApplication.class, args);
Environment environment = context.getBean(Environment.class);
System.out.println("访问链接:http://localhost:" + environment.getProperty("server.port")+environment.getProperty("server.servlet.context-path"));
System.out.println("(♥◠‿◠)ノ゙ 项目启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \ \ \ / / \n" +
" | ( ' ) | \ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\ \ | || |(_,_)' \n" +
" | | \ `' /| `-' / \n" +
" | | \ / \ / \n" +
" ''-' `'-' `-..-' ");
}
}
📕 Controller 添加测试访问入口
@Controller
@Slf4j
public class TestController {
@Resource
private UserMapper userMapper;
@RequestMapping(value = "/getUser",method = RequestMethod.POST)
@ResponseBody
public List<User> getUser(){
return userMapper.getAllUser();
}
}
访问启动入口 hppt://localhost:8081/getUser
📕 增加mybatis-generator逆序生成工具插件
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
📕 generatorConfig配置文件
<?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="baseTable" targetRuntime="MyBatis3">
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- genenat entity时,生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
<!--生成mapper.xml时覆盖原文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<commentGenerator>
<!-- 关闭自动生成的注释 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/knowrikc?useUnicode=true&characterEncoding=UTF-8"
userId="root" password="jiejie01">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<javaModelGenerator
targetPackage="com.ruleyou1.model.origin"
targetProject="src\main\java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper\origin"
targetProject="src\main\resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.ruleyou1.mapper"
targetProject="src\main\java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="t_coun_software_base_info" domainObjectName="SoftwareBaseInfoEntity"
enableInsert="true" enableSelectByPrimaryKey="true"
enableSelectByExample="true" enableUpdateByPrimaryKey="true"
enableDeleteByPrimaryKey="true" enableDeleteByExample="true"
enableCountByExample="true" enableUpdateByExample="true"
selectByExampleQueryId="true">
<property name="runtimeTableName" value="t_coun_software_base_info"/>
<generatedKey column="id" sqlStatement="Mysql" identity="true"
type="post"/>
</table>
<!--根据数据库的表结构进行生成,上面是一个示例-->
</context>
</generatorConfiguration>
📕 maven-plugin 启动
📕 右侧maven 命令启动