[mybatis]逆向工程MGB基本编写
Posted 唐火
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[mybatis]逆向工程MGB基本编写相关的知识,希望对你有一定的参考价值。
逆向工程
-
项目结构
-
依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
- mgb.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>
<!-- targetRuntime创建一个myBatis容器 有MyBatis3Simple(简单版的CURD)和MyBatis3带有动态sql -->
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!-- 指定如何连接目标数据库 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"
userId="root"
password="8888.216">
</jdbcConnection>
<!-- 类型解析器 是否强制转换forceBigDecimals -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--javaModelGenerator:指定javaBean的生产策略-->
<!-- 定义java模型属性 形成javabean targetPackage目标包-->
<!-- targetProject 目标工程-->
<javaModelGenerator targetPackage="com.atguigu.mybatis.bean" targetProject=".\\src">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 定义sql映射 -->
<!--sqlMapGenerator sql映射生产策略-->
<sqlMapGenerator targetPackage="com.atguigu.mybatis.dao"
targetProject=".\\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- javaClientGenerator 指定Mapper的接口所在的位置 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.atguigu.mybatis.dao" targetProject=".\\src">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--table 指定逆向分析那些表,根据表创建javabean(Departmanet) -->
<table tableName="tb1_dept" domainObjectName="Department"></table>
<table tableName="tb1_employee" domainObjectName="Employee"></table>
</context>
</generatorConfiguration>
- java
@Test
public void mybatisGeneratorTest() throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
ConfigurationParser cp = new ConfigurationParser(warnings);
Reader reader = Resources.getResourceAsReader("mgb.xml");
Configuration config = cp.parseConfiguration(reader);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
以上是关于[mybatis]逆向工程MGB基本编写的主要内容,如果未能解决你的问题,请参考以下文章