经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot整合Spring Data JPA过程解析
来源:jb51  时间:2019/10/8 9:02:58  对本文有异议

Spring Boot整合Spring Data JPA

1)加入依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-jpa</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>mysql</groupId>
  7. <artifactId>mysql-connector-java</artifactId>
  8. <scope>runtime</scope>
  9. </dependency>

2)增加配置(application.properties)

  1. server.port=8080
  2. server.servlet.context-path=/
  3.  
  4. # database configuration
  5. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  6. spring.datasource.url=jdbc:mysql://localhost:3306/blog?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
  7. spring.datasource.username=root
  8. spring.datasource.password=123
  9.  
  10. # jpa configuration
  11. # 更新或者创建数据库表结构
  12. spring.jpa.hibernate.ddl-auto=update
  13. # 控制台打印sql语句
  14. spring.jpa.show-sql=true
  15. spring.jpa.open-in-view=false
  16.  
  17. # log configuration
  18. logging.level.root=info

3)编写一个实体类(bean)和数据表进行映射,并且配置好映射关系

  1. import javax.persistence.Column;
  2. import javax.persistence.Entity;
  3. import javax.persistence.GeneratedValue;
  4. import javax.persistence.GenerationType;
  5. import javax.persistence.Id;
  6. import javax.persistence.Table;
  7. import javax.validation.constraints.NotBlank;
  8.  
  9. /**
  10. * 使用JPA注解配置映射关系
  11. * Created by zxf on 2019年9月30日
  12. */
  13. @Entity // 告诉JPA这是一个实体类(和数据库映射的类)
  14. @Table(name = "t_type") // @Table来指定和哪个数据表对应,如果省略默认表名就是类名首字母小写
  15. public class Type {
  16. @Id // 表明这是一个主键
  17. @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增主键
  18. private Long id;
  19.  
  20. @Column(name = "last_name", length = 50) // 这是和数据表对应的一个列,省略默认列名就是属性名
  21. private String name;
  22. }

4)编写一个Dao接口来操作实体类对应的数据表

  1. import org.springframework.data.jpa.repository.JpaRepository;
  2.  
  3. /**
  4. * Created by zxf on 2019年10月1日
  5. */
  6. // 第一个泛型表示操作的类是Type,第二个泛型Long表示Type的主键id为Long类型
  7. public interface TypeRepository extends JpaRepository<Type, Long> {
  8. // 定义自己的方法
  9. Type findTypeByName(String name);
  10. }

5)service层调用测试

  1. import java.util.List;
  2. import java.util.Optional;
  3.  
  4. import javax.transaction.Transactional;
  5.  
  6. import org.springframework.beans.BeanUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.data.domain.Page;
  9. import org.springframework.data.domain.Pageable;
  10. import org.springframework.stereotype.Service;
  11.  
  12. import com.fei.NotFoundException;
  13. import com.fei.po.Type;
  14. import com.fei.repository.TypeRepository;
  15. import com.fei.service.TypeService;
  16.  
  17. /**
  18. * Created by zxf on 2019年10月1日
  19. */
  20. @Service
  21. @Transactional
  22. public class TypeServiceImpl implements TypeService {
  23.  
  24. @Autowired
  25. private TypeRepository typeRepository;
  26.  
  27. /**
  28. * 保存一个分类
  29. *
  30. * @param type
  31. * @return
  32. */
  33. @Override
  34. public Type saveType(Type type) {
  35. return typeRepository.save(type);
  36. }
  37.  
  38. /**
  39. * 根据id获得一个分类对象
  40. *
  41. * @param id
  42. * @return
  43. */
  44. @Override
  45. public Type getType(Long id) {
  46. return typeRepository.findById(id).get();
  47. }
  48.  
  49. /**
  50. * 根据分页参数查询一个分类列表
  51. *
  52. * @param pageable
  53. * @return
  54. */
  55. @Override
  56. public Page<Type> listType(Pageable pageable) {
  57. return typeRepository.findAll(pageable);
  58. }
  59.  
  60. /**
  61. * 更新分类
  62. *
  63. * @param id
  64. * @param type
  65. * @return
  66. */
  67. @Override
  68. public Type updateType(Long id, Type type) {
  69. Type t = typeRepository.findById(id).get();
  70.  
  71. if (t == null) {
  72. throw new NotFoundException("类型不存在");
  73. }
  74. BeanUtils.copyProperties(type, t);
  75.  
  76. return typeRepository.save(t);
  77. }
  78.  
  79. /**
  80. * 根据id删除一个分类
  81. *
  82. * @param id
  83. */
  84. @Override
  85. public void deleteType(Long id) {
  86. typeRepository.deleteById(id);
  87. }
  88.  
  89. /**
  90. * 根据名字查询一个分类对象
  91. *
  92. * @param name
  93. * @return
  94. */
  95. @Override
  96. public Type getTypeByName(String name) {
  97. return typeRepository.findTypeByName(name);
  98. }
  99.  
  100. /**
  101. * 不带参数的查询所有分类
  102. *
  103. * @return
  104. */
  105. @Override
  106. public List<Type> listType() {
  107. return typeRepository.findAll();
  108. }
  109.  
  110. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号