文章出处http://m.blog.csdn.net/dreamer_8399/article/details/77186710
1、在pom.xml的project>build里面添加如下代码,让maven环境支持mybatis-generator组件
<pluginManagement><plugins><plugin><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.2</version><configuration><configurationFile>src/main/resources/generator.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>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.2</version></dependency></dependencies></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.19.1</version><configuration><skipTests>true</skipTests></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.0.1</version><configuration><encoding>UTF-8</encoding></configuration></plugin></plugins></pluginManagement>
注:如果在dependencies中已经引入mysql-connector-java则不需加入以下dependency,反之加入
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.35</version> <scope>runtime</scope> </dependency>
2、generator.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="E:\apache-maven-3.3.9\repo\mysql\mysql-connector-java\5.1.18\mysql-connector-java-5.1.18.jar" /> <context id="Tables" targetRuntime="MyBatis3"><commentGenerator><!-- 是否去除自动生成的注释 true:是 : false:否 --><property name="suppressAllComments" value="true" /></commentGenerator><!-- 数据库链接URL、用户名、密码 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://192.168.100.52:3306/dev_test" userId="数据库用户名"password="数据库密码"><!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1521:orcl" userId="msa" password="msa"> --></jdbcConnection><javaTypeResolver><!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal --><property name="forceBigDecimals" value="true" /></javaTypeResolver><!-- 生成实体类的包名和位置,这里配置将生成的实体类放在com.loan.test.entity这个包下 --><javaModelGenerator targetPackage="com.loan.test.entity"targetProject=".\src\main\java\"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="true" /><!-- 从数据库返回的值被清理前后的空格 --><property name="trimStrings" value="true" /></javaModelGenerator><!-- 生成的SQL映射文件包名和位置,这里配置将生成的SQL映射文件放在com.loan.test.dao.xml这个包下 --><sqlMapGenerator targetPackage="com.loan.test.dao.xml"targetProject=".\src\main\java\"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 生成DAO的包名和位置,这里配置将生成的dao类放在com.loan.test.dao.mapper这个包下 --><javaClientGenerator type="XMLMAPPER"targetPackage="com.loan.test.dao.mapper" targetProject=".\src\main\java\"><!-- enableSubPackages:是否让schema作为包的后缀 --><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 要生成那些表(更改tableName和domainObjectName就可以) --><table tableName="test" domainObjectName="Test"enableCountByExample="false" enableUpdateByExample="false"enableDeleteByExample="false" enableSelectByExample="false"selectByExampleQueryId="false" /></context></generatorConfiguration>
3、运用maven指令生成逆向工程
项目右键->run as->maven build...,Goals:中输入mybatis-generator:generate
.
4、刷新项目,结果
1)Test.java
package com.loan.test.entity;import java.math.BigDecimal;public class Test { private Integer id; private String userName; private Integer cardId; private BigDecimal moneyAmount; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } public Integer getCardId() { return cardId; } public void setCardId(Integer cardId) { this.cardId = cardId; } public BigDecimal getMoneyAmount() { return moneyAmount; } public void setMoneyAmount(BigDecimal moneyAmount) { this.moneyAmount = moneyAmount; }}
2)TestMapper.java
package com.loan.test.dao.mapper;import com.loan.test.entity.Test;public interface TestMapper { int deleteByPrimaryKey(Integer id); int insert(Test record); int insertSelective(Test record); Test selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Test record); int updateByPrimaryKey(Test record);}
3)TestMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.loan.test.dao.mapper.TestMapper"> <resultMap id="BaseResultMap" type="com.loan.test.entity.Test"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="card_id" jdbcType="INTEGER" property="cardId" /> <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" /> </resultMap> <sql id="Base_Column_List"> id, user_name, card_id, money_amount </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from test where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from test where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.loan.test.entity.Test"> insert into test (id, user_name, card_id, money_amount) values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER}, #{moneyAmount,jdbcType=DECIMAL}) </insert> <insert id="insertSelective" parameterType="com.loan.test.entity.Test"> insert into test <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="userName != null"> user_name, </if> <if test="cardId != null"> card_id, </if> <if test="moneyAmount != null"> money_amount, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="userName != null"> #{userName,jdbcType=VARCHAR}, </if> <if test="cardId != null"> #{cardId,jdbcType=INTEGER}, </if> <if test="moneyAmount != null"> #{moneyAmount,jdbcType=DECIMAL}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test"> update test <set> <if test="userName != null"> user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="cardId != null"> card_id = #{cardId,jdbcType=INTEGER}, </if> <if test="moneyAmount != null"> money_amount = #{moneyAmount,jdbcType=DECIMAL}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test"> update test set user_name = #{userName,jdbcType=VARCHAR}, card_id = #{cardId,jdbcType=INTEGER}, money_amount = #{moneyAmount,jdbcType=DECIMAL} where id = #{id,jdbcType=INTEGER} </update> <resultMap id="BaseResultMap" type="com.loan.test.entity.Test"> <id column="id" jdbcType="INTEGER" property="id" /> <result column="user_name" jdbcType="VARCHAR" property="userName" /> <result column="card_id" jdbcType="INTEGER" property="cardId" /> <result column="money_amount" jdbcType="DECIMAL" property="moneyAmount" /> </resultMap> <sql id="Base_Column_List"> id, user_name, card_id, money_amount </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from test where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from test where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.loan.test.entity.Test"> insert into test (id, user_name, card_id, money_amount) values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{cardId,jdbcType=INTEGER}, #{moneyAmount,jdbcType=DECIMAL}) </insert> <insert id="insertSelective" parameterType="com.loan.test.entity.Test"> insert into test <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> id, </if> <if test="userName != null"> user_name, </if> <if test="cardId != null"> card_id, </if> <if test="moneyAmount != null"> money_amount, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="userName != null"> #{userName,jdbcType=VARCHAR}, </if> <if test="cardId != null"> #{cardId,jdbcType=INTEGER}, </if> <if test="moneyAmount != null"> #{moneyAmount,jdbcType=DECIMAL}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.loan.test.entity.Test"> update test <set> <if test="userName != null"> user_name = #{userName,jdbcType=VARCHAR}, </if> <if test="cardId != null"> card_id = #{cardId,jdbcType=INTEGER}, </if> <if test="moneyAmount != null"> money_amount = #{moneyAmount,jdbcType=DECIMAL}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.loan.test.entity.Test"> update test set user_name = #{userName,jdbcType=VARCHAR}, card_id = #{cardId,jdbcType=INTEGER}, money_amount = #{moneyAmount,jdbcType=DECIMAL} where id = #{id,jdbcType=INTEGER} </update></mapper>
配置完成!!