Springboot的Mybatis逆向工程
1、pom.xml添加mybatis和逆向插件依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- mybatis-generator-core 反向生成java代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
2、application.properties配置数据库连接信息:
#mysql数据配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=test
spring.datasource.password=test
3、写mybatis-generator.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">
<!-- mybatis逆向生成xml配置 -->
<generatorConfiguration>
<properties resource="application.properties" /> <!-- 数据库连接配置文件 -->
<context id="sqlserverTables" targetRuntime="MyBatis3">
<!-- 生成的pojo,将implements Serializable-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 数据库链接URL、用户名、密码 -->
<jdbcConnection driverClass="${spring.datasource.driverClassName}"
connectionURL="${spring.datasource.url}"
userId="${spring.datasource.username}"
password="${spring.datasource.password}">
</jdbcConnection>
<!--
默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--
生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,
也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下
-->
<!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->
<javaModelGenerator targetPackage="com.example.demo.entity" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--对应的mapper.xml文件 -->
<sqlMapGenerator targetPackage="config/xml" targetProject="./src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 对应的Mapper接口类文件 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->
<table tableName="user_info" domainObjectName="UserInfo"
enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" selectByExampleQueryId="false" >
<property name="useActualColumnNames" value="false"/>
</table>
</context>
</generatorConfiguration>
4、编写GenMain.java类:
package com.example.demo.CodeGenerator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.util.ResourceUtils;
/**************************************
* 类说明:
* mybatis逆向工程main函数
***************************************
*/
public class GenMain {
public static void main(String[] args) throws FileNotFoundException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//如果这里出现空指针,直接写绝对路径即可。
String genCfg = "E:\\cloud\\demo\\src\\test\\resources\\config\\mybatis-generator.xml";
// File configFile = new File(GenMain.class.getResource(genCfg).getFile()); //获取路径出错
File configFile = ResourceUtils.getFile(genCfg);
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = null;
try {
config = cp.parseConfiguration(configFile);
} catch (IOException e) {
e.printStackTrace();
} catch (XMLParserException e) {
e.printStackTrace();
}
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = null;
try {
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
try {
myBatisGenerator.generate(null);
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
5、运行GenMain.java类的main方法,刷新项目即可。
Springboot的Mybatis逆向工程的更多相关文章
- springboot中mybatis逆向工程与分页的应用
最近在项目中应用到springboot与mybatis,在进行整合过程中遇到一些坑,在此将其整理出来,便于以后查阅与复习. 项目运行环境为:eclispe+jdk1.8+maven 一.搭建Sprin ...
- SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper
一.添加所需依赖,当前完整的pom文件如下: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&qu ...
- springboot集成mybatis(逆向工程),热部署以及整合Swagger2
本文是作者原创,版权归作者所有.若要转载,请注明出处. springboot集成mybatis和mybatis-generator插件 1.新建Springboot项目(略) 2.导入相关依赖 < ...
- SpringBoot 之 Mybatis 逆向工程
今天给大家介绍在 spring- boot 项目中如何使用 maven 插件逆向工程生成 Mybatis 代码. pom.xml 添加依赖和插件 <dependency> <grou ...
- idea+springboot+mybatis逆向工程
前提:使用idea开发,基于springboot.用到了mybatis的逆向工程 因为之前用eclipse开发ssm比较多,现在转idea 使用springboot 踩了一些坑,在这记录一下~ 注意事 ...
- SpringBoot (四) - 整合Mybatis,逆向工程,JPA
1.SpringBoot整合MyBatis 1.1 application.yml # 数据源配置 spring: datasource: driver-class-name: com.mysql.c ...
- springboot整合mybatis增删改查(三):mybatis逆向工程
上一篇已经把项目基本框架完善,接下来就是利用Mybatis Generator逆向工程进行mybatis的整合. 我们在创建项目开始的时候已经勾选web,mybatis,sql等,但是这些依赖还是不够 ...
- SpringBoot系列——MyBatis整合
前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...
- mybatis逆向工程(MyBatis Generator)
mybatis逆向工程(MyBatis Generator) 1. 什么是mybatis逆向工程 mybatis官方为了提高开发效率,提高自动对单表生成sql,包括 :mapper.xml.mappe ...
随机推荐
- 爬取拉勾网所有python职位并保存到excel表格 对象方式
# 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...
- 将QTP运行时的错误截图上传到QC
Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed the ...
- C++中函数模板的深入理解
1,函数模板深入理解: 1,编译器从函数模板通过具体类型产生不同的函数: 1,模板就是模子,通过这个模子可以产生很多的实物: 2,函数模板就是编译器用来产生具体函数的模子: 2,编译器会对函数模板进行 ...
- JavaWeb的几种设计模式
原文:http://blog.csdn.net/yue7603835/article/details/7479855 Java Web开发方案有多种可供选择,这里列举一些经典的开发模式进行横向比较,为 ...
- Dagger2 探索记2——四大基本组件(二)
书接上文,先回顾以下前一章写的内容. 内容大概就是在Activity中用@Inject标记一个注入的类,然后在这个类的构造函数上也打个@Inject标记,然后使用@Component来连接两边,完成对 ...
- scroll兼容性
document.html=>document.documentElement function scroll() { if(window.pageYOffset != null) // ie9 ...
- 如何判断一个List集合中是否有空值
list.Any(v => string.IsNullOrWhiteSpace(v))
- 2018-2-13-win10-UWP-显示地图
title author date CreateTime categories win10 UWP 显示地图 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17: ...
- Linux用户的基本操作3 (组的基本管理,用户提权)
目录 组的基本原理 用户身份切换 5.用户身份提权 组的基本原理 组账户信息保存在/etc/group 和/etc/gshadow 两个文件中. /etc/group组账户信息 [root@zls ~ ...
- 转载:jQuery的deferred对象详解
一.什么是deferred对象? 开发网站的过程中,我们经常遇到某些耗时很长的javascript操作.其中,既有异步的操作(比如ajax读取服务器数据),也有同步的操作(比如遍历一个大型数组),它们 ...