最近在学习SpringBoot,SpringBoot是整合了spring的技术的一个框架。写了一个小demo整合一下Redis和Jpa。
SpringBoot使用maven来进行构建项目,需要依赖什么jar包可以在pom.xml写相应的依赖包的地址。
新建maven项目,然后在pom.xml填写依赖包的信息。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </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-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--AOP--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- springboot整合redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <version>2.0.0.RELEASE</version> </dependency> </dependencies>
maven会根据填写的信息,来下载依赖。
创建springboot启动类
@SpringBootApplication public class LearnBootApplication { public static void main(String[] args) { SpringApplication.run(LearnBootApplication.class); } }
新建一个yml文件,来配置项目。
#运行的端口号 server: port: 8888 #mysql连接信息 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #jpa的配置。 jpa: hibernate: ddl-auto: update show-sql: true #日期格式。 jackson: date-format: yyyy-MM-dd HH:mm:ss joda-date-time-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
然后新建User实体类。
@Entity public class User implements Serializable { @Id @GeneratedValue private Integer id; private Integer age; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } 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; } @Override public String toString() { return "User{" + "id=" + id + ", age=" + age + ", userName='" + userName + '\'' + ", password='" + password + '\'' + '}'; } }
然后新建UserRepository.java来继承JpaRepository,因为JPA封装好了很多的常用操作,直接调用就可以使用。
public interface UserRepository extends JpaRepository<User,Integer> { List<User> findByAge(Integer age); }
findByAge()方法是根据传递的age参数来进行查询。
然后写controller来测试JPA的增删查改和redis取值和存值。
@Autowired是依赖注入,可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。
@RestController=@Controller + @ResponseBody 对类进行一个controller声明,并且返回json格式。
@RestController @RequestMapping("user") public class UserController { @Autowired private UserRepository userRepository; @Autowired private RedisTemplate<String,String> redisTemplate; @PostMapping("/insert") public User InsertUser(@RequestParam(value = "age") Integer age, @RequestParam(value = "password") String password, @RequestParam(value = "userName") String userName){ User user=new User(); user.setAge(age); user.setPassword(password); user.setUserName(userName); userRepository.save(user); return user; } @DeleteMapping("/delete/{id}") public String DeleteByid(@PathVariable(value = "id")Integer id){ userRepository.deleteById(id); return "已删除id="+id+"的数据"; } @PutMapping("/update/{id}") public User UpdateUserById(@PathVariable(value = "id")Integer id, @RequestParam(value = "age",required = false) Integer age, @RequestParam(value = "password",required = false) String password, @RequestParam(value = "userName",required = false) String userName) { User user=new User(); user.setId(id); user.setAge(age); user.setPassword(password); user.setUserName(userName); userRepository.saveAndFlush(user); return user; } @GetMapping("/findUserByAge/{age}") public List<User> findUserByAge(@PathVariable("age")Integer age){ List<User> user = userRepository.findByAge(age); return user; } /*** * 测试redis取值与存值 * * */ @RequestMapping("/redisTest") public String redisTest() throws JsonProcessingException { String msg=""; ObjectMapper objectMapper=new ObjectMapper(); String Json=redisTemplate.boundValueOps("BootRedis").get(); if (Json!=null){ msg=("从redis中取值"+Json); } else{ String Json1=objectMapper.writeValueAsString(userRepository.findAll()); redisTemplate.boundValueOps("BootRedis").set(Json1); msg=("从mysql获取值"+Json1); } return msg; } }
本文gitlub代码地址:https://github.com/lsxaizk/learn_springboot_01