- 整合
- 整合Junit
- 1.Test类:
- 2.UserService类:
- 3.控制台显示:
- 整合redis
- 1. 新建项目
- 2. redis启动
- 3. Test类:
- 4. 启动redis测试类
- 整合MyBatis
- 1.新建项目
- 2. User类:
- 3. 配置
- 4. 数据库表
- 使用注解Select的方式
- 1.Mapper类:
- 2. Test类
- 3. 控制台结果:
- 使用xml配置的方式
- 1.xml配置文件
- 2.application.yml配置
- 3.Test类
- 4.Mapper类
- 5.控制台结果打印
- 5. 期间遇到的错误
- 解决方法:
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add() {
System.out.println("add---");
}
}
3.控制台显示:
整合redis
1. 新建项目
首先要在新建时勾选NoSQL里的Spring Data 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会在控制台成功打印名字的值。
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 findAll();
}
2. Test类
@SpringBootTest
class DemoMybatisApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void testFindAll() {
List list=userMapper.findAll();
System.out.println(list);
}
}
3. 控制台结果:
使用xml配置的方式
1.xml配置文件
2.application.yml配置select * from t_user;
加上扫描
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 list=userxmlMapper.findAll();
System.out.println(list);
}
}
4.Mapper类
@Mapper
@Repository
public interface UserXMLMapper {
public List 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)}
扫描的时候只能扫描主程序入口同级包或者是其子包。



