pom.xml
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>myspringboot-demo03</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
redis:
host: localhost
port: 6379
mybatis:
mapper-locations: classpath:mappers/*Mapper.xml
type-aliases-package: com.lichun.pojo
UserMapper.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.lichun.mapper.UserMapper">
<select id="findAllUser" resultType="com.lichun.pojo.User">
select * from user
</select>
<select id="findById" resultType="com.lichun.pojo.User">
select * from user where id=#{id}
</select>
</mapper>
User.java
package com.lichun.pojo;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private String name;
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
UserController.java
package com.lichun.controller;
import com.lichun.pojo.User;
import com.lichun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/findAllUser")
public List<User> findAllUser() {
return userService.findAllUser();
}
@RequestMapping("/{id}")
public User findById(@PathVariable(name = "id") Integer id) {
return userService.findById(id);
}
}
UserService.java
package com.lichun.service;
import com.lichun.pojo.User;
import java.util.List;
public interface UserService {
public String getUser();
public List<User> findAllUser();
public User findById(Integer id);
}
UserServiceImpl.java
package com.lichun.service.impl;
import com.lichun.mapper.UserMapper;
import com.lichun.pojo.User;
import com.lichun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List<User> findAllUser() {
List<User> list;
list = (List<User>) redisTemplate.boundValueOps("key_all").get();
if (list == null || list.size() <= 0) {
list = userMapper.findAllUser();
redisTemplate.boundValueOps("key_all").set(list);
}
return list;
}
@Override
public User findById(Integer id) {
return userMapper.findById(id);
}
public String getUser() {
System.out.println("返回用户信息");
return "xinglibao";
}
}
UserMapper.java
package com.lichun.mapper;
import com.lichun.pojo.User;
import java.util.List;
public interface UserMapper {
public List<User> findAllUser();
public User findById(Integer id);
}
SpringBootApplicationTest.java
package com.lichun;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.lichun.mapper")
public class SpringBootApplicationTest {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationTest.class, args);
}
}
test.java
package com.lichun.test;
import com.lichun.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
@Autowired
private UserService userService;
@Test
public void test01() {
String user = userService.getUser();
System.out.println(user);
}
}
|