搭建开发环境:
a.本地仓库
b.创建项目
c.pom.xml添加相关依赖
Mybatis-plus简单应用:
a.编写启动类 MyBatisPlusApplication
b.编写配置文件 application.properties
c.创建一个数据库 mybatis_plus
d.编写一个实体映射类 User
e.编写一个mapper操作数据库UserMapper
f.测试
搭建开发环境
代码实现4.0.0 com.tan mybatis_plus 1.0-SNAPSHOT spring-boot-parent org.springframework.boot 2.2.6.RELEASE com.baomidou mybatis-plus-boot-starter 3.0.5 org.springframework.boot spring-boot-starter-test mysql mysql-connector-java org.projectlombok lombok
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; @Repository public interface UserMapper extends BaseMapper{ }
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;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSearchAllUser(){
List userList = userMapper.selectList(null);
for (User user : userList) {
System.out.println(user);
}
}
@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即可 问题四: 报错: 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)



