MyBatis Generator是一款mybatis自动代码生成工具,可以通过配置后自动生成文件。
MyBatis Generator有几种方法可以生成代码,下面是其中一种。
一、官网下载 MyBatis Generator
1、下载地址:https://github.com/mybatis/generator/releases
目前最新版本是1.3.7。
在命令行下自动创建代码只需要下载mybatis-generator-core-1.3.7.zip即可。
2、压缩包解压后,主要是lib目录,进去只有三个jar文件
二、创建配置文件等
1、在lib目录里面创建配置文件generatorConfig.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> <!--MySQL连接驱动--> <classPathEntry location="mysql-connector-java-5.1.37.jar" /> <!--数据库链接URL,用户名、密码 --> <context id="MySQL" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <!-- 是否去除自动生成的注释 true:是 : false:否 --> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password=""> </jdbcConnection> <!--是否启用java.math.BigDecimal--> <javaTypeResolver > <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成模型的包名和位置--> <javaModelGenerator targetPackage="test.model" targetProject="src"> <property name="enableSubPackages" value="true" /> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成映射文件的包名和位置--> <sqlMapGenerator targetPackage="test.xml" targetProject="src"> <property name="enableSubPackages" value="true" /> </sqlMapGenerator> <!-- 生成DAO的包名和位置--> <javaClientGenerator type="XMLMAPPER" targetPackage="test.dao" targetProject="src"> <property name="enableSubPackages" value="true" /> </javaClientGenerator> <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名--> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
2、因为的是mysql,所以在lib目录里面还需要放入mysql驱动包,如mysql-connector-java-5.1.37.jar
3、配置文件指定输出目录为src(targetProject="src"),所以需要在lib目录手动创建src目录,最终文件如下:
三、命令行下生成代码
1、在cmd命令行定位到D:projectsmybatis-generator-core-1.3.7lib
2、输入命令java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite
正常情况下会提示操作成功。
3、查看src目录,已经生成3个子目录,并且相应文件也在里面
4、UserMapper.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="test.dao.UserMapper"> <resultMap id="BaseResultMap" type="test.model.User"> <id column="Id" jdbcType="INTEGER" property="id" /> <result column="Name" jdbcType="VARCHAR" property="name" /> <result column="Sex" jdbcType="VARCHAR" property="sex" /> <result column="Age" jdbcType="TINYINT" property="age" /> </resultMap> <sql id="Base_Column_List"> Id, Name, Sex, Age </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where Id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where Id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="test.model.User"> insert into user (Id, Name, Sex, Age) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR}, #{age,jdbcType=TINYINT}) </insert> <insert id="insertSelective" parameterType="test.model.User"> insert into user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="id != null"> Id, </if> <if test="name != null"> Name, </if> <if test="sex != null"> Sex, </if> <if test="age != null"> Age, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="id != null"> #{id,jdbcType=INTEGER}, </if> <if test="name != null"> #{name,jdbcType=VARCHAR}, </if> <if test="sex != null"> #{sex,jdbcType=VARCHAR}, </if> <if test="age != null"> #{age,jdbcType=TINYINT}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="test.model.User"> update user <set> <if test="name != null"> Name = #{name,jdbcType=VARCHAR}, </if> <if test="sex != null"> Sex = #{sex,jdbcType=VARCHAR}, </if> <if test="age != null"> Age = #{age,jdbcType=TINYINT}, </if> </set> where Id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="test.model.User"> update user set Name = #{name,jdbcType=VARCHAR}, Sex = #{sex,jdbcType=VARCHAR}, Age = #{age,jdbcType=TINYINT} where Id = #{id,jdbcType=INTEGER} </update> </mapper>
5、User.java
package test.model; public class User { private Integer id; private String name; private String sex; private Byte age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex == null ? null : sex.trim(); } public Byte getAge() { return age; } public void setAge(Byte age) { this.age = age; } }
6、UserMapper.java
package test.dao; import test.model.User; public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }