Spring Boot Mybatis 搞反向工程,太方便咯。。
1. 拷贝 Mybatis 反向工程配置文件到项目的根目录下
2. 根据项目及表的情况,修改 GeneratorMapper.xml 配置
如果使用 高版本 , 驱动类变为:com.mysql.cj.jdbc.Driver
url 后面应该加属性 nullCatalogMeansCurrent=true ,否则生成有问题
当前版本 MySQL 数据库为 5.7
主要根据注释来修改自己的内容
<?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>
此时会报错,如下
这个时候可以不用理会,项目也是会正常运行的,当然也可以这样:
添加之后就不会报红了。
Spring Boot 理论+实战系列教程大家看这个:
https://github.com/javastacks/spring-boot-best-practice
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>
4. 双击生成相关文件
5. 生成的文件
自动生成model/Student、实体类
以及StudentMapper,接口
StudentMapper.xml 具体对数据库的操作
这样方便我们使用,具体的下面详细介绍,注意看注释
Student
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
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
<?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>
关注微信公众号:互联网架构师,在后台回复:2T,可以获取Mybatis 教程,都是干货。
本文来源:https://blog.csdn.net/m0_37922192/article/details/109248865
Spring Boot Mybatis 搞反向工程,太方便咯。。相关推荐
- 第七章、Spring Boot MyBatis升级篇
课时二十七.Spring Boot MyBatis升级篇-注解 缘起:在一节视频中,有这么一段留言:"会不会推出SpringBoot整合Mybaits配置文件sqlMapConfig.xml ...
- Spring boot Mybatis 整合(注解版)
之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...
- Spring Boot + Mybatis 配合 AOP 和注解实现动态数据源切换配置
Spring Boot + Mybatis 配合 AOP 和注解实现动态数据源切换配置 前言: 1. 数据库准备: 2. 环境准备: 3.代码部分 4. 测试: 5.等等 6.配合注解实现 7 .测试 ...
- Eclipse + Spring boot +mybatis + mysql
Eclipse + Spring boot +mybatis + mysql 如题.使用Springboot 2.0 版本进行网页的开发.原理和优点很多博文已经讲过了,这里不再赘述.但是很多项目按照他 ...
- spring boot+mybatis整合
LZ今天自己搭建了下Spring boot+Mybatis,比原来的Spring+SpringMVC+Mybatis简单好多.其实只用Spring boot也可以开发,但是对于多表多条件分页查询,Sp ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- 在Spring Boot + Mybatis 中,使用@Repository失效
在Spring Boot + Mybatis 中,使用@Repository失效 在springboot 中,给mapper的接口上加上@Repository,无法生成相应的bean,从而无法@Aut ...
- Spring Boot + Mybatis 快速整合
引言 最近在工作结束后抽时间学习了一下mybatis的知识,因为之前有学习过,但是经久不用,也未曾踏实地整理,因此有所淡忘. super meeting会议管理系统是我厂最近开发的一套会议预约平台.持 ...
- Spring Boot MyBatis
MyBatis简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis . 集成spring bo ...
- spring boot + mybatis + layui + shiro后台权限管理系统
后台管理系统 版本更新 后续版本更新内容 链接入口: springboot + shiro之登录人数限制.登录判断重定向.session时间设置:https://blog.51cto.com/wyai ...
最新文章
- C#中运行命令行截取输出流的例子
- 2016-01-18 10:33PM at Parkyard difference between WebUI and Fiori
- etf基金代码大全_银行ETF最新规模首超28亿元再创历史新高,近4个月资金净流入超12亿元...
- Python SQLAlchemy --3
- vue 接口节流_vue防抖节流之v-debounce--throttle使用指南
- PostgreSQL数据目录深度揭秘
- 记一次阿里巴巴一面的经历
- Java并发学习笔记:ReentrantLock
- springboot过滤器配置
- 计算地球经纬度两点之间的弧长
- Java分布式系统框架教程,架构设计
- Unity材质:玻璃
- java list判断元素_Java的ListObject如何判断元素类别
- 计算机mac地址怎么修改密码,如何修改苹果电脑mac地址?
- Android界面布局练习
- 博士第六年还没有发Paper是一种什么样的体验?
- HashMap扩容为什么是2的倍数
- Bash Shell中16进制数字和ASCII字符相互转换
- Android手柄如何玩gta5,gta5手机怎么拿出来
- 主元分析matlab,matlab spss做因子分析 主元分析 factor analysis 学习笔记
热门文章
- 输入url后的加载过程
- SqlServer 在创建数据库时候指定的初始数据库大小是不能被收缩的
- centos 6.5 搭建svn
- 0=iiLen 在C++中
- apache网站访问需要密码设置步骤总结
- 【STM32 .Net MF开发板学习-10】SPI测试之触摸屏坐标获取
- 「leetcode」501. 二叉搜索树中的众数【暴力统计】【中序遍历】详解
- 苹果Ma视频剪辑软件:DaVinci Resolve Studio (达芬奇调色)
- 在 Mac 上如何放大或缩小?
- QuarkXPress 2021 for Mac(图文设计排版布局工具)