ssm+mybatis自动生成器

使用ssm框架+springboot+maven+mysql8+mybatis自动生成工具(generatorsqlmapcustom)+eclipse

参考:MyBatis框架generatorSqlmapCustom自动生成及下载方法

            Java读取resource文件/路径的几种方式

            MyBatis Generator 生成器把其他数据库的同名表生成下来的问题

首先,建表(mysql)

CREATE DATABASE `generator_ssmdb`CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(20) DEFAULT NULL,
  `password` varchar(20) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

第一步:新建Spring Starter Project工程

                ssm+mybatis自动生成器

                ssm+mybatis自动生成器

第二步:增加依赖 pom.xml

<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
   	<groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>
<!-- https://github.com/mybatis/generator/releases -->
<dependency>
   	<groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
</dependency>

第三步:GeneratorSqlmap.java

package com.example.demo.generator;

import java.io.File;
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.internal.DefaultShellCallback;

public class GeneratorSqlmap {

	public void generator() throws Exception {

		List<String> warnings = new ArrayList<String>();
		boolean overwrite = true;
		// 指定 ****配置文件
		File configFile = new File(this.getClass().getClassLoader().getResource("generatorConfig.xml").getPath());
//		File configFile = new File("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);

	}

	public static void main(String[] args) throws Exception {
		try {
			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
			generatorSqlmap.generator();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

第四步:配置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>
	<context id="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/generator_ssmdb" userId="root"
			password="root">		
			<!--MySQL 8.x 需要指定服务器的时区-->
			<property name="serverTimezone" value="UTC"/>
			<!--MySQL 不支持 schema 或者 catalog 所以需要添加这个-->
			<!--参考 : http://www.mybatis.org/generator/usage/mysql.html-->
			<property name="nullCatalogMeansCurrent" value="true"/>
		</jdbcConnection>
		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 
			和 NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!-- targetProject:生成PO类的位置 -->
		<javaModelGenerator
			targetPackage="com.example.demo.bean" targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.example.demo.mapper"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.example.demo.dao" targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		<!-- 指定数据库表 tableName 有几张表 就写几张表的表名 -->
		<table schema="mybatis" tableName="user"></table>

	</context>
</generatorConfiguration>

第五步:Run as 注意执行GeneratorSqlmap.java的main()方法

ssm+mybatis自动生成器

掉坑总结:

        1.确定jar包全部正确:

                    mysql-connector-java-8.0.15-sources.jar

                    mybatis-generator-core-1.3.7-sources.jar

                    mybatis-generator-maven-plugin-1.3.7-sources.jar

        2.路径问题:

                     我的xml文件放在了resource包下(/generator_ssm/src/main/resources/generatorConfig.xml)

                     注意修改读取generatorConfig.xml的路径,根据具体需要进行修改。

        3.自动生成工具generator扫描到了其他数据库的同名表:(重点)

                     修改配置文件generatorConfig.xml中的jdbcConnection标签

<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/generator_ssmdb" userId="root" password="root">
			<!--MySQL 8.x 需要指定服务器的时区-->
			<property name="serverTimezone" value="UTC"/>
			<!--MySQL 不支持 schema 或者 catalog 所以需要添加这个-->
			<!--参考 : http://www.mybatis.org/generator/usage/mysql.html-->
			<property name="nullCatalogMeansCurrent" value="true"/>

		</jdbcConnection>

        4.生成之后,需要将bean、dao、mapper三个文件夹由src\com\example\demo下拖到src\main\java\com\example\demo下,然后删除下面src下的com包(红框选中)就完成了。根据具体需求操作。

ssm+mybatis自动生成器

相关资料拓展:Mybatis ****的三种方法

        mybatis中关于example类详解