标签:text category 部署 users control column odi rda down
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost/springboot?userUnicode=true@characterEncoding=UTF8&serverTimezone=UTC
package com.willam.domain;
import javax.persistence.*;
/**
* @author :lijunxuan
* @date :Created in 2019/6/28 17:04
* @description :
* @version: 1.0
*/
@Table(name = "user")
@Entity
public class User {
@Id //ID代表是主键
@GeneratedValue(strategy = GenerationType.IDENTITY) //按照主键自增
private Integer id;
//@Column(name="username") //把数据库里的名字和当前名字做一个绑定关系
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;
}
}
package com.willam.Controller;
import com.willam.Service.UserService;
import com.willam.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author :lijunxuan
* @date :Created in 2019/6/28 17:02
* @description :
* @version: 1.0
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
UserService userService;
/**
* 查询所有用户
* @return
*/
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
/**
* 通过ID查询用户
* @param id
* @return
*/
@RequestMapping("/findById")
public User findById(Integer id){
return userService.findById(id);
}
/**
* 添加用户
* @param user
*
*/
@RequestMapping("/add")
public void add(User user){
userService.add(user);
}
/**
* 通过用户ID删除用户
* @param id
*
*/
@RequestMapping("/deleteById")
public void deleteById(Integer id){
userService.deleteById(id);
}
}
package com.willam.Service;
import com.willam.domain.User;
import java.util.List;
public interface UserService {
/**
* 查询所有用户信息
* @return
*/
List<User> findAll();
/**
* 通过ID查询用户
* @param id
* @return
*/
User findById(Integer id);
/**
* 添加用户
* @param user
*
*/
void add(User user);
/**
* 通过用户ID删除用户
* @param id
*
*/
void deleteById(Integer id);
}
package com.willam.Service.impl;
import com.willam.Dao.UserDao;
import com.willam.Service.UserService;
import com.willam.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
/**
* @author :lijunxuan
* @date :Created in 2019/6/28 17:27
* @description :
* @version: 1.0
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public User findById(Integer id) {
Optional<User> userById = userDao.findById(id);
return userById.get();
}
@Override
public void add(User user) {
userDao.save(user);
}
@Override
public void deleteById(Integer id) {
userDao.deleteById(id);
}
}
package com.willam.Dao;
import com.willam.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends JpaRepository<User,Integer> {
}
<!--redis起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
# Redis 配置(不填也是可以的)
spring.redis.host=localhost
spring.redis.port=6379
@Resource
RedisTemplate redisTemplate;
@RequestMapping("/findAll")
public List<User> findAll(){
/*如果第一次查询,redis缓存没有数据,就从数据库中获取
如果有缓存数据,那么从缓存中获取
*/
String users = (String)redisTemplate.boundValueOps("userDao.findAll").get();
if (users==null){
//数据库查询
List<User> all=userService.findAll();
users = all.toString();
redisTemplate.boundValueOps("userDao.findAll").set(users); //把当前的数据缓存
}
return userService.findAll();
}
打一个断点
地址栏 发送请求 http://localhost:8080/user/findAll
后台
redis中有缓存则直接走 return userService.findAll();
package com.willam.Utils;
import com.willam.Service.UserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
/**
* @author :lijunxuan
* @date :Created in 2019/6/28 19:18
* @description :
* @version: 1.0
*/
@Component //把当前的类加入spring容器
public class Timer {
UserService userService;
/**
* 定时任务的配置
* corn 表达式
* fixedDelay 固定的延迟时间执行,上一个任务执行完成,多久之后下一个任务执行。
* rateDelay 固定频率执行,每隔固定时间执行任务
*/
@Scheduled(fixedRate = 2000)
public void task(){
System.out.println(new Date());
System.out.println(LocalDateTime.now());
}
}
每隔2秒执行一次
<!--springBoot的测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
package com.willam;
import com.willam.Service.UserService;
import com.willam.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest //SpringBoot测试类
public class SpringbootJpaApplicationTests {
@Resource
UserService userService;
@Test
public void contextLoads() {
List<User> userList = userService.findAll();
System.out.println(userList);
}
}
在cmd切换到项目的路径
如:H:\workspace\IdeaProject\SpringBoot\day01\day01_springboot_jpa
启动命令:
java -jar target/day01_springboot_jpa-0.0.1-SNAPSHOT.jar
启动命令的时候配置jvm参数也是可以的。然后查看一下Java的参数配置结果
java -Xmx80m -Xms20m -jar target/day01_springboot_demo01-1.0-SNAPSHOT.jar
<packaging>war</packaging>
//web.xml
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
标签:text category 部署 users control column odi rda down
原文地址:https://www.cnblogs.com/vana/p/11772656.html