Mybatis_Plus-简单应用

Mybatis-plus简单应用

开发步骤

搭建开发环境:
		a.本地仓库
		b.创建项目
        c.pom.xml添加相关依赖

Mybatis-plus简单应用:
		a.编写启动类 MyBatisPlusApplication
		b.编写配置文件 application.properties
		c.创建一个数据库 mybatis_plus
		d.编写一个实体映射类 User
		e.编写一个mapper操作数据库UserMapper
		f.测试

搭建开发环境

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tan</groupId>
    <artifactId>mybatis_plus</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.6.RELEASE</version>
    </parent>

    <dependencies>
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>

        <!--导入Spring Boot测试模块以及JUnitAssertJHamcrest和许多其他有用的库-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--lombok用来简化实体类-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>


</project>

代码实现

a.编写启动类 MyBatisPlusApplication

package com.tan;

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

@SpringBootApplication
@MapperScan("com.tan.mapper")
public class MyBatisPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyBatisPlusApplication.class,args);
    }
}

b.编写配置文件 application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

c.创建一个数据库 mybatis_plus

CREATE TABLE user
	(
		id BIGINT(20) NOT NULL COMMENT '主键ID',
		name VARCHAR(50) NULL DEFAULT NULL COMMENT '姓名',
		age INT(11) NULL DEFAULT NULL COMMENT '年龄',
		email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
		PRIMARY KEY (id)
	);
INSERT INTO user (id, name, age, email) VALUES
	(1, 'aa', 18, 'test1@future.com'),
	(2, 'bb', 19, 'test2@future.com'),
	(3, 'cc', 20, 'test3@future.com'),
	(4, 'dd', 21, 'test4@future.com'),
	(5, 'ee', 22, 'test5@future.com');

d.编写一个实体映射类 User

package com.tan.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
public class User {
    private Long id;
    private String name;
    private String email;
    private int age;
}

e.编写一个mapper操作数据库UserMapper

package com.tan.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tan.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/*
@Component  : generic stereotype for any Spring-managed component
@Repository : stereotype for persistence layer
@Service    : stereotype for service layer
@Controller : stereotype for presentation layer (spring-mvc)

@Component比@Repository、@Service、@Controller应用场景更广,@Repository、@Service、@Controller是@Component的特殊情况
@Component  : 表明是spring的组件
@Repository : 表明是持久层的部分
@Service    : 表明是业务层的部分
@Controller : 表明是控制层的部分
 */
@Repository
public interface UserMapper extends BaseMapper<User> {

}

f.测试

package com.tan;


import com.tan.entity.User;
import com.tan.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/*
1.@SpringBootTest:对众多的单元测试技术进行了集成

2.@RunWith(SpringRunner.class):让测试在Spring容器环境下执⾏,如测试类中⽆此注解,将导致service,dao等⾃动注⼊失败
因为SpringRunner.class继承了SpringJUnit4ClassRunner.class且没有进⾏任何修改
所以@RunWith(SpringRunner.class)基本等同于@RunWith(SpringJUnit4ClassRunner.class)
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyBatisPlusTest {

    @Autowired
    private UserMapper userMapper;

    /*
    1.查找所有的用户
     */
    @Test
    public void testSearchAllUser(){
        List<User> userList = userMapper.selectList(null);
        for (User user : userList) {
            System.out.println(user);
        }
    }

    /*
    2.添加一个用户
     */
    @Test
    public void testSaveUser(){
        User user = new User();
        user.setAge(22);
        user.setName("Guzmin");
        user.setEmail("Guzmin@ModernLove.com");
        userMapper.insert(user);
    }
}

知识归纳

问题一:时区问题
报错:
java.sql.SQLException: The server time zone value '�й���׼ʱ��'
is unrecognized or represents more than one time zone.
解决办法:
将spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus改成:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
即:加上?serverTimezone=GMT%2B8

问题二:sql的cj问题
报错:
Loading class `com.mysql.jdbc.Driver'. This is deprecated.
The new driver class is `com.mysql.cj.jdbc.Driver'.
解决办法:
将spring.datasource.driver-class-name=com.mysql.jdbc.Driver改成:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

问题三:映射绑定问题
报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
com.tan.mapper.UserMapper.selectList
解决办法:
org.apache.ibatis.binding.BindingException:在mybatis中dao接口与mapper配置文件在做映射绑定的时候出现问题
在写UserMapper接口中继承BaseMapper没有指明泛型类型,加上BaseMapper<User>即可

问题四:
报错:
No qualifying bean of type 'com.tan.mapper.UserMapper' available
没有可用的'com.tan.mapper.UserMapper'类型的对象
原因:
项目没有扫描到mapper包
解决办法:
方法一:在启动类上添加@MapperScan("com.tan.mapper")
方法二:在mapper类上加入注解@Mapper,告诉spring这是一个mapper类

知识点1:Jupiter与Junit的区别
org.junit.jupiter.api.Test与Junit的区别:
jupiter不能加@RunWith(SpringRunner.class),但是Junit必须要加@RunWith(SpringRunner.class)

知识点2:MyBatis-PLUS添加主键
使用MyBatis-PLUS添加一个用户信息的时候:
当id是主键的时候,id要自动生成,则id必须是对象类型,不能是基本类型
因为是基本类型的时候默认的id都是0,会报错,但是是包装类的时候会有一定自动生成id的方式

知识点3:SpringBoot版本区别
springboot版本区别:
a.1.5.x与2.x的版本区别
	springboot1.5.x采用的spring是4.x
	springboot2.x采用的spring是5.x(大量采用了注解)
b.2.0.x与2.1.x的区别
	数据库连接需要有时区以及cj版本

知识点4:集群和分布式的概念
集群:	 相同的代码或者服务 部署在不同的服务器上
分布式:  不同的代码或者服务 部署在不同的服务器上
    ---> 分布式一定是集群  集群不一定是分布式

知识点5:@Component、@Repository、@Service、@Controller的作用
    @Component  : generic stereotype for any Spring-managed component
    @Repository : stereotype for persistence layer
    @Service    : stereotype for service layer
    @Controller : stereotype for presentation layer (spring-mvc)

@Component比@Repository、@Service、@Controller应用场景更广,@Repository、@Service、@Controller是@Component的特殊情况
@Component  : 表明是spring的组件
@Repository : 表明是持久层的部分
@Service    : 表明是业务层的部分
@Controller : 表明是控制层的部分


知识点6:@SpringBootTest和@RunWith(SpringRunner.class)
1.@SpringBootTest:对众多的单元测试技术进行了集成,在测试类中标识了该注解,则会去找主启动类

2.@RunWith(SpringRunner.class):让测试在Spring容器环境下执⾏,如测试类中⽆此注解,将导致service,dao等⾃动注⼊失败
因为SpringRunner.class继承了SpringJUnit4ClassRunner.class且没有进⾏任何修改
所以@RunWith(SpringRunner.class)基本等同于@RunWith(SpringJUnit4ClassRunner.class)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值