js9905com金沙网站SpringBoot 3.SpringBoot 整合 MyBatis 逆

SpringBoot 3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper,springbootmybatis

一、添加所需依赖,当前完整的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的信息,请查看:

二、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.....

js9905com金沙网站 1

  我们的UserMapper以及UserMapper.xml就生成了。

js9905com金沙网站 2

  (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

js9905com金沙网站 3

  我们原先编写的 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

 js9905com金沙网站 4

  输入:localhost:8080/user/list

 js9905com金沙网站 5

  好了,到这我们整合了Mybatis逆向工程以及通用Mapper。

3.SpringBoot 整合 MyBatis 逆向工程以及 MyBatis 通用 Mapper,springbootmybatis 一、添加所需依赖,当前完整的pom文件如下: project xmlns ="htt...

本文由js9905com金沙网站-金沙澳门手机版网址发布于计算机,转载请注明出处:js9905com金沙网站SpringBoot 3.SpringBoot 整合 MyBatis 逆

您可能还会对下面的文章感兴趣: