1 在pom.xml中添加org.mybatis.generator插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.2version>
plugin>
plugins>
build>
2 添加generator运行选项
在Command line中添加如下代码
mybatis-generator:generate -e
3 在resources下建立generatorConfig.xml 内容如下
<generatorConfiguration>
<classPathEntry location="D:\woooooork\jar\mysqlconnnect\mysql-connector-java-5.1.44.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true" userId="root" password="123456">
jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
javaTypeResolver>
<javaModelGenerator targetPackage="main.java.com.model" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
javaModelGenerator>
<sqlMapGenerator targetPackage="main.resources.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="main.java.com.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<table tableName="t_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">table>
context>
generatorConfiguration>
细节根据注释修改,绑定到数据的表后,运行generator后就可以啦!
4 自动生成的xml文件结构如下
<mapper namespace="com.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.model.User" >
<id column="user_id" property="userId" jdbcType="INTEGER" />
<result column="user_name" property="userName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="phone" property="phone" jdbcType="VARCHAR" />
resultMap>
<sql id="Base_Column_List" >
user_id, user_name, password, phone
sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_user
where user_id = #{userId,jdbcType=INTEGER}
select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_user
where user_id = #{userId,jdbcType=INTEGER}
delete>
<insert id="insert" parameterType="com.model.User" >
insert into t_user (user_id, user_name, password,
phone)
values (#{userId,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR})
insert>
<insert id="insertSelective" parameterType="com.model.User" >
insert into t_user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="userId != null" >
user_id,
if>
<if test="userName != null" >
user_name,
if>
<if test="password != null" >
password,
if>
<if test="phone != null" >
phone,
if>
trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="userId != null" >
#{userId,jdbcType=INTEGER},
if>
<if test="userName != null" >
#{userName,jdbcType=VARCHAR},
if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
if>
<if test="phone != null" >
#{phone,jdbcType=VARCHAR},
if>
trim>
insert>
<update id="updateByPrimaryKeySelective" parameterType="com.model.User" >
update t_user
<set >
<if test="userName != null" >
user_name = #{userName,jdbcType=VARCHAR},
if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
if>
<if test="phone != null" >
phone = #{phone,jdbcType=VARCHAR},
if>
set>
where user_id = #{userId,jdbcType=INTEGER}
update>
<update id="updateByPrimaryKey" parameterType="com.model.User" >
update t_user
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR}
where user_id = #{userId,jdbcType=INTEGER}
update>
<select id="getUser" parameterType="String" resultType="com.model.User" >
SELECT * FROM t_user WHERE username = #{userName,jdbcType=VARCHAR}
select>
<select id="getUserByPhone" parameterType="String" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"/>
FROM t_user where phone= #{phone,jdbcType=VARCHAR}
select>
mapper>