速读摘要文章源自JAVA秀-https://www.javaxiu.com/19996.html
欢迎加入我的知识星球,一起探讨架构,交流源码。MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/19996.html
原文约 3976 字 | 图片 11 张 | 建议阅读 8 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/19996.html
Spring Boot Mybatis 搞反向工程,太方便咯。。
点击关注 ? Java基基 文章源自JAVA秀-https://www.javaxiu.com/19996.html
收录于话题文章源自JAVA秀-https://www.javaxiu.com/19996.html
#Java基基文章源自JAVA秀-https://www.javaxiu.com/19996.html
25个文章源自JAVA秀-https://www.javaxiu.com/19996.html
点击上方“Java基基”,选择“设为星标”文章源自JAVA秀-https://www.javaxiu.com/19996.html
做积极的人,而不是积极废人!文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html 源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/19996.html
原创 | Java 2020 超神之路,很肝~文章源自JAVA秀-https://www.javaxiu.com/19996.html
中文详细注释的开源项目文章源自JAVA秀-https://www.javaxiu.com/19996.html
RPC 框架 Dubbo 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
网络应用框架 Netty 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
消息中间件 RocketMQ 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
数据库中间件 Sharding-JDBC 和 MyCAT 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
作业调度中间件 Elastic-Job 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
分布式事务中间件 TCC-Transaction 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
Eureka 和 Hystrix 源码解析文章源自JAVA秀-https://www.javaxiu.com/19996.html
Java 并发源码文章源自JAVA秀-https://www.javaxiu.com/19996.html
来源:blog.csdn.net/m0_37922192/文章源自JAVA秀-https://www.javaxiu.com/19996.html
article/details/109248865文章源自JAVA秀-https://www.javaxiu.com/19996.html
1. 拷贝 Mybatis 反向工程配置文件到项目的根目录下文章源自JAVA秀-https://www.javaxiu.com/19996.html
2. 根据项目及表的情况,修改 GeneratorMapper.xml 配置文章源自JAVA秀-https://www.javaxiu.com/19996.html
3. 在pom.xml 文件中添加 mysql 反向工程依赖文章源自JAVA秀-https://www.javaxiu.com/19996.html
4. 双击生成相关文件文章源自JAVA秀-https://www.javaxiu.com/19996.html
5. 生成的文件文章源自JAVA秀-https://www.javaxiu.com/19996.html
大家好,我是基基!文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
今天来分享一个 MyBattis 提效的神器!文章源自JAVA秀-https://www.javaxiu.com/19996.html
1. 拷贝 Mybatis 反向工程配置文件到项目的根目录下文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
2. 根据项目及表的情况,修改 GeneratorMapper.xml 配置
如果使用 高版本 , 驱动类变为:com.mysql.cj.jdbc.Driver文章源自JAVA秀-https://www.javaxiu.com/19996.html
url 后面应该加属性 nullCatalogMeansCurrent=true ,否则生成有问题文章源自JAVA秀-https://www.javaxiu.com/19996.html
当前版本 MySQL 数据库为 5.7文章源自JAVA秀-https://www.javaxiu.com/19996.html
主要根据注释来修改自己的内容文章源自JAVA秀-https://www.javaxiu.com/19996.html
<?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> <!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 --> <classPathEntry location="E:\Java\tool\maven_repository\mysql\mysql-connector-java\5.1.9\mysql-connector-java-5.1.9.jar"/> <!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 --> <context id="tables" targetRuntime="MyBatis3"> <!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 配置数据库连接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot" userId="root" password="123456"> </jdbcConnection> <!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定 生成的 model 放在 IDEA 的哪个工程下面--> <javaModelGenerator targetPackage="com.md.springboot.model" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="false"/> </javaModelGenerator> <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的 包名, targetProject 指定生成的 mapper.xml 放在 IDEA 的哪个工程下面 --> <sqlMapGenerator targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包 名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪个工程下面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.md.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 数据库表名及对应的 Java 模型类名,有几个表写几个table --> <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context></generatorConfiguration>
此时会报错,如下文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
这个时候可以不用理会,项目也是会正常运行的,当然也可以这样文章源自JAVA秀-https://www.javaxiu.com/19996.html
添加之后就不会报红了文章源自JAVA秀-https://www.javaxiu.com/19996.html
3. 在pom.xml 文件中添加 mysql 反向工程依赖
<build> <plugins> <!--mybatis 代码自动生成插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.6</version> <configuration> <!--配置文件的位置--> <configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>1234567891011121314151617
4. 双击生成相关文件
文章源自JAVA秀-https://www.javaxiu.com/19996.html
5. 生成的文件
自动生成model/Student、实体类文章源自JAVA秀-https://www.javaxiu.com/19996.html
以及StudentMapper,接口文章源自JAVA秀-https://www.javaxiu.com/19996.html
StudentMapper.xml 具体对数据库的操作文章源自JAVA秀-https://www.javaxiu.com/19996.html
这样方便我们使用,具体的下面详细介绍,注意看注释文章源自JAVA秀-https://www.javaxiu.com/19996.html
Student文章源自JAVA秀-https://www.javaxiu.com/19996.html
package com.md.springboot.model;public class Student { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
StudentMapper文章源自JAVA秀-https://www.javaxiu.com/19996.html
package com.md.springboot.mapper;import com.md.springboot.model.Student;public interface StudentMapper { int deleteByPrimaryKey(Integer id); int insert(Student record); int insertSelective(Student record); Student selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Student record); int updateByPrimaryKey(Student record);}
StudentMapper.xml文章源自JAVA秀-https://www.javaxiu.com/19996.html
<?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.md.springboot.mapper.StudentMapper"> <!-- 数据库字段名称 实体对象属性名称 user_name userName user_age userAge --> <!-- 如果数据表中的字段是多个单词构成的,通过Mybatis逆向工程生成的对象属性名称 会按照驼峰命名法的规则生成属性名称 自己设计数据表的时候,多个单词之前使用下划线分隔 --> <!-- resultMap的作用 1. 当数据库中的字段名称和实体类对象的属性名不一致,可以进行转换 2. 当前查询的结果对象没有对应一个表时,可以自定义一个结果集 --> <resultMap id="BaseResultMap" type="com.md.springboot.model.Student"> <!-- id标签只能修饰主键字段,result标签修饰其他字段 column 数据库中的字段名称 property 映射对象的属性名称 jdbcType 对应的类型 --> <id column="id" jdbcType="INTEGER" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="INTEGER" property="age" /> </resultMap> <!--sql语句片段,将公共部分抽出--> <sql id="Base_Column_List"> id, name, age </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_student where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from t_student where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.md.springboot.model.Student"> insert into t_student (id, name, age ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER} ) </insert> <!--动态sql--> <insert id="insertSelective" parameterType="com.md.springboot.model.Student"> insert into t_student <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="name != null"> name, </if> <if test="age != null"> age, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="age != null"> #{age,jdbcType=INTEGER}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.md.springboot.model.Student"> update t_student <set> <if test="name != null"> name = #{name,jdbcType=VARCHAR}, </if> <if test="age != null"> age = #{age,jdbcType=INTEGER}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.md.springboot.model.Student"> update t_student set name = #{name,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update></mapper>
文章源自JAVA秀-https://www.javaxiu.com/19996.html文章源自JAVA秀-https://www.javaxiu.com/19996.html
欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢:文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/19996.html
提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/19996.html
获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章源自JAVA秀-https://www.javaxiu.com/19996.html
文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)文章源自JAVA秀-https://www.javaxiu.com/19996.html
阅读原文文章源自JAVA秀-https://www.javaxiu.com/19996.html

评论