
# 开启模板缓存(默认值: true )
spring.thymeleaf.cache=false
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/teaching?serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=root
# 应用服务 WEB 访问端口
server.servlet.context-path=/lb
spring.profiles.active=dev
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
spring.redis.host=127.0.0.1
spring.redis.port=6379
controller
package com.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@RestController
public class MyController {
@RequestMapping("/hello")
public String hello(){
return "hello springboot!";
}
@RequestMapping("/mav")
public ModelAndView mav(){
return new ModelAndView("index");
}
}
entity
package com.example.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Article implements Serializable {
private Integer id;
private String title;
private String content;
}
mapper
package com.example.mapper;
import com.example.entity.Article;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface MyMapper {
//增
@Insert("insert into article(id,title,content) values(#{id},#{title},#{content})")
public Boolean add(Article article);
//修改
@Update("update article set title=#{title},content=#{content} where id=#{id}")
public Boolean update(Article article);
//删
@Delete("delete from article where id=#{id}")
public Boolean del(Integer id);
//查单个
@Select("select * from article where id=#{id}")
public Article findById(Integer id);
//查全部
@Select("select * from article")
public List<Article> getAll();
}
service
package com.example.service;
import com.example.entity.Article;
import com.example.mapper.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MyService {
@Autowired
private MyMapper myMapper;
@Cacheable(cacheNames = "art",unless = "#result==null")
public List<Article> getAll(){
return myMapper.getAll();
}
@CachePut(cacheNames = "art")
public Boolean update(Article article){
return myMapper.update(article);
}
@CacheEvict(cacheNames = "art")
public Boolean del(Integer id){
return myMapper.del(id);
}
}
启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@EnableCaching
@SpringBootApplication
public class SpringbootTotalApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTotalApplication.class, args);
}
}
mappertest
package com.example;
import com.example.entity.Article;
import com.example.mapper.MyMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootTotalApplicationTests {
@Autowired
private MyMapper mapper;
@Test
void add() {
Article article = new Article();
article.setId(3);
article.setTitle("3");
article.setContent("3");
mapper.add(article);
}
@Test
void del(){
mapper.del(11);
}
@Test
void update(){
Article article = new Article();
article.setId(3);
article.setTitle("333");
article.setContent("333");
mapper.update(article);
}
@Test
void findById(){
System.out.println(mapper.findById(3));
}
@Test
void findAll(){
mapper.getAll();
}
}
servicetest
package com.example;
import com.example.entity.Article;
import com.example.service.MyService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class MyServiceCaseTest {
@Autowired
private MyService myService;
@Test
void find(){
myService.getAll();
}
@Test
void upd(){
Article article = new Article();
article.setId(3);
article.setTitle("66");
article.setContent("33355");
myService.update(article);
}
@Test
void del(){
myService.del(2);
}
}
RedisTemplate
package com.example;
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
public class redisTemelateTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
void set(){
redisTemplate.opsForValue().set("name","lb");
}
@Test
void get(){
System.out.println( redisTemplate.opsForValue().get("name"));
}
@Test
void hset(){
redisTemplate.opsForHash().put("lb","age",12);
}
@Test
void hget(){
System.out.println(redisTemplate.opsForHash().get("lb","age"));
}
}