Spring Boot与Spring Data JPA
0、编辑properties
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
1、创建实体类bean
package com.example.bean; import javax.persistence.*; @Table(name="grade") @Entity public class Grade { @Id @GeneratedValue(strategy = GenerationType.AUTO)//id和GeneratedValue必须同时使用 private int id; private String gradeNM; private int teacherID; @Override public String toString() { return "Grade{" + "id=" + id + ", gradeNM='" + gradeNM + '\'' + ", teacherID=" + teacherID + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getGradeNM() { return gradeNM; } public void setGradeNM(String gradeNM) { this.gradeNM = gradeNM; } public int getTeacherID() { return teacherID; } public void setTeacherID(int teacherID) { this.teacherID = teacherID; } }
2、创建repository
1)CrudRepository
package com.example.reposittory; import com.example.bean.Grade; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; public interface GradeCrudRepository extends CrudRepository<Grade,Integer> { public Grade findByGradeNM(String gradeNM); @Query(value = "from Grade b where gradeNM=:nm") public Grade findMyGrade(@Param("nm") String gradeNM); }
2)Repository
package com.example.reposittory; import com.example.bean.Grade; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; public interface GradeRepository extends Repository<Grade,Integer> { public Grade findByGradeNM(String gradeNM); @Query(value = "from Grade b where gradeNM=:nm") public Grade findMyGrade(@Param("nm") String gradeNM); }
3)PagingAndSortingRepository
package com.example.reposittory; import com.example.bean.Grade; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; public interface GradePageAndSortRepository extends PagingAndSortingRepository<Grade,Integer> { Grade findByGradeNM(String gradeNM); @Query(value = "from Grade b where gradeNM=:nm") Grade findMyGrade(@Param("nm") String gradeNM); }
3、创建service
package com.example.service; import com.example.bean.Grade; import com.example.reposittory.GradeRepository; import com.example.reposittory.GradeCrudRepository; import org.springframework.data.domain.PageRequest; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.transaction.Transactional; @Service public class GradeService { @Resource private GradeRepository gradeRepository; @Resource private GradeCrudRepository gradeRepository2; @Resource private PagingAndSortingRepository gradeRepository3; public Grade findByGradeNM(String gradeNM){ Grade byGradeNm = gradeRepository.findByGradeNM(gradeNM); return byGradeNm; } public Grade findMyGrade(String gradeNM){ Grade byGradeNm = gradeRepository.findMyGrade(gradeNM); return byGradeNm; } @Transactional public void save(Grade grade){ gradeRepository2.save(grade); } @Transactional public void delete(int id){ gradeRepository2.delete(id); } public Iterable<Grade> getAll(){ return gradeRepository3.findAll(new PageRequest(0,3)); } }
4、创建control
package com.example.controller; import com.example.bean.Grade; import com.example.service.GradeService; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @RestController public class GradeController { @Resource private GradeService gradeService; @RequestMapping("/findbygradeNM") public Grade findByGradeNM(String gradeNM) { Grade byGradeNM = gradeService.findByGradeNM(gradeNM); return byGradeNM; } @RequestMapping("/findmygradeNM") public Grade findMyGrade(String gradeNM) { Grade byGradeNM = gradeService.findMyGrade(gradeNM); return byGradeNM; } @RequestMapping("/save") public String save(@RequestParam("gradeNM") String gradeNM, @RequestParam("id") int id) { Grade grade = new Grade(); grade.setGradeNM(gradeNM); grade.setTeacherID(id); gradeService.save(grade); return "success"; } @RequestMapping("/delete") public String delete(int id) { gradeService.delete(id); return "success"; } @RequestMapping(value = "/getAll",method = RequestMethod.GET) public @ResponseBody List<Grade> getAll() { Iterable<Grade> all = gradeService.getAll(); Iterator<Grade> iterator = all.iterator(); List<Grade> grades = new ArrayList<>(); while (iterator.hasNext()) { Grade grade = iterator.next(); grades.add(grade); } return grades; } }
5、spring boot启动入口
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HctSpringBootApplication { public static void main(String[] args) { SpringApplication.run(HctSpringBootApplication.class, args); } }
6、测试
1)查询所有
2)保存数据