Springboot+Mybatis的逆向工程
Mybatis逆向工程,自动生成 entity类和常用的增删改查方法。
- 1.pom.xml引入类
<!-- 通用mapper 用于mabatis封装的基础增删改查的功能-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId><!--已引入了这个,故此处例外掉其默认自带的-->
</exclusion>
</exclusions>
</dependency>
<!-- 逆向工程 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
<scope>provided</scope>
</dependency>
- 2.springboot入口类,引入换一下
- 3.配置文件:\src\main\resources\resources\generatorConfig.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>
<properties resource="application.properties"></properties> <!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="D:\sqljdbc4.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<!--<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=gkzp" userId="sa" password="nsoft">
</jdbcConnection>-->
<jdbcConnection driverClass="${spring.datasource.druid.driverClassName}" connectionURL="${spring.datasource.druid.url}" userId="${spring.datasource.druid.username}" password="${spring.datasource.druid.password}">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.generator.model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="com.generator.mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.generator.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<!--
<table tableName="HR_Recruit_entryInfo_base" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recruit_entryInfo_education" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recruit_entryInfo_family" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recruit_entryInfo_other" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recruit_entryInfo_work" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> <table tableName="Dept" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Post_Type" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recurit_Plan_Needs" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
<table tableName="HR_Recurit_Plan_Return" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
-->
<table tableName="sys_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table> </context>
</generatorConfiguration>
源代码
- 4.配置Java类
\src\main\java\com\nsoft\gkzp\system\sysuser\service\SysUserServiceImpl.java
File configFile文件参数指定generatorConfig.xml配置文件位置
package com.nsoft.gkzp.util;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File;
import java.util.ArrayList;
import java.util.List; public class GeneratorSqlmap {
public void generator() throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("./src/main/resources/resources/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,callback, warnings);
myBatisGenerator.generate(null);
}
/**
* 运行这个main方法就可以进行逆向工程
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行上面的main方法,就会自动生成三个文件(以sys_User表为例)(如下图)
create table sys_user(
id int identity(1,1) primary key not null, --id主键 自增长
loginName nvarchar(50) NOT NULL , --登录用户名
password nvarchar(50) NOT NULL , --登录密码
email nvarchar(255) , --用户邮箱 (邮箱登录)
phone nvarchar(15) , --用户手机号(手机号登录)
createTime datetime default CONVERT(varchar(100), GETDATE(), 20), --用户创建时间
pwdChangeTime datetime default CONVERT(varchar(100), GETDATE(), 20), --改密码时间(初始值为用户创建时间)
code varchar(255) , --'激活码'
usertype int NOT NULL default 2, --用户类型(0系统管理员 1、内网用户 2、普通用户)
state int NOT NULL default 0 , --用户激活状态:0表示未激活,1表示激活
nstatusid int default 1 --用户状态 0无效 1有效
);
表sys_user
另:SysUser实体类中,例如loginname字段均为小写的,如果表字段设计为login_name的话,生成的实体类此字段映射为loginName,就会大写。同理表名也是如此。
- 5.实际应用
生成后,可以将
5.1 接口类SysUserMapper.java、映射文件UserMapper.xml 两个文件删除了,用不到。 实体类SysUser.java复制粘贴到你要放实际放的地方就可以了。
5.2 实际的dao层接口类继承 Mapper<com.generator.model.SysUser> ,继承后,基础的增删改查操作mybatis已经自动生成并封装,不需要写了,这里只写其他业务逻辑方法就可以了。
注意:1).这里<com.generator.model.SysUser>为你生成实体类存放位置,如你在4.1步骤将SysUser.java实体类复制粘贴到其他位置,那么这里就写新位置的路径
2). 继承的Mapper为 tk.mybatis.mapper.common.Mapper。
在service实现类,就可以直接使用mybatis自动生成并封装的基础的增删改成操作,如下图。
Springboot+Mybatis的逆向工程的更多相关文章
- SpringBoot+Mybatis+Generator 逆向工程使用(二)
Mybatis-Genarator 逆向工程使用 个人开发环境 java环境:Jdk1.8.0_60 编译器:IntelliJ IDEA 2017.1.4 mysql驱动:mysql-connecto ...
- 项目转移时发生的错误<springboot+mybatis(xml逆向工程自动生成)>
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'app ...
- Java逆向工程SpringBoot + Mybatis Generator + MySQL
Java逆向工程SpringBoot+ Mybatis Generator + MySQL Meven pop.xml文件添加引用: <dependency> <groupId> ...
- SpringBoot+Mybatis+Maven+MySQL逆向工程实现增删改查
SpringBoot+Mybatis+MySQL+MAVEN逆向工程实现增删改查 这两天简单学习了下SpringBoot,发现这玩意配置起来是真的方便,相比于SpringMVC+Spring的配置简直 ...
- idea+springboot+mybatis逆向工程
前提:使用idea开发,基于springboot.用到了mybatis的逆向工程 因为之前用eclipse开发ssm比较多,现在转idea 使用springboot 踩了一些坑,在这记录一下~ 注意事 ...
- springboot实现mybaitis逆向工程
springboot实现mybaitis逆向工程 首先引入依赖,一共需要两个依赖(一个是mybaits依赖,一个是mybatis逆向工程插件) <dependency> <group ...
- 第五章 springboot + mybatis(转载)
本编博客转发自:http://www.cnblogs.com/java-zhao/p/5350021.html springboot集成了springJDBC与JPA,但是没有集成mybatis,所以 ...
- mybatis的逆向工程
mybatis的逆向工程是很大的减少了程序员对代码的编写工作,由于mybatis是半自动的sql语句使用,我们在项目中一般都是采用逆向工程来生成mybatis的文件,mapper接口相当于我们平常所说 ...
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
随机推荐
- K8s容器资源限制
在K8s中定义Pod中运行容器有两个维度的限制: 1. 资源需求:即运行Pod的节点必须满足运行Pod的最基本需求才能运行Pod. 如: Pod运行至少需要2G内存,1核CPU 2. 资源限额: ...
- [算法模版]Link-Cut-Tree
[算法模版]Link-Cut-Tree 博主懒本博客只对现有博客进行补充,先直接放隔壁链接. FlashHu-LCT总结 Menci-LCT学习笔记 make-root操作 make-root操作用于 ...
- 数据分析,R语言
数据结构 创建向量和矩阵 1 函数c(), length(), mode(), rbind(), cbind() 求平均值,和,连乘,最值,方差,标准差 1 函数mean(), sum(), min( ...
- 作业15-JDBC数据库编程
参考资料 本次作业参考文件 MySQL操作视频 数据库相关jar文件请参考QQ群文件. 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造 ...
- 避免因为Arcgis Server服务设置不当导致Oracle Process溢出的方法
我之前写过一篇文章<arcsoc进程无限增长导致oracle processes溢出>(见链接:https://www.cnblogs.com/6yuhang/p/9379086.html ...
- putty常用配置修改
1.修改putty默认的颜色方案 初次使用putty时,发现默认的配色的方案看得非常难受,特别是黑色背景,深蓝色的字体,根本看不清楚,下面介绍如何更改默认的配色方案: (1)下载配置文件 首先下载注册 ...
- CentOS下安装php 5.6.19
# php安装包下载wget https://www.php.net/distributions/php-5.6.19.tar.bz2# 解压bunzip2 php-5.6.19.tar.bz2tar ...
- AntDesign vue学习笔记(五)导航菜单动态加载
一般的后台系统都有一个树形导航菜单,具体实现如下,主要参考https://my.oschina.net/u/4131669/blog/3048416 "menuList": [ { ...
- 最细的eclipse 安装maven踩过的坑
Eclipse安装maven插件踩过的坑 在线安装maven eclipse安装maven插件,在网上有各种各样的方法,博主使用过的也不止一种,但是留下的印象总是时好时不好,同样的方法也不确定那一次能 ...
- 『7.5 NOIP模拟赛题解』
T1 Gift Description 人生赢家老王在网上认识了一个妹纸,然后妹纸的生日到了,为了表示自己的心 意,他决定送她礼物.可是她喜爱的东西特别多,然而他的钱数有限,因此他想 知道当他花一 ...