1.快速创建springboot项目
2.下载启动redis的windows版本
3.整合mybatis+redis+junit
访问地址:https://start.spring.io
或者idea,new 项目,选择 spring initializr 快速创建项目;
下载redis,地址:Redis 教程 | 菜鸟教程 (runoob.com)
启动server
创建各种项目文件,目录:
1.pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ligy</groupId> <artifactId>springbootstudy</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springbootstudy</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</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.2.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.properties
#mysql配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/school?useunicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root #mybatis配置 mybatis.type-aliases-package=com.ligy.springbootstudy.model mybatis.mapper-locations=classpath:mapper/*.xml #reds配置 spring.redis.host=127.0.0.1 spring.redis.port=6379
3.mapper对应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.ligy.springbootstudy.mapper.StudentMapper"> <select id="getList" resultType="com.ligy.springbootstudy.model.Student"> select * from Student </select> <insert id="insert" parameterType="student" keyProperty="id" useGeneratedKeys="true"> insert into Student(name) values('${name}') </insert> </mapper>
4.mapper接口
package com.ligy.springbootstudy.mapper; import com.ligy.springbootstudy.model.Student; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface StudentMapper { List<Student> getList(); boolean insert(Student student); }
5.model文件
package com.ligy.springbootstudy.model; public class Student { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
6.service层接口+实现类
package com.ligy.springbootstudy.service; import com.ligy.springbootstudy.model.Student; import java.util.List; public interface StudentService { List<Student> getList(); boolean insert(Student student); }
package com.ligy.springbootstudy.service.impl; import com.ligy.springbootstudy.mapper.StudentMapper; import com.ligy.springbootstudy.model.Student; import com.ligy.springbootstudy.service.StudentService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Resource StudentMapper studentMapper; @Override public List<Student> getList() { return studentMapper.getList(); } @Override public boolean insert(Student student) { return studentMapper.insert(student); } }
7.springboot启动入口
package com.ligy.springbootstudy; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.ligy.springbootstudy.mapper") public class SpringbootstudyApplication { public static void main(String[] args) { SpringApplication.run(SpringbootstudyApplication.class, args); } }
8.单元测试(redis + mybatis 查询和插入)
package com.ligy.springbootstudy; import com.ligy.springbootstudy.model.Student; import com.ligy.springbootstudy.service.StudentService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import java.util.List; @SpringBootTest class SpringbootstudyApplicationTests { @Resource StudentService studentService; @Test void test2() { Student student = new Student(); student.setName("jack100"); boolean t = studentService.insert(student); if (t) { System.out.println("插入成功"+student); } else { System.out.println("插入失败"); } } @Test void test1() { List<Student> list = studentService.getList(); System.out.println(list); } }
package com.ligy.springbootstudy; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ligy.springbootstudy.model.Student; import com.ligy.springbootstudy.service.StudentService; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; @SpringBootTest public class RedisTest { @Resource RedisTemplate<String, Object> redisTemplate; @Resource StudentService studentService; @Test void test2() { if (redisTemplate.hasKey("student-list")) { String str = redisTemplate.boundValueOps("student-list").get().toString(); JSONArray array = JSON.parseArray(str); List<Student> list = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { String t = array.get(i).toString(); Student student = JSONObject.parseObject(t, Student.class); System.out.println("从redis拿数据:" + student.getName()); list.add(student); } System.out.println("从redis拿数据:" + list); } else { List<Student> list = studentService.getList(); redisTemplate.boundValueOps("student-list").set(JSON.toJSONString(list)); System.out.println("从数据库拿数据:" + list); } } @Test void test1() { redisTemplate.boundValueOps("name").set("{id:1,name:'jack'}"); Object name = redisTemplate.boundValueOps("name").get(); Object parse = JSON.parse(name.toString()); System.out.println("redis:name:" + parse); } }