描述: mybatis generator 的作用是根据数据库自动生成 实体类、Dao接口、Mapper 映射文件。
数据库:
create table product(
id int primary key AUTO_INCREMENT,
pname varchar(20),
price double,
category_id varchar(32)
);
INSERT INTO product(pname,price,category_id) VALUES('联想',5000,'电脑');
INSERT INTO product(pname,price,category_id) VALUES('海尔',3000,'洗衣机');
INSERT INTO product(pname,price,category_id) VALUES('雷神',5000,'笔记本');
INSERT INTO product(pname,price,category_id) VALUES('JACK JONES',800,'服饰');
INSERT INTO product(pname,price,category_id) VALUES('真维斯',200,'服饰');
INSERT INTO product(pname,price,category_id) VALUES('花花公子',440,'服饰');
INSERT INTO product(pname,price,category_id) VALUES('劲霸',2000,'服饰');
INSERT INTO product(pname,price,category_id) VALUES('香奈儿',800,'奢侈品');
INSERT INTO product(pname,price,category_id) VALUES('相宜本草',200,'亲民品');
INSERT INTO product(pname,price,category_id) VALUES('面霸',5,'垃圾');
INSERT INTO product(pname,price,category_id) VALUES('好想你枣',56,'零食');
INSERT INTO product(pname,price,category_id) VALUES('香飘飘奶茶',1,'毒药');
INSERT INTO product(pname,price,category_id) VALUES('果9',1,NULL);
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/mybatis/generator/generatorConfig.xml</configurationFile>
<!-- 设置覆盖原有的文件:第二次执行的时候 -->
<overwrite>true</overwrite>
<!-- 输出一些日志信息-->
<verbose>true</verbose>
</configuration>
<dependencies>
<!-- maven 插件使用依赖包要独立设置 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
</dependencies>
</plugin>
<?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="MySQLTables" targetRuntime="MyBatis3">
<!-- 虽然插件中配置了<overwrite>true</overwrite>,但是只对java 文件有效,对mapper文件无效.特定使用 UnmergeableXmlMappersPlugin 才可以覆盖xml文件 -->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!--关闭注释的生成-->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--覆盖生成XML文件-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mybatisDemo"
userId="root"
password="root">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 实体类-->
<javaModelGenerator targetPackage="net.suncaper.demo1.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--Mapper 映射文件-->
<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--Mapper 到接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="net.suncaper.demo1.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 对应的表生成对应的 实体类 -->
<table tableName="product" domainObjectName="Product"/>
<!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
<property name="useActualColumnNames" value="true"/>
<generatedKey column="ID" sqlStatement="DB2" identity="true" />
<columnOverride column="DATE_FIELD" property="startDate" />
<ignoreColumn column="FRED" />
<columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
</table>-->
</context>
</generatorConfiguration>
<select id="selectAllUser" resultMap="userMap">
select * from `user`
</select>
List<User> users = userMapper.selectAllUser();
for (User user : users) {
System.out.println(user);
}