时间:2021-07-01 10:21:17 帮助过:6人阅读
yml文件
server: port: 8081 spring: application: name: pomit datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/boot?characterEncoding=utf8&serverTimezone=UTC username: root password: sugar888 type: org.apache.commons.dbcp.BasicDataSource dbcp2: max-wait-millis: 60000 min-idle: 20 initial-size: 2 validation-query: SELECT 1 test-while-idle: true test-on-borrow: true test-on-return: false jpa: database: mysql show-sql: true hibernate: ddl-auto: none naming: implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
dto
package com.blb.dto; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "user_role") public class UserRole { @Id private int id; @Column(name = "user_name") private String userName; private String role; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
dao
package com.blb.dao; import com.blb.dto.UserRole; import org.springframework.data.repository.CrudRepository; import java.util.List; public interface UserRoleDao extends CrudRepository<UserRole,Integer> { List<UserRole> findByRole(String role); }
service
package com.blb.service; import com.blb.dao.UserRoleDao; import com.blb.dto.UserRole; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Service public class UserRoleService { @Autowired private UserRoleDao userRoleDao; public List<UserRole> selectAll() { return (List<UserRole>)userRoleDao.findAll(); } public void saveTest(UserRole userRole) { userRoleDao.save(userRole); } @Transactional public void update(Integer id,String phone) { UserRole userRole = userRoleDao.findById(id).orElse(null); if(userRole != null) { userRole.setPhone(phone); userRoleDao.save(userRole); } } @Transactional public void delete(Integer id) { userRoleDao.deleteById(id); } public List<UserRole> findByRole(String role) { return userRoleDao.findByRole(role); } }
controller
package com.blb.controller; import com.blb.dto.UserRole; import com.blb.service.UserRoleService; 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; @RestController @RequestMapping("/db") public class TestJpaRest { @Autowired private UserRoleService userRoleService; @RequestMapping(value = "/query") public List<UserRole> query() { return userRoleService.selectAll(); } @RequestMapping("/testFind") public List<UserRole> testFind() { return userRoleService.findByRole("admin"); } @RequestMapping("/save") public String save() { UserRole userRole = new UserRole(); userRole.setRole("Test"); userRole.setUserName("Test"); userRole.setPhone("159611"); userRoleService.saveTest(userRole); return "0000"; } @RequestMapping("/update") public String update() { userRoleService.update(4,"7899"); return "0000"; } @RequestMapping("/delete") public String delete() { userRoleService.delete(5); return "0000"; } }
启动器
package com.blb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootJpaApplication { public static void main(String[] args) { SpringApplication.run(SpringbootJpaApplication.class, args); } }
使用spring-data-jpa操作数据库crud
标签:web nta this figure prope mon admin hiberna sql