之前在生成代碼的時候,有很是多的沒必要要的註釋,註解等,最近抽時間研究了一下,弄了一個簡便的反向生成,也可能有不對的地方,但願各位指正。下面附上代碼:java
generatorConfig.xmlmysql
<?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="mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <property name="javaFileEncoding" value="UTF-8"/> <!-- mybatis的插件 --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/> </plugin> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <!--數據庫鏈接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/bendi?useUnicode=true&characterEncoding=utf8&useSSL=false&requireSSL=false&verifyServerCertificate=false" userId="jeecg" password="jeecg"> </jdbcConnection> <!-- model路徑 --> <javaModelGenerator targetPackage="com.work.model" targetProject="src/main/java"/> <!-- mapper路徑 --> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/> <!-- xml文件路徑 --> <javaClientGenerator targetPackage="com.work.mapper" targetProject="src/main/java" type="XMLMAPPER"/> <!-- 表名 --> <table tableName="user_t"/> </context> </generatorConfiguration>
在pom.xml文件中加入所需的插件sql
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.6</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.3.8</version> </dependency> </dependencies> </plugin> </plugins> </build>
生成的model數據庫
在model中註解和表名都有了,這裏面的註釋和表字段的註釋相關聯,表字段註釋是什麼,屬性上的註釋就是什麼mybatis
package com.work.model; import java.io.Serializable; import javax.persistence.*; @Table(name = "user_t") public class UserT implements Serializable { private static final long serialVersionUID = 275281505124996746L; @Id @Column(name = "ID") private Integer id; @Column(name = "USER_NAME") private String userName; @Column(name = "PASSWORD") private String password; @Column(name = "AGE") private Integer age; /** * @return ID */ public Integer getId() { return id; } /** * @param id */ public void setId(Integer id) { this.id = id; } /** * @return USER_NAME */ public String getUserName() { return userName; } /** * @param userName */ public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim(); } /** * @return PASSWORD */ public String getPassword() { return password; } /** * @param password */ public void setPassword(String password) { this.password = password == null ? null : password.trim(); } /** * @return AGE */ public Integer getAge() { return age; } /** * @param age */ public void setAge(Integer age) { this.age = age; } }
生成的mapperapp
package com.work.mapper; import com.work.model.UserT; import tk.mybatis.mapper.common.Mapper; public interface UserTMapper extends Mapper<UserT> { }
生成的mapper xml文件maven
<?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.work.mapper.UserTMapper"> <resultMap id="BaseResultMap" type="com.work.model.UserT"> <!-- WARNING - @mbggenerated --> <id column="ID" jdbcType="INTEGER" property="id" /> <result column="USER_NAME" jdbcType="VARCHAR" property="userName" /> <result column="PASSWORD" jdbcType="VARCHAR" property="password" /> <result column="AGE" jdbcType="INTEGER" property="age" /> </resultMap> </mapper>
省去了不少沒必要要的註釋而且看着比較簡練。本人工做也沒多長時間,還有不少不足,個位不喜勿噴ui