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>generate</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插件逆向工程
1.其实上一篇博客https://www.cnblogs.com/leeethan/p/12197424.html,讲的easy code就是使用idea的插件配合database实现的逆向工程;
2.网上看到一个更好的插件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的功能
7.注意事项
1.在mybatis-config.xml中的<mapper resource="com/eran/bc/dao/mapper/EmployeeMapper.xml"/>这么配置的话,会出现找不到该xml的报错。后来找到原因了。【解决办法】maven默认是不编译src/main/java中的xml文件,需要在maven->build标签中加入
<build>
<resources>
<!-- resources文件 -->
<resource>
<directory>src/main/java</directory>
<!-- 引入映射文件等 -->
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
解决链接:https://blog.csdn.net/chenmingxu438521/article/details/86768408