基础模块mybatis逆向工程
使用mybatis逆向工程生成对应的bean,mappper
mybatis
文档地址:http://www.mybatis.org/mybatis-3
我们在mybatis-config.xml
下配置:
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 驼峰命名规则 -->
<setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
<typeAliases>
<package name="com.liantao.crud.bean"></package>
</typeAliases>
</configuration>
数据库
首先在mysql数据库ssm-crud
创建需要用到的数据表:
--员工表
CREATE TABLE tbl_emp
(
emp_id INT(11) UNSIGNED AUTO_INCREMENT,
emp_name VARCHAR(255) NOT NULL,
gender CHAR(3),
email VARCHAR(255),
PRIMARY KEY(emp_id)
)
-- 部门表
CREATE TABLE tbl_dept
(
dept_id INT(11) UNSIGNED AUTO_INCREMENT,
dept_name VARCHAR(255) NOT NULL,
PRIMARY KEY(dept_id)
)
--tbl_emp表添加d_id列,属性有int unsigned
alter table tbl_emp add column d_id int UNSIGNED
--为tbl_emp添加外键
alter table tbl_emp add foreign key(d_id) references tbl_dept(dept_id)
如代码中的注释所说,我们建立了两个数据表,并且为他们建立了外键联系。
添加MyBatis Generator依赖
MyBatis Generator介绍
在maven
仓库搜索:mybatis generator,得到地址
在ssm-crud
项目pom.xml
文件的<dependencies></dependencies>
标签内添加:
<!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
mbg配置文件
MyBatis GeneratorXML配置文件参考
在ssm-crund
项目根目录创建mbg.xml
文件,在地址中复制示例代码
删除<classPathEntry location="/Program Files/IBM/SQLLIB/java/db2java.zip" />
然后添加如下:
<?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="DB2Tables" targetRuntime="MyBatis3">
<!-- 让生成的bean,dao等文件没有注释-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!-- 配置数据库连接 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ssm_crud"
userId="root"
password="admin">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 指定javaBean生成的位置 -->
<javaModelGenerator
targetPackage="com.liantao.crud.bean"
targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 指定sql映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject=".srcmain
esources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 指定dao接口生成的位置,mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.liantao.crud.dao" targetProject=".srcmainjava">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 指定每个表的生成策略 -->
<table tableName="tbl_emp" domainObjectName="Employee"></table>
<table tableName="tbl_dept" domainObjectName="Department"></table>
</context>
</generatorConfiguration>
创建测试类,MGBTest.java
,参考地址
复制粘贴代码,后引入对应的包,如下:
package com.liantao.crud.test;
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 MGBTest {
public static void main(String[] args) throws Exception {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
File configFile = new File("mbg.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);
}
}
运行,刷新项目,可以看到:
END