Spring boot 与 mybatis-plus
-
建立Spring boot项目
添加依赖
-
连接数据库
-
生成mapper、service、xml等文件
应该是com.example.demo 写错了写成了/
文件已经生成好!
4. 导入plus包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
- 链接数据库 配置文件application.yml
spring:
datasource: #数据源
url: jdbc:mysql://localhost:3306/student
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
- 测试
package com.example.demo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.model.Stu;
import com.example.demo.service.StuService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.lang.model.element.VariableElement;
@SpringBootTest
class Demo1ApplicationTests {
@Autowired
StuService service;
@Test
public void demo(){
Stu stu = service.getOne(new QueryWrapper<Stu>().eq("id", 1));
System.out.println(stu);
boolean save = service.save(stu);
}
}
成功!!!
Spring boot 与 Redis
- 导入redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 建立redis配置文件,防止乱码
package com.zaz.config;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置类
*
* @program: springbootdemo
*/
//@EnableCaching //开启注解
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
//key 的序列化
template.setKeySerializer(new StringRedisSerializer());
//value 的序列化
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//hash类型 key
template.setHashKeySerializer(new StringRedisSerializer());
//value
template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//注入连接工厂
template.setConnectionFactory(factory);
return template;
}
}
- 链接Redis 配置文件application.yml
spring:
redis:
host: localhost
port: 6379
# jedis:
# pool:
# max-wait: 30000 #最大阻塞等待时间
# max-active: 100 #连接池最大连接数
# max-idle: 20 #连接池中最大空闲连接
# min-idle: 0
# timeout: 3000 #连接超时
- 测试
package com.zaz.user.service;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
/**
* @author
* @create
*/
@Slf4j
@SpringBootTest
public class RedisTest {
@Resource
RedisTemplate redisTemplate;
@Test
public void RedisTest(){
redisTemplate.opsForValue().set("index","就是个索引哦!!!!!");
Object index = redisTemplate.opsForValue().get("index");
log.info(String.valueOf(index));
}
}
运行成功!!!
启动类要加上
@MapperScan("com.example.demo.mapper")