Spring Boot 整合Mybatis 之 Mapper外掛(自動生成XML及Mapper程式碼)
阿新 • • 發佈:2018-12-19
pom檔案 主要程式碼
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--分頁外掛 可忽略--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.9</version> </dependency> </dependencies> <build> <plugins> <!--要打包了這個生成程式碼要禁止掉,本地開發開啟--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <dependencies> <!--配置這個依賴主要是為了等下在配置mybatis-generator.xml的時候可以不用配置classPathEntry這樣的一個屬性,避免程式碼的耦合度太高--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.4.4</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <phase>package</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <!--允許移動生成的檔案 --> <verbose>true</verbose> <!-- 是否覆蓋 --> <overwrite>true</overwrite> <!-- 自動生成的配置 --> <configurationFile>src/main/resources/mybatis-generator/mybatis-generator.xml</configurationFile> </configuration> </plugin> </plugins> </build>
mybatis-generator.xml(resources目錄下面 在pom檔案中指定)
<?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> <!--執行generator外掛生成檔案的命令: call mvn mybatis-generator:generate -e --> <!-- 引入配置檔案 --> <properties resource="config/druid.properties"/> <!--classPathEntry:資料庫的JDBC驅動,換成你自己的驅動位置 可選 --> <!--<classPathEntry location="E:\mybatis\mysql-connector-java-5.1.24-bin.jar" /> --> <!-- 一個數據庫一個context --> <!--defaultModelType="flat" 大資料欄位,不分表 --> <context id="MysqlTables" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <!-- 自動識別資料庫關鍵字,預設false,如果設定為true,根據SqlReservedWords中定義的關鍵字列表; 一般保留預設值,遇到資料庫關鍵字(Java關鍵字),使用columnOverride覆蓋 --> <property name="autoDelimitKeywords" value="true"/> <!-- 生成的Java檔案的編碼 --> <property name="javaFileEncoding" value="utf-8"/> <!-- beginningDelimiter和endingDelimiter:指明資料庫的用於標記資料庫物件名的符號,比如ORACLE就是雙引號,MYSQL預設是`反引號; --> <property name="beginningDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <!-- 格式化java程式碼 --> <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/> <!-- 格式化XML程式碼 --> <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/> <plugin type="org.mybatis.generator.plugins.SerializablePlugin"/> <plugin type="org.mybatis.generator.plugins.ToStringPlugin"/> <!-- 指定需要編譯的Mapper型別 (自定義介面方法)--> <!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">--> <!--<property name="mappers" value="com.wangs.config.MapperBase"/>--> <!--<!–caseSensitive預設false,當資料庫表名區分大小寫時,可以將該屬性設定為true–>--> <!--<property name="caseSensitive" value="true"/>--> <!--</plugin>--> <!-- 註釋 --> <commentGenerator> <property name="suppressAllComments" value="false"/><!-- 是否取消註釋 --> <property name="suppressDate" value="true"/> <!-- 是否生成註釋代時間戳--> </commentGenerator> <!-- jdbc連線 --> <jdbcConnection driverClass="${spring.datasource.type}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"/> <!-- 型別轉換 --> <javaTypeResolver> <!-- 是否使用bigDecimal, false可自動轉化以下型別(Long, Integer, Short, etc.) --> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 生成實體類地址 targetPackage指向資料庫表對應實體類資料夾--> <javaModelGenerator targetPackage="com.wangs.model" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapxml檔案 targetPackage指向儲存生成XML的資料夾--> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成mapxml對應client,也就是介面dao targetPackage指向存放生成Mapper的java介面資料夾--> <javaClientGenerator targetPackage="com.wangs.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- table可以有多個,每個資料庫中的表都可以寫一個table,tableName表示要匹配的資料庫表,也可以在tableName屬性中通過使用%萬用字元來匹配所有資料庫表,只有匹配的表才會自動生成檔案 --> <!--如果想生成一個表則tableName="table_name"--> <table tableName="%" enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true" enableSelectByExample="true" selectByExampleQueryId="true"> <property name="useActualColumnNames" value="false"/> <!-- 資料庫表主鍵 --> <generatedKey column="id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
實體類 User.java(生成程式碼前)
package com.wangs.model; import java.io.Serializable; public class User implements Serializable { private String id; private String name; private String password; private String mobile; private Integer age; private String dis; private static final long serialVersionUID = 1L; }
指向生成程式碼命令:mvn mybatis-generator:generate
IDEA工具可使用
執行成功:
以下為生成程式碼:
程式碼結構:
UserMapper.java
package com.wangs.mapper;
import com.wangs.model.User;
import java.util.List;
public interface UserMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
int deleteByPrimaryKey(String id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
int insert(User record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
User selectByPrimaryKey(String id);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
List<User> selectAll();
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
int updateByPrimaryKey(User record);
}
User.java
package com.wangs.model;
import java.io.Serializable;
public class User implements Serializable {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.id
*
* @mbg.generated
*/
private String id;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.name
*
* @mbg.generated
*/
private String name;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.password
*
* @mbg.generated
*/
private String password;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.mobile
*
* @mbg.generated
*/
private String mobile;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.age
*
* @mbg.generated
*/
private Integer age;
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user.dis
*
* @mbg.generated
*/
private String dis;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database table user
*
* @mbg.generated
*/
private static final long serialVersionUID = 1L;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.id
*
* @return the value of 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 user.id
*
* @param id the value for 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 user.name
*
* @return the value of 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 user.name
*
* @param name the value for 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 user.password
*
* @return the value of user.password
*
* @mbg.generated
*/
public String getPassword() {
return password;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.password
*
* @param password the value for user.password
*
* @mbg.generated
*/
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.mobile
*
* @return the value of user.mobile
*
* @mbg.generated
*/
public String getMobile() {
return mobile;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.mobile
*
* @param mobile the value for user.mobile
*
* @mbg.generated
*/
public void setMobile(String mobile) {
this.mobile = mobile == null ? null : mobile.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.age
*
* @return the value of 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 user.age
*
* @param age the value for user.age
*
* @mbg.generated
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user.dis
*
* @return the value of user.dis
*
* @mbg.generated
*/
public String getDis() {
return dis;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user.dis
*
* @param dis the value for user.dis
*
* @mbg.generated
*/
public void setDis(String dis) {
this.dis = dis == null ? null : dis.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table user
*
* @mbg.generated
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", password=").append(password);
sb.append(", mobile=").append(mobile);
sb.append(", age=").append(age);
sb.append(", dis=").append(dis);
sb.append(", serialVersionUID=").append(serialVersionUID);
sb.append("]");
return sb.toString();
}
}
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="com.wangs.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.wangs.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<id column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="password" jdbcType="VARCHAR" property="password" />
<result column="mobile" jdbcType="VARCHAR" property="mobile" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="dis" jdbcType="VARCHAR" property="dis" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
delete from user
where id = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.wangs.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.String">
SELECT LAST_INSERT_ID()
</selectKey>
insert into user (`name`, `password`, mobile,
age, dis)
values (#{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER}, #{dis,jdbcType=VARCHAR})
</insert>
<update id="updateByPrimaryKey" parameterType="com.wangs.model.User">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
update user
set `name` = #{name,jdbcType=VARCHAR},
`password` = #{password,jdbcType=VARCHAR},
mobile = #{mobile,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
dis = #{dis,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select id, `name`, `password`, mobile, age, dis
from user
where id = #{id,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
-->
select id, `name`, `password`, mobile, age, dis
from user
</select>
</mapper>