mybatis-plus官网> https://mp.baomidou.com/
java1.8
maven3.6.0
Sql Server 5.7
idea
2.创建组名以及文件名
3.父类文件不用管,直接在父类文件再创建一个子类文件作为demo,这样可以在一个父类中练习多个测试模块
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
com.cd
rm-demo
0.0.1-SNAPSHOT
rm-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
com.baomidou
mybatis-plus-boot-starter
3.0.3
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-maven-plugin
2.1.在resource中创建application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=true
username: mysql账号
password: mysql密码
2.2.创建pojo包,然后加入实体类
package com.cd.pojo;
import lombok.Data;
@Data //默认提供Getter,Setter,equals,hashCode,toString方法
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
2.3.创建dao包,创建接口.继承的BaseMapper<>中有对连接数据库的封装.可以Ctrl+点击查看
package com.cd.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cd.pojo.User;
public interface UserMapper extends BaseMapper {
}
2.4.然后在启动类中加入注解@MapperScan();
package com.cd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cd.dao")//写自己的mqpper位置
public class RmDemoApplication {
public static void main(String[] args) {
SpringApplication.run(RmDemoApplication.class, args);
}
}
2.5.在测试类中
package com.cd;
import com.cd.dao.UserMapper;
import com.cd.pojo.User;
import org.junit.Assert;
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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RmDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void contextLoads() {
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
说说我练习中遇到的BUG:
1:首先是yml中useSSL=true,这个是需要加的,如果不加的话会报错
意思是使用JDBC跟你的数据库连接的时候,你的JDBC版本与MySQL版本不兼容,MySQL的版本更高一些,在连接语句后加上“useSSL=‘true’” ,就可以连接到数据库了。更高版本
2.测试类注入Mapper时会报红,但是这个不影响操作,
应该是他需要的依赖没有给他,在运行编译时获取到的数据会自动给它,所以这个不影响