spring boot整合mybatis的逆向工程
1.先添加pom.xml依赖
<!-- mybatis整合的数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- mybatis整合spring boot的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
2.添加插件支持在pom.xml的build中
<!-- mybatis代码自动生成的插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
3.编写GeneratorMapper.xml生成文件(一定要按照要求写 路径和src同级)
GeneratorMapper.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>
<!--指定连接数据库的JDBC 驱动包所在位置,指定到你本机的完整路径 这个数据库的驱动包是自己主机上的位置-->
<classPathEntry location="D:\MyAppClass\MySQL\mysql-connector-java-5.1.7-bin.jar"/>
<!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
<context id="tables" targetRuntime="MyBatis3">
<!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--配置数据库连接信息-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"
userId="root"
password="28110">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!--生成model 类,targetPackage 指定 model 类的包名,targetProject 指定
生成的 model放在eclipse的哪个工程下面-->
<javaModelGenerator targetPackage="com.springboot.mybatistogether.model"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!--生成 MyBatis的Mapper.xml文件,targetPackage 指定 mapper.xml文件的包名,targetProject 指定生成的 mapper.xml放在 eclipse的哪个工程下面
-->
<sqlMapGenerator targetPackage="com.springboot.mybatistogether.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的 Mapper 接口放在eclipse 的哪个工程下面
-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.springboot.mybatistogether.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--数据库表名及对应的Java模型类名-->
<!--库名实体类名的对应-->
<table tableName="user" domainObjectName="User"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false" />
</context>
</generatorConfiguration>
4.生成实体类和接口
5.生成结果:
6.我们看一下生成的结果(6个4种方法)
mapper接口
package com.springboot.mybatistogether.mapper;
import com.springboot.mybatistogether.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);
}
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.springboot.mybatistogether.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.springboot.mybatistogether.model.User">
<!-- id 标签只能映射的是主键的字段
column 数据库中的字段名称
property 是对象的属性名称
jdbcType列在数据库中的字段类型
result是除了主键外的字段
resultMap 1.是属性名和数据库中的字段名不一样的时候进行转换用的
2.当前查询的结果没有对应属性的时候,自定义一个结果集-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="username" jdbcType="VARCHAR" property="username" />
<result column="birthday" jdbcType="TIMESTAMP" property="birthday" />
<result column="sex" jdbcType="CHAR" property="sex" />
<result column="address" jdbcType="VARCHAR" property="address" />
</resultMap>
<!-- sql语句片段 抽取公共部分-->
<sql id="Base_Column_List">
id, username, birthday, sex, address
</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="com.springboot.mybatistogether.model.User">
insert into user (id, username, birthday,
sex, address)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{birthday,jdbcType=TIMESTAMP},
#{sex,jdbcType=CHAR}, #{address,jdbcType=VARCHAR})
</insert>
<!-- 属性值可以暂时不添加 更加人性化 可选择的插入某些值-->
<insert id="insertSelective" parameterType="com.springboot.mybatistogether.model.User">
insert into user
# 去除多余的逗号,并且指明了插入语句的左右括号
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="username != null">
username,
</if>
<if test="birthday != null">
birthday,
</if>
<if test="sex != null">
sex,
</if>
<if test="address != null">
address,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="username != null">
#{username,jdbcType=VARCHAR},
</if>
<if test="birthday != null">
#{birthday,jdbcType=TIMESTAMP},
</if>
<if test="sex != null">
#{sex,jdbcType=CHAR},
</if>
<if test="address != null">
#{address,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!-- 可选择的更新某些字段 有值更新 无值略过-->
<update id="updateByPrimaryKeySelective" parameterType="com.springboot.mybatistogether.model.User">
update user
<set>
<if test="username != null">
username = #{username,jdbcType=VARCHAR},
</if>
<if test="birthday != null">
birthday = #{birthday,jdbcType=TIMESTAMP},
</if>
<if test="sex != null">
sex = #{sex,jdbcType=CHAR},
</if>
<if test="address != null">
address = #{address,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<!-- 根据主键更新属性 全部更新属性-->
<update id="updateByPrimaryKey" parameterType="com.springboot.mybatistogether.model.User">
update user
set username = #{username,jdbcType=VARCHAR},
birthday = #{birthday,jdbcType=TIMESTAMP},
sex = #{sex,jdbcType=CHAR},
address = #{address,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
注意只能操作单!!!
注意点:
我们数据库的字段起名字的时候一定要单词之间以_下划线进行分割才能让逆向工程导入到实体类中的起名字的规则正确
从上面我们可以看出,如果我们数据库字段的设计名称以下划线进行分割的话,我们的属性名称会默认按照驼峰命名法进行设计,如果我们不按照规则,自己按照自己方式起名字,那么会造成一些差错,不符合我们设计名称的原则!!!