您好,登錄后才能下訂單哦!
一、添加所需依賴,當前完整的pom文件如下:
<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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <groupId>org.rcddup</groupId> <artifactId>rcddup-custom</artifactId> <version>1.0.0</version> <properties> <java.version>1.8</java.version> <mybatis.version>1.3.1</mybatis.version> <mapper.version>3.4.3</mapper.version> <mybatis-generator.version>1.3.3</mybatis-generator.version> <mybatis-tk.version>1.1.4</mybatis-tk.version> <pagehelper.version>1.2.1</pagehelper.version> </properties> <dependencies> <!-- SpringBoot - Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot - MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <!-- SpringBoot - MyBatis 逆向工程 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator.version}</version> </dependency> <!-- MyBatis 通用 Mapper --> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>${mybatis-tk.version}</version> </dependency> <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <!-- MyBatis 逆向工程 插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>${mybatis.generator.version}</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>${mapper.version}</version> </dependency> </dependencies> <configuration> <!-- 允許移動生成的文件 --> <verbose>true</verbose> <!-- 是否覆蓋 --> <overwrite>true</overwrite> <!-- 配置文件 --> <configurationFile> ${basedir}/src/main/resources/generator/generatorConfig.xml </configurationFile> </configuration> </plugin> </plugins> </build> </project>
MyBtais 結合通用 Mapper 非常好使,接下來看逆向工程的配置。
有關通用Mapper的信息,請查看:https://github.com/abel533/Mapper
二、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> <!-- 引入 application.properties --> <properties resource="application.properties" /> <!-- MyBatis3Simple:不生成 Example相關類及方法 --> <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginningDelimiter" value="`" /> <property name="endingDelimiter" value="`" /> <!-- 指定生成 Mapper 的繼承模板 --> <plugin type="tk.mybatis.mapper.generator.MapperPlugin"> <property name="mappers" value="${generator.mappers}" /> </plugin> <!-- 生成 JavaBean 對象重寫 toString方法 --> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <!-- 生成 JavaBean 對象繼承 Serializable 類 --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <!-- 生成 JavaBean 對象重寫 equals 和 hashCode 方法 --> <!-- <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin" /> --> <!-- jdbc 連接配置 --> <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> </jdbcConnection> <javaModelGenerator targetPackage="${generator.javaModel-targetPackage}" targetProject="${generator.targetProject}" /> <sqlMapGenerator targetPackage="${generator.sqlMap-targetPackage}" targetProject="${generator.targetProject}" /> <javaClientGenerator targetPackage="${generator.javaClient-targetPackage}" targetProject="${generator.targetProject}" type="XMLMAPPER" /> <!-- Mysql 配置 --> <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true" /> --> <!-- Oracle 配置 --> <!-- <generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/> --> <!-- tableName:數據庫表名,domainObjectName:生成文件名 ,schema:數據源--> <table tableName="user"> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> </context> </generatorConfiguration>
我們將對應的信息都在 application.properties 中定義好,在這直接使用,application.properties如下:
# Generator generator.targetProject=src/main/java generator.mappers=org.rcddup.app.common.base.IBaseMapper generator.javaModel-targetPackage=org.rcddup.app.domain generator.sqlMap-targetPackage=org.rcddup.app.dao generator.javaClient-targetPackage=org.rcddup.app.dao
三、編寫生成Mapper的集成接口IBaseMapper
package org.rcddup.app.common.base; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.MySqlMapper; public interface IBaseMapper<T> extends Mapper<T>, MySqlMapper<T> { }
集成的 Mapper 以及 MySqlMapper 幾乎包含了你所有的 CRUD方法。
四、使用逆向工程生成我們的第一個文件
右鍵項目 --> Maven --> Maven build --> Goals: mybtais-generator:generate,點擊 run.....
我們的UserMapper以及UserMapper.xml就生成了。
(1)UserMapper.java
package org.rcddup.app.dao; import org.rcddup.app.common.base.IBaseMapper; import org.rcddup.app.domain.User; public interface UserMapper extends IBaseMapper<User> { }
(2)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="org.rcddup.app.dao.UserMapper"> <resultMap id="BaseResultMap" type="org.rcddup.app.domain.User"> <!-- WARNING - @mbg.generated --> <id column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="age" jdbcType="TINYINT" property="age" /> <result column="status" jdbcType="TINYINT" property="status" /> </resultMap> </mapper>
是不是發現xml中都沒有配置任何CRUD方法。
五、重新編寫 UserService.java
我們原先編寫的 UserService 現在我們用新生成的 UserMapper 來重新編寫。
package org.rcddup.app.service.impl; import java.util.List; import org.rcddup.app.dao.UserMapper; import org.rcddup.app.domain.User; import org.rcddup.app.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import tk.mybatis.mapper.entity.Example; @Service public class UserService implements IUserService{ @Autowired private UserMapper userMapper; @Override public List<User> listUser() { return userMapper.selectAll(); } @Override public User getUserById(Long id) { // 方法一:適用于任何字段的查詢 // Example example = new Example(User.class); // example.createCriteria().andEqualTo("id", id); // List<User> list = userMapper.selectByExample(example); // if (list != null && list.size() > 0) { // return list.get(0); // } else { // return null; // } // 方法二:只適用于主鍵字段 return userMapper.selectByPrimaryKey(id); } }
六、啟動 App.java
輸入:localhost:8080/user/get?id=1
輸入:localhost:8080/user/list
好了,到這我們整合了Mybatis逆向工程以及通用Mapper。
總結
以上所述是小編給大家介紹的SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper實例詳解 ,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。