时间:2021-07-01 10:21:17 帮助过:18人阅读
3.配置application.yml及application-test.yml
application.yml: (需注意配置格式)
spring: profiles: active: test datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password: 123 jpa: database: MYSQL hibernate: ddl-auto: update show-sql: true jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: UTC
application-test.yml:
spring: devtools: restart: enabled: true additional-paths: src/main/java
我们的实体User类 :
package com.example.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY)//自增 private Integer id; private String name; private String passWord; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
到这里,我们启动项目,然后就会自动在数据库中生成对应的t_user表
4.创建controller,service,dto做一些增删改查的操作
a.controller:
package com.example.demo.controller; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.demo.entity.User; import com.example.demo.service.IUserService; @RestController public class UserController { @Autowired IUserService userService; @GetMapping(value="/getUserById") public Optional<User> findUserById(@RequestParam("Id") Integer id){ return userService.findUserById(id); } @GetMapping("/all") public List<User> findAll(){ return userService.findAll(); } @PostMapping("/update") public User updateUser(@RequestParam("Id") Integer id,@RequestParam("name") String name,@RequestParam("passWord") String passWord) { return userService.updateUserById(id, name, passWord); } @PutMapping("/insert") public User insertUser(@RequestParam("name") String name,@RequestParam("passWord") String passWord) { return userService.insertUser(name, passWord); } @DeleteMapping("/delete") public void deleteUser(@RequestParam("Id") Integer id) { userService.deleteUserById(id); } }
b.service:
package com.example.demo.service; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.demo.dto.IUserDto; import com.example.demo.entity.User; @Service public class UserServiceImpl implements IUserService{ @Autowired IUserDto iUserDto; @Override public Optional<User> findUserById(Integer id) { return iUserDto.findById(id); } @Override public User updateUserById(Integer id,String name, String passWord) { User user = new User(); user.setId(id); user.setName(name); user.setPassWord(passWord); return iUserDto.save(user); } @Override public User insertUser(String name, String passWord) { User user = new User(); user.setName(name); user.setPassWord(passWord); return iUserDto.save(user); } @Override public void deleteUserById(Integer id) { iUserDto.deleteById(id); } @Override public List<User> findAll() { return iUserDto.findAll(); } } c.dto: package com.example.demo.dto; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import com.example.demo.entity.User; public interface IUserDto extends JpaRepository<User,Integer>{//默认传的是ID为条件,如果需要其他条件来进行CRDU操作可通过增加方法来实现 public List<User> findByName(String name);//方法名不能乱写,要按你需要的条件来写 findBy你的字段名 }
5.测试:
这里采用火狐的RESTClient
a.put用法
b.delete用法
c.post用法
d.Get用法
---------------------
作者:北半球先生
来源:CSDN
原文:https://blog.csdn.net/sinat_22808389/article/details/81592642
SpringBoot系列之——整合JPA、mysql
标签:enabled 掌握 for 用法 不能 list return ssl 定义