通用mybatis mapper程式碼生成工具
阿新 • • 發佈:2019-01-10
以下介紹的是針對通用tk mybatis 程式碼生成方法
1.依賴引入
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--自動程式碼生成--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--指定生成檔案配置--> <configurationFile>${basedir}/src/main/resources/mybatis-generator-config.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.2</version> </dependency> </dependencies> </plugin> </plugins> </build>
2.mybatis-generator-config.xml
該檔案放在靜態資源目錄resources下(同application.properties目錄)
<?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"> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <!--生成的mapper.java繼承的類--> <property name="mappers" value="com.test.layui.base.mapper.BaseMapper"/> <!-- caseSensitive預設false,當資料庫表名區分大小寫時,可以將該屬性設定為true --> <property name="caseSensitive" value="true"/> </plugin> <!--資料庫配置--> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://ip:port/database" userId="root" password="123"> </jdbcConnection> <!--實體類路徑--> <javaModelGenerator targetPackage="com.test.layui.model" targetProject="E:\saas\layui\src\main\java"/> <!--mapper.xml生成的路徑:resources/mapper下--> <sqlMapGenerator targetPackage="mapper" targetProject="E:\saas\layui\src\main\resources"/> <!--mapper.java生成的路徑--> <javaClientGenerator targetPackage="com.test.layui.mapper" targetProject="E:\saas\layui\src\main\java" type="XMLMAPPER" /> <!--表結構--> <table tableName="test_person" > <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> <table tableName="test_student" > <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
3.執行
在idea中點選mybatis-generator:generate即可,
結果會按照預設駝峰命名規則生成TestStudent.java TestPerson.java TestStudentMapper.java TestPersonMapper.java TestStudentMapper.xml TestPersonMapper.xml
(1)model package com.test.layui.model; import javax.persistence.*; @Table(name = "test_student") public class TestStudent { @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "USER_NAME") private String userName; /** * @return ID */ public Long getId() { return id; } /** * @param id */ public void setId(Long id) { this.id = id; } /** * @return USER_NAME */ public String getUserName() { return userName; } /** * @param userName */ public void setUserName(String userName) { this.userName = userName; } } (2)mapper.java public interface TestStudentMapper extends BaseMapper<TestStudent> { } (3)mapper.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.test.layui.mapper.TestStudentMapper"> <resultMap id="BaseResultMap" type="com.test.layui.model.TestStudent"> <id column="ID" jdbcType="BIGINT" property="id" /> <result column="USER_NAME" jdbcType="VARCHAR" property="userName" /> </resultMap> </mapper>