Mybatis-Generator 自动生成代码
1.Mybatis-Generator官网介绍
链接:http://www.mybatis.org/generator/index.html
2.我的项目结构:
3.首先是先创建好表格,后面的代码都是通过这个表格来创建的,我的表格如下:
4.添加依赖,我用的是数据库Mysql 8
<!--Mybatis-Generator-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
5.添加插件,注意要改成自己的配置文件地址。
<plugins>
<plugin>
<!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--这里面改成你自己的配置文件的位置-->
<configurationFile>src/main/resources/mybatis-generator.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
添加好之后就会出现如下插件,下面配置好之后就是双击该插件进行自动生成代码:
6.在resources下创建xml文件mybatis-generator.xml,里面的配置一项一项地看下去就大致明白是什么意思了。Model,Mapper,和Dao这三个生成的顺序好像不可以调换。
<?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="DB2Tables" targetRuntime="MyBatis3">
<!--配置注释,如下是不生成注释-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--配置数据库-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/db?characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--1.生成Model类存放位置-->
<javaModelGenerator targetPackage="model" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--2.生成映射文件存放位置-->
<sqlMapGenerator targetPackage="resources" targetProject="src/main">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!--3.生成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--生成对应表及类名-->
<table tableName="message" domainObjectName="Message" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
7.到此基本配置完成,双击插件即可生成代码
8.生成三个文分别是Model,Mapper,和Dao
Model-Message
package model;
public class Message {
private Long id;
private String userName;
private String userSex;
private String school;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName == null ? null : userName.trim();
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex == null ? null : userSex.trim();
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school == null ? null : school.trim();
}
}
Mapper-MessageMapper.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="dao.MessageMapper">
<resultMap id="BaseResultMap" type="model.Message">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_sex" jdbcType="VARCHAR" property="userSex" />
<result column="school" jdbcType="VARCHAR" property="school" />
</resultMap>
<sql id="Base_Column_List">
id, user_name, user_sex, school
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from message
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from message
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="model.Message">
insert into message (id, user_name, user_sex,
school)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{userSex,jdbcType=VARCHAR},
#{school,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="model.Message">
insert into message
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
<if test="userSex != null">
user_sex,
</if>
<if test="school != null">
school,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
#{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
#{school,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="model.Message">
update message
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
user_sex = #{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
school = #{school,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="model.Message">
update message
set user_name = #{userName,jdbcType=VARCHAR},
user_sex = #{userSex,jdbcType=VARCHAR},
school = #{school,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
<resultMap id="BaseResultMap" type="model.Message">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_sex" jdbcType="VARCHAR" property="userSex" />
<result column="school" jdbcType="VARCHAR" property="school" />
</resultMap>
<sql id="Base_Column_List">
id, user_name, user_sex, school
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from message
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from message
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="model.Message">
insert into message (id, user_name, user_sex,
school)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{userSex,jdbcType=VARCHAR},
#{school,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="model.Message">
insert into message
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
<if test="userSex != null">
user_sex,
</if>
<if test="school != null">
school,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
#{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
#{school,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="model.Message">
update message
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
user_sex = #{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
school = #{school,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="model.Message">
update message
set user_name = #{userName,jdbcType=VARCHAR},
user_sex = #{userSex,jdbcType=VARCHAR},
school = #{school,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
<resultMap id="BaseResultMap" type="model.Message">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="user_name" jdbcType="VARCHAR" property="userName" />
<result column="user_sex" jdbcType="VARCHAR" property="userSex" />
<result column="school" jdbcType="VARCHAR" property="school" />
</resultMap>
<sql id="Base_Column_List">
id, user_name, user_sex, school
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from message
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from message
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="model.Message">
insert into message (id, user_name, user_sex,
school)
values (#{id,jdbcType=BIGINT}, #{userName,jdbcType=VARCHAR}, #{userSex,jdbcType=VARCHAR},
#{school,jdbcType=VARCHAR})
</insert>
<!--判断是否为空进行插入-->
<insert id="insertSelective" parameterType="model.Message">
insert into message
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
<if test="userSex != null">
user_sex,
</if>
<if test="school != null">
school,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
#{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
#{school,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!--判断是否为空进行插入-->
<update id="updateByPrimaryKeySelective" parameterType="model.Message">
update message
<set>
<if test="userName != null">
user_name = #{userName,jdbcType=VARCHAR},
</if>
<if test="userSex != null">
user_sex = #{userSex,jdbcType=VARCHAR},
</if>
<if test="school != null">
school = #{school,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="model.Message">
update message
set user_name = #{userName,jdbcType=VARCHAR},
user_sex = #{userSex,jdbcType=VARCHAR},
school = #{school,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>
Dao-MessageDao
package dao;
import model.User;
public interface UserDao {
int saveUser(User user);
int deleteUserById(Integer id);
int updateUser(User user);
User getUserById(Integer id);
User getUserByName(String name);
}
至此mybatis-generator就大功告成了
PS:可能遇到的坑:
1.一定要记得要先创建表,一切都是通过表来自动生成代码的
2.MySQL 8的中一些问题都通过url来解决
connectionURL="jdbc:mysql://127.0.0.1:3306/db?characterEncoding=utf-8&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true"
各含义如下:
characterEncoding=utf-8&------限定编码格式为utf8
serverTimezone=GMT%2B8&-------限定时区为本地
useSSL=false&------------禁用SSL
allowPublicKeyRetrieval=true------------允许使用主键进行查询
上一篇: 数据库系统设计概述
下一篇: Java实现医疗系统