springboot_total

在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/b5
在这里插入图片描述

pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
spring:
  profiles: dev

server:
  port: 8081
# 应用名称
spring.application.name=springboot-total

# THYMELEAF (ThymeleafAutoConfiguration)
# 开启模板缓存(默认值: 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"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值