什么是逆向工程?
在学习的过程中会发现, 需要我们写大量的 sql 语句
此时 mybaatis 官方为我们提供逆向工程可以针对单表自动生成的 mybatis 执行所需要的代码
使用方法:
- MyBatis Generator (MBG) can be run in the following ways:
- From the command prompt with an xml configuration
- As an Ant task with an xml configuration
- As a Maven Plugin
- From another Java program with an xml configuration
- From another Java program with a Java based configuration
推荐使用红色的方法
jar 包
官方文档在解压后的 doc 文件中点击 index.html
简单的讲解一下:
MyBatis GeneratorXML Configuration File Reference
建立新的工程:
导入需要加入的 jar
在 config 文件下的
gengeratorConfig.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:3307/shopping"
- userId="root"
- password="1234">
- </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.MrChengs.po"
- targetProject=".\src">
- <!-- enableSubPackages: 是否让 schema 作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- <!-- 从数据库返回的值被清理前后的空格 -->
- <property name="trimStrings" value="true" />
- </javaModelGenerator>
- <!-- targetProject:mapper 映射文件生成的位置 -->
- <sqlMapGenerator targetPackage="com.MrChengs.mapper"
- targetProject=".\src">
- <!-- enableSubPackages: 是否让 schema 作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </sqlMapGenerator>
- <!-- targetPackage:mapper 接口生成的位置 -->
- <javaClientGenerator type="XMLMAPPER"
- targetPackage="com.MrChengs.mapper"
- targetProject=".\src">
- <!-- enableSubPackages: 是否让 schema 作为包的后缀 -->
- <property name="enableSubPackages" value="false" />
- </javaClientGenerator>
- <!-- 指定数据库表 -->
- <table tableName="items"></table>
- <table tableName="orders"></table>
- <table tableName="orderdetail"></table>
- <table tableName="user"></table>
- </context>
- </generatorConfiguration>
test 目录下
GeneratorSqlmap.java
- public class GeneratorSqlmap {
- public void generator() throws Exception{
- List<String> warnings = new ArrayList<String>();
- boolean overwrite = true;
- // 指定 逆向工程配置文件
- File configFile = new File("config/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();
- }
- }
- }
都是固定的格式, 固体的可以参考官方的文档.
然后运行在
简单说明一下
如下的注释
首先是表
其次是表中的字段
- 2018-10-25 16:04:22,837 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "items"
- 2018-10-25 16:04:22,848 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..items"
- 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "name", data type 12, in table "shopping..items"
- 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "price", data type 7, in table "shopping..items"
- 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "detail", data type -1, in table "shopping..items"
- 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "pic", data type 12, in table "shopping..items"
- 2018-10-25 16:04:22,849 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..items"
- 2018-10-25 16:04:22,855 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orders"
- 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orders"
- 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "user_id", data type 4, in table "shopping..orders"
- 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "number", data type 12, in table "shopping..orders"
- 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "createtime", data type 93, in table "shopping..orders"
- 2018-10-25 16:04:22,859 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "note", data type 12, in table "shopping..orders"
- 2018-10-25 16:04:22,864 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "orderdetail"
- 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..orderdetail"
- 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "orders_id", data type 4, in table "shopping..orderdetail"
- 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_id", data type 4, in table "shopping..orderdetail"
- 2018-10-25 16:04:22,868 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "items_num", data type 4, in table "shopping..orderdetail"
- 2018-10-25 16:04:22,869 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Retrieving column information for table "user"
- 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "id", data type 4, in table "shopping..user"
- 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "username", data type 12, in table "shopping..user"
- 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "birthday", data type 91, in table "shopping..user"
- 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "sex", data type 1, in table "shopping..user"
- 2018-10-25 16:04:22,874 [main] DEBUG [org.mybatis.generator.internal.db.DatabaseIntrospector] - Found column "address", data type 12, in table "shopping..user"
刷新观察
- com.MrChengs.po
- com.MrCehngs.mapper
两个包里面的文件
此时的文件我们得到了, 在使用的时候我们不要进行对原有的代码的修改.
返回上一个博文将进行简单的小案例测试:
有不懂的可以参考: MyBatis 整合 Spring
复制图中的文件到本工程中
新建测试类
- public class test {
- private ApplicationContext applicationContext;
- public ApplicationContext getApplication(){
- applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
- return applicationContext;
- }
- // 根据主键 id 进行查询
- @Test
- public void test() {
- ItemsMapper items = (ItemsMapper) getApplication().getBean("itemsMapper");
- Items it = items.selectByPrimaryKey(1);
- System.out.println(it.toString());
- }
- // 自定义查询条件
- @Test
- public void test1() {
- ItemsMapper itemsMapperems = (ItemsMapper) getApplication().getBean("itemsMapper");
- ItemsExample itemsExample = new ItemsExample();
- // 通过 criteria 构造条件查询
- ItemsExample.Criteria criteria = itemsExample.createCriteria();
- criteria.andNameEqualTo("笔记本");
- // 可能返回多个记录
- List<Items> items = itemsMapperems.selectByExample(itemsExample);
- System.out.println(items);
- }
- }
均可以测试成功!!
对于其他的方法, 在使用的时候, 可以参考自动生成的源码进行测试, 都是可以测试成功的!
逆向工程的使用极大的方便我们进行开发, 作为程序员不需要过多的关注这个, 我们进行了解基本的使用和用法即可.
其余的增删改除, 可以进行测试, 在不会使用的情况下, 可以参考源码, 源码是之前的前几章节的内容.
来源: https://www.cnblogs.com/Mrchengs/p/9851117.html