整合
整合Junit
1.Test类:
2.UserService类:
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add() {
System.out.println("add---");
}
}
3.控制台显示:
整合redis
1. 新建项目
首先要在新建时勾选NoSQL里的Spring Data Redis
2. redis启动
[root@localhost ~]#cd /usr/local/redis/bin
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# ps aux|grep redis
root 101199 0.0 0.2 140992 2032 ? Ssl 04:13 0:00 ./redis-server *:6379
root 101203 0.0 0.0 112824 984 pts/3 S+ 04:13 0:00 grep --color=auto redis
[root@localhost bin]# systemctl stop firewalld
我的redis是之前做项目时在Linux环境下安装配置的,这里就不细说安装过程啦~
3. Test类:
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest
class DemoRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void testSet() {
redisTemplate.boundValueOps("name").set("HR");
}
@Test
void testGet() {
Object name=redisTemplate.boundValueOps("name").get();
System.out.print(name);
}
}
4. 启动redis测试类
先后运行set、get会在控制台成功打印名字的值。
整合MyBatis
1.新建项目
2. User类:
public class User {
private int id;
private String username;
private String password;
}
3. 配置
这次用的是application.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/springbootstyle?characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
4. 数据库表
使用注解Select的方式
1.Mapper类:
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
public List<User> findAll();
}
2. Test类
@SpringBootTest
class DemoMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void testFindAll() {
List<User> list=userMapper.findAll();
System.out.println(list);
}
}
3. 控制台结果:
使用xml配置的方式
1.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.UserXMLMapper">
<select id="findAll" resultType="user">
select * from t_user;
</select>
</mapper>
2.application.yml配置
加上扫描
mybatis:
mapper-locations: classpath:Mapper/*Mapper.xml #mapper映射文件路径
type-aliases-package: com.example.demo #别名包扫描
3.Test类
@SpringBootTest
class DemoMybatisApplicationTests {
@Autowired
private UserXMLMapper userxmlMapper;
@Test
void testFindAll2() {
List<User> list=userxmlMapper.findAll();
System.out.println(list);
}
}
4.Mapper类
@Mapper
@Repository
public interface UserXMLMapper {
public List<User> findAll();
}
5.控制台结果打印
同上(没错我懒了)
5. 期间遇到的错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.example.demo.DemoMybatisApplicationTests’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.example.mapper.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
解决方法:
扫描的时候只能扫描主程序入口同级包或者是其子包。
2.测试通过,但是打印出来的结果为null
这位大佬解决了~
https://blog.csdn.net/SiuMu_/article/details/105130739