码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis_plus简单学习

时间:2020-05-27 23:14:22      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:zab   actor   包名   figure   repos   time   ini   configure   根据   

数据库略

二、初始化工程

使用 Spring Initializr 快速初始化一个 Spring Boot 工程

Group:com.atguigu
Artifact:mybatis-plus
版本:2.2.1.RELEASE

三、添加依赖

1、引入依赖
spring-boot-starter、spring-boot-starter-test
添加:mybatis-plus-boot-starter、MySQL、lombok、
在项目中使用Lombok可以减少很多重复代码的书写。比如说getter/setter/toString等方法的编写

<?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.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mybatisplus</groupId>
    <artifactId>myplus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>myplus</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

四、配置

在 application.properties 配置文件中添加 MySQL 数据库的相关配置:
mysql5

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/loadtest?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
#热部署文件,页面不产生缓存,及时更新# 开发阶段务必关闭缓存 (=false)
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#logging.level.com.dy.springboot.server.mapper=debug
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=1000MB
server.port=8081

#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

注意:

1、这里的 url 使用了 ?serverTimezone=GMT%2B8 后缀,因为Spring Boot 2.1 集成了 8.0版本的jdbc驱动,这个版本的 jdbc 驱动需要添加这个后缀,否则运行测试用例报告如下错误:

java.sql.SQLException: The server time zone value ‘?D1ú±ê×?ê±??‘ is unrecognized or represents more

2、这里的 driver-class-name 使用了 com.mysql.cj.jdbc.Driver ,在 jdbc 8 中 建议使用这个驱动,之前的 com.mysql.jdbc.Driver 已经被废弃,否则运行测试用例的时候会有 WARN 信息

五、编写代码

1、主类
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹
注意:扫描的包名根据实际情况修改


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

/**
 *
 * @author liwen406
 * @date 2020-05-27 11:22
 */

@MapperScan("com.mybatisplus.mapper")
@SpringBootApplication
public class MyplusApplication {

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

}

2、实体
创建包 entity 编写实体类 ProjectInterface.java(此处使用了 Lombok 简化代码)


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;

/**
 * <p>
 * 工程接口
 * </p>
 *
 * @author liwen
 * @since 2020-05-25
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value = "ProjectInterface对象", description = "工程接口")
public class ProjectInterface implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "主键ID")
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "工程id")
    private Long projectId;

    private String interName;

    @ApiModelProperty(value = "接口url")

Lombok使用参考:
https://blog.csdn.net/motui/article/details/79012846

3、mapper
创建包 mapper 编写Mapper 接口: ProjectInterfaceMapper.java

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mybatisplus.pojo.ProjectInterface;
import org.springframework.stereotype.Repository;

/**
 * @author liwen
 * @Title: ProjectInterfaceMapper
 * @Description:
 * @date 2020/5/27 / 11:33
 * IDEA在 userMapper 处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确的执行。
 * <p>
 * 为了避免报错,可以在 dao 层 的接口上添加 @Repository 注
 */
@Repository
public interface ProjectInterfaceMapper extends BaseMapper<ProjectInterface> {
}

六、开始使用

添加测试类,进行功能测试:


import com.mybatisplus.mapper.ProjectInterfaceMapper;
import com.mybatisplus.pojo.ProjectInterface;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class MyplusApplicationTests {

    @Autowired
    ProjectInterfaceMapper projectInterfaceMapper;

    @Test
    void contextLoads() {
        System.out.println(("----- selectAll method test ------"));

        List<ProjectInterface> projectInterfaces = projectInterfaceMapper.selectList(null);
        projectInterfaces.forEach(System.out::println);
    }

}

mybatis_plus简单学习

标签:zab   actor   包名   figure   repos   time   ini   configure   根据   

原文地址:https://blog.51cto.com/357712148/2499078

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!