1、创建数据库和表
2、创建一个Maven工程
我创建的是一个Maven Module工程,如下图:
3、为工程增加Mybatis的maven依赖
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>MybatisPlugin</groupId>
<artifactId>MybatisPlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>MybatisGeneratorPlugin</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
</project>
4、创建generatorConfig.xml文件
file–>new–>other–>mybatis–>mybatis genertator configuration file–>next–>browse–>finish
5、修改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> <!-- 引入配置文件 --> <properties resource="./init.properties" /> <!-- 指定数据连接驱动jar地址 --> <classPathEntry location="${classPath}" /> <!-- 一个数据库一个context --> <context id="infoGuardian"> <!-- 注释 --> <commentGenerator> <!-- 是否取消注释 --> <property name="suppressAllComments" value="false" /> <!-- 是否生成注释代时间戳 --> <property name="suppressDate" value="true" /> </commentGenerator> <!-- jdbc连接 --> <jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}" userId="${jdbc_user}" password="${jdbc_password}" /> <!-- 类型转换 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!-- 生成实体类地址,targetProject是工程的名字,也就是生成的类和xml放到哪个工程下, 注意:这个工程是Eclipse工程的名字,不是Maven工程的groupId或者artifactId --> <javaModelGenerator targetPackage="com.example.entity" targetProject="${project}"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> <!-- 是否针对string类型的字段在set的时候进行trim调用 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!-- 生成mapxml文件,注意如果是Maven工程,配置文件都放在src/main/resources目录下, targetProject必须要写上${project}/src/main/resources --> <sqlMapGenerator targetPackage="com.example.dao" targetProject="${project}/src/main/resources"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </sqlMapGenerator> <!-- 生成mapxml对应client,也就是接口dao --> <javaClientGenerator targetPackage="com.example.dao" targetProject="${project}" type="XMLMAPPER"> <!-- 是否在当前路径下新加一层schema,eg:fase路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] --> <property name="enableSubPackages" value="false" /> </javaClientGenerator> <!-- 配置表信息 --> <table schema="${jdbc_user}" tableName="s_user" domainObjectName="UserEntity" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"> <!-- schema即为数据库名 tableName为对应的数据库表 domainObjectName是要生成的实体类 enable*ByExample 是否生成 example类 --> <!-- 忽略列,不生成bean 字段 --> <ignoreColumn column="FRED" /> <!-- 指定列的java数据类型 --> <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> </table> </context> </generatorConfiguration>
- init配置文件
#Mybatis Generator configuration
project=MybatisGenerator插件
classPath=/home/faith/Repository/Programs/apache-maven-3.3.9/repository/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
jdbc_user=root
jdbc_password=br13jHH5H6
6、运行Mybatis Generator插件,生成类和xml
生成之后的目录结构如下:
7、查看生成的类(接口)和xml
package com.example.entity;
public class UserEntity {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column s_user.id
*
* @mbg.generated
*/
private String id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column s_user.LONG_VARCHAR_FIELD
*
* @mbg.generated
*/
private String longVarcharField;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column s_user.name
*
* @mbg.generated
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column s_user.age
*
* @mbg.generated
*/
private Integer age;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column s_user.id
*
* @return the value of s_user.id
*
* @mbg.generated
*/
public String getId() {
return id;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column s_user.id
*
* @param id the value for s_user.id
*
* @mbg.generated
*/
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column s_user.LONG_VARCHAR_FIELD
*
* @return the value of s_user.LONG_VARCHAR_FIELD
*
* @mbg.generated
*/
public String getLongVarcharField() {
return longVarcharField;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column s_user.LONG_VARCHAR_FIELD
*
* @param longVarcharField the value for s_user.LONG_VARCHAR_FIELD
*
* @mbg.generated
*/
public void setLongVarcharField(String longVarcharField) {
this.longVarcharField = longVarcharField == null ? null : longVarcharField.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column s_user.name
*
* @return the value of s_user.name
*
* @mbg.generated
*/
public String getName() {
return name;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column s_user.name
*
* @param name the value for s_user.name
*
* @mbg.generated
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column s_user.age
*
* @return the value of s_user.age
*
* @mbg.generated
*/
public Integer getAge() {
return age;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column s_user.age
*
* @param age the value for s_user.age
*
* @mbg.generated
*/
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.dao;
import com.example.entity.UserEntity;
public interface UserEntityMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
int deleteByPrimaryKey(String id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
int insert(UserEntity record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
int insertSelective(UserEntity record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
UserEntity selectByPrimaryKey(String id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
int updateByPrimaryKeySelective(UserEntity record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table s_user
*
* @mbg.generated
*/
int updateByPrimaryKey(UserEntity record);
}
<?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.example.dao.UserEntityMapper">
<resultMap id="BaseResultMap" type="com.example.entity.UserEntity">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" property="longVarcharField" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Base_Column_List">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
id, LONG_VARCHAR_FIELD, name, age
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select
<include refid="Base_Column_List" />
from s_user
where id = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from s_user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.example.entity.UserEntity">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into s_user (id, LONG_VARCHAR_FIELD, name,
age)
values (#{id,jdbcType=VARCHAR}, #{longVarcharField,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.example.entity.UserEntity">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
insert into s_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="longVarcharField != null">
LONG_VARCHAR_FIELD,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="longVarcharField != null">
#{longVarcharField,jdbcType=VARCHAR},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.entity.UserEntity">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update s_user
<set>
<if test="longVarcharField != null">
LONG_VARCHAR_FIELD = #{longVarcharField,jdbcType=VARCHAR},
</if>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.entity.UserEntity">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update s_user
set LONG_VARCHAR_FIELD = #{longVarcharField,jdbcType=VARCHAR},
name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>