SpringCloud第01讲:整合MyBatis通用Mapper

导读:本篇文章讲解 SpringCloud第01讲:整合MyBatis通用Mapper,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

参考官网:

https://github.com/abel533/Mapper

https://github.com/abel533/Mapper/wiki/1.3-spring-boot

        MyBatis通用Mapper是一款MyBatis增强插件,可以根据数据库表快速生成实体Entity、数据访问对象模型DAO,数据库映射文件Mapper。

一、整合通用Mapper
1.1、在项目pom.xml中添加依赖(文章最后面提供完整pom.xml)

放在<denpendencies>下

<!-- 自动生成getter和setter,构造器-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

由于通用mapper集成了MyBatis,所以这里我们需要将下方MyBatista的依赖删除掉

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.1</version>
</dependency>

1.2、在application.yml中添加数据库配置属性

server:
  port: 8082
spring:
  datasource:
    url: jdbc:mysql://39.107.35.145:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: Aa123123.
    driver-class-name: com.mysql.cj.jdbc.Driver  #8.0??
    #driver-class-name: com.mysql.jdbc.Driver

1.3、在pom.xml中添加插件

继续添加依赖,整合通用代码生成器(文章最后面提供完整pom.xml)

放在<build>——<plugins>下

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.16</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

1.4、 配置通用mapper

在resource中创建文件夹generator及配置文件config.properties和generatorConfig.xml(文章最后面提供完整文件)

SpringCloud第01讲:整合MyBatis通用Mapper 

1.4.1、配置config.properties

jdbc.driverClass=com.mysql.cj.jdbc.Driver
#jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://39.107.35.145:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
jdbc.user=root
jdbc.password=Aa123123.

# 模块名(包名)
moduleName=user
# 表名
tableName=user

1.4.2、generatorConfig.xml

<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="generator/config.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
            <property name="lombok" value="Getter,Setter,ToString"/>
        </plugin>

        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <!--实体-->
        <javaModelGenerator targetPackage="com.jf.demo5.entity.${moduleName}"
                            targetProject="src/main/java"/>

        <!--mapper.xml-->
        <sqlMapGenerator targetPackage="com.jf.demo5.dao.${moduleName}"
                         targetProject="src/main/resources"/>

        <!--mapper接口-->
        <javaClientGenerator targetPackage="com.jf.demo5.dao.${moduleName}"
                             targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <!--为哪张表生成代码-->
        <table tableName="${tableName}">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

1.5、通过mybatis-generator生成代码

代码生成器的使用方式:可以自动生成实体、dao和mapper.xml

SpringCloud第01讲:整合MyBatis通用Mapper

效果如下:

SpringCloud第01讲:整合MyBatis通用Mapper 

二、测试

2.1、新建TestController测试

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 测试查询
     * @return
     */
    @GetMapping("/t1")
    @ResponseBody
    public List<User> getuser(){
        List<User> users = userMapper.selectAll();
        return users;
    }

2.2、在BootApplication中添加@MapperScan注解扫描dao

注意:导入的MapperScan是tk包下的

package com.jf.demo5;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("com.jf.demo5.dao")
public class Demo5Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo5Application.class, args);
    }

}

2.3、测试

SpringCloud第01讲:整合MyBatis通用Mapper

 2.4、测试添加、修改、删除

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 测试查询
     * @return
     */
    @GetMapping("/t1")
    @ResponseBody
    public List<User> getuser(){
        List<User> users = userMapper.selectAll();
        return users;
    }

    /**
     * 测试添加
     * @return
     */
    @GetMapping("/t2")
    @ResponseBody
    public String adduser(){
        User u = new User();
        u.setName("英英");
        u.setAge(18);
        u.setEmail("1234567@163.com");
        int rows = userMapper.insert(u);
        if (rows > 0){
            return "添加成功";
        }
        else {
            return "添加失败";
        }
    }

    /**
     * 测试修改
     * @return
     */
    @GetMapping("/t3")
    @ResponseBody
    public String updateuser(){
        User u = new User();
        u.setName("小樱");
        u.setId(25l);
        int rows = userMapper.modify(u);
        if (rows > 0){
            return "修改成功";
        }
        else {
            return "修改失败";
        }
    }

    /**
     * 测试删除
     * @return
     */
    @GetMapping("/t4")
    @ResponseBody
    public String deleteuser(){
        User u = new User();
        u.setId(25l);
        int rows = userMapper.delete(u);
        if (rows > 0){
            return "删除成功";
        }
        else {
            return "删除失败";
        }
    }
}

SpringCloud第01讲:整合MyBatis通用Mapper

数据库效果如下:

 SpringCloud第01讲:整合MyBatis通用Mapper

 SpringCloud第01讲:整合MyBatis通用Mapper

 数据库效果如下:

SpringCloud第01讲:整合MyBatis通用Mapper

SpringCloud第01讲:整合MyBatis通用Mapper 

数据库效果如下:

 SpringCloud第01讲:整合MyBatis通用Mapper

三、完整pom.xml及源代码

<?xml version="1.0" encoding="UTF-8"?>
<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 https://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>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jf</groupId>
    <artifactId>Demo5</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Demo5</name>
    <description>Demo5</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- 自动生成getter和setter,构造器-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.16</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

</project>

附录一:解决jar包依赖找不到问题

第1步:在你自己的本地仓库找到你所需要添加的 jar 包,如下

SpringCloud第01讲:整合MyBatis通用Mapper

第2步:右键,工程项目,找到如下图所示: 

SpringCloud第01讲:整合MyBatis通用Mapper

 第3步:手动添加你刚刚找到的 jar 包,如下图:

SpringCloud第01讲:整合MyBatis通用Mapper

 第4步:添加成功,如下图:

SpringCloud第01讲:整合MyBatis通用Mapper

 参考自:

解决“driver-class-name: com.mysql.jdbc.Driver”无法解析问题_Alpha_xxy的博客-CSDN博客_driver-class-name:

SpringCloud第03讲:(番外篇)整合MyBatis通用Mapper_大坏蛋^_^的博客-CSDN博客_springcloud整合mybatis

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/111751.html

(0)
Java光头强的头像Java光头强

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!