1. 背景
这篇文章刚开始想着哪个分类呢? mybatis,idea 或是 maven 呢, 最后还是选择了 mybatis. 最初使用这个逆向工具是在 eclipse 上, 使用的是 eclispe 上 mbg 插件执行配置 generatorConfig.xml 文件; 现在 idea 作为主力开发工具, 而网上基本上都是介绍使用 maven 的 mbg 插件, 而不是 idea 的插件执行 generatorConfig.xml. 本篇文章也是重复验证一下 idea 中 maven 使用 mybatis-generator 插件, 至于 idea 的插件, 最后试一下 (其实另一篇文章已经介绍了一种 easycode 插件).
2. 工程结构
3. 配置文件
generatorConfig.xml, 该文件是逆向工程核心文件, 该实验中需要配置在 pom.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>
- <classPathEntry location="/Users/tusm/config/self_lib/mysql-connector-java-5.1.38.jar"/>
- <context id="default" targetRuntime="MyBatis3">
- <commentGenerator>
- <property name="suppressDate" value="true"/>
- <property name="suppressAllComments" value="true"/>
- </commentGenerator>
- <jdbcConnection driverClass="com.mysql.jdbc.Driver"
- connectionURL="jdbc:mysql://192.168.3.32:3306/test"
- userId="root"
- password="123456">
- </jdbcConnection>
- <javaModelGenerator targetPackage="com.eran.bc.entity"
- targetProject="./src/main/java">
- <property name="trimStrings" value="true"/>
- </javaModelGenerator>
- <sqlMapGenerator targetPackage="com.eran.bc.dao.mapper"
- targetProject="./src/main/java">
- </sqlMapGenerator>
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="com.eran.bc.dao"
- targetProject="./src/main/java">
- </javaClientGenerator>
- <table tableName="employee" domainObjectName="Employee"
- enableCountByExample="false" enableUpdateByExample="false"
- enableDeleteByExample="false" enableSelectByExample="false"
- selectByExampleQueryId="false">
- </table>
- </context>
- </generatorConfiguration>
datasource.properties, 该文件为数据源配置文件;
- jdbc.databaseurl=jdbc:MySQL://192.168.3.32:3306/test
- jdbc.driver=com.MySQL.jdbc.Driver
- jdbc.username=root
- jdbc.password=123456
mybatis-config.xml, 该文件是 mybatis 框架工作核心文件;
<?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTDConfig3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <properties resource="config/datasource.properties"></properties>
- <typeAliases>
- <!-- 别名方式 1, 一个一个的配置 type 中放置的是类的全路径, alias 中放置的是类别名
- <typeAliasetype="com.mybatis.demo.Entity.User"alias="UserBean"/>-->
- <!-- 别名方式 2, 自动扫描, 将 JAVA 类的类名作为类的类别名 -->
- <!--<packagename="com.mybatis.demo.Entity"/>-->
- <typeAlias alias="Employee" type="com.eran.bc.entity.Employee"/>
- </typeAliases>
- <!-- 配置 mybatis 运行环境 -->
- <environments default="development">
- <environment id="development">
- <!--type="JDBC" 代表使用 JDBC 的提交和回滚来管理事务 -->
- <transactionManager type="JDBC"/>
- <!--mybatis 提供了 3 种数据源类型, 分别是: POOLED,UNPOOLED,JNDI-->
- <!--POOLED 表示支持 JDBC 数据源连接池 -->
- <!--UNPOOLED 表示不支持数据源连接池 -->
- <!--JNDI 表示支持外部数据源连接池 -->
- <dataSource type="POOLED">
- <property name="driver" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.databaseurl}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <!-- 映射文件方式 1, 一个一个的配置 -->
- <mapper resource="mapper/EmployeeMapper.xml"/> <!-- 放到之里就没事 -->
- <!-- <mapper resource="com/eran/bc/dao/mapper/EmployeeMapper.xml"/> 放到 dao/mapper 包里报错 BindingException, 路径呀还是要弄明白 -->
- <!-- 映射文件方式 2, 自动扫描包内的 Mapper 接口与配置文件 -->
- <!-- <package name="com/eran/bc/dao/mapper"/>-->
- </mappers>
- </configuration>
pom.xml, 其中使用了 mybatis-generator-maven-plugin 插件
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.eran.tools</groupId>
- <artifactId>blog-collection</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>war</packaging>
- <name>blog-collection Maven webapp</name>
- <!-- FIXME change it to the project's website -->
- <url>http://www.example.com</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <maven.compiler.source>1.7</maven.compiler.source>
- <maven.compiler.target>1.7</maven.compiler.target>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>3.4.6</version>
- </dependency>
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
- </dependencies>
- <build>
- <finalName>blog-collection</finalName>
- <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
- <plugins>
- <plugin>
- <artifactId>maven-clean-plugin</artifactId>
- <version>3.1.0</version>
- </plugin>
- <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
- <plugin>
- <artifactId>maven-resources-plugin</artifactId>
- <version>3.0.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.0</version>
- </plugin>
- <plugin>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.22.1</version>
- </plugin>
- <plugin>
- <artifactId>maven-war-plugin</artifactId>
- <version>3.2.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-install-plugin</artifactId>
- <version>2.5.2</version>
- </plugin>
- <plugin>
- <artifactId>maven-deploy-plugin</artifactId>
- <version>2.8.2</version>
- </plugin>
- <!-- mybatis-generator-maven-plugin 不能放到这里, 因为放了没有显示这个插件, 对这个 PluginManager 使用还是不清楚 -->
- </plugins>
- </pluginManagement>
- <plugins>
- <plugin>
- <groupId>org.mybatis.generator</groupId>
- <artifactId>mybatis-generator-maven-plugin</artifactId>
- <version>1.3.2</version>
- <configuration>
- <configurationFile>src/main/resources/config/generatorConfig.xml</configurationFile>
- <verbose>true</verbose>
- <overwrite>true</overwrite>
- </configuration>
- <executions>
- <execution>
- <id>Generate MyBatis Artifacts</id>
- <goals>
- <goal>generator</goal>
- </goals>
- </execution>
- </executions>
- <dependencies>
- <dependency>
- <groupId>MySQL</groupId>
- <artifactId>MySQL-connector-java</artifactId>
- <version>5.1.38</version>
- </dependency>
- </dependencies>
- </plugin>
- </plugins>
- </build>
- </project>
4. 测试文件
- package com.eran.bc.service;
- import com.eran.bc.dao.EmployeeMapper;
- import com.eran.bc.entity.Employee;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- import java.io.IOException;
- import java.io.Reader;
- public class EmployeeService {
- public static void main(String[] args) {
- /*
- 测试 mbg
- */
- try {
- Reader reader = Resources.getResourceAsReader("config/mybatis-config.xml");
- SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
- SqlSession session = sessionFactory.openSession();
- EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
- Employee employee = mapper.selectByPrimaryKey(1);
- System.out.println(employee.getName());
- session.commit();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
5. 使用 idea 插件逆向工程
其实上一篇博客 https://www.cnblogs.com/leeethan/p/12197424.html, 讲的 easy code 就是使用 idea 的插件配合 database 实现的逆向工程;
网上看到一个更好的插件 idea-mybatis-generator, 似乎更好, 但我懒得去试了, 这种东西基本差不多感觉. 有兴趣自己去看看 https://blog.csdn.net/qq_34208844/article/details/83819404
6. 参考
Intellij IDEA 中使用 MyBatis-generator 插件自动生成 MyBatis 代码
源文档 <https://blog.csdn.net/xiao1_1bing/article/details/81944348>
利用 mybatis-generator 自动生成代码
源文档 <https://www.cnblogs.com/deng-cc/p/9340748.html>
通过 idea-mybatis-generator 插件生成实体和 mapper
源文档 <https://blog.csdn.net/qq_34208844/article/details/83819404> 这个似乎是最简单的一个插件, 集 database+easycode 的功能
来源: http://www.bubuko.com/infodetail-3382719.html