在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

spring boot jpa

2016-7-29 15:47| 发布者: zhangjf| 查看: 633| 评论: 0

摘要: spring boot 微型框架越来越流行,spring的顶级项目。boot微型框架讲究的使零配置。与XML配置说拜拜。一分钟创建的web服务。也是已经占市场35%。 这也足以说明 spring boot 的魅力所在。此列子基于 boot jpa 。 ...
spring boot 微型框架越来越流行,spring的顶级项目。boot微型框架讲究的使零配置。与XML配置说拜拜。一分钟创建的web服务。也是已经占市场35%。 这也足以说明 spring boot 的魅力所在。此列子基于 boot jpa 。
  1. /**maven依赖*/
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-jpa</artifactId>
  5. <version>1.2.3.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <version>5.1.36</version>
  11. </dependency>
  12. /**application.properties*/
  13. spring.datasource.url = jdbc:mysql://localhost:3306/test
  14. spring.datasource.username = root
  15. spring.datasource.password = tiger
  16. spring.datasource.driverClassName = com.mysql.jdbc.Driver
  17. # Show or not log for each sql query
  18. spring.jpa.show-sql =true
  19. # Hibernate ddl auto (create, create-drop, update)
  20. spring.jpa.hibernate.ddl-auto =update
  21. # Naming strategy
  22. spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  23. # stripped before adding them to the entity manager)
  24. spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  25. /**实体*/
  26. package com.tian.entity;
  27. import java.io.Serializable;
  28. import javax.persistence.Entity;
  29. import javax.persistence.GeneratedValue;
  30. import javax.persistence.GenerationType;
  31. import javax.persistence.Id;
  32. import javax.persistence.Table;
  33. import javax.validation.constraints.NotNull;
  34. @Entity
  35. @Table(name = "t_user")
  36. public class User implements Serializable {
  37. private static final long serialVersionUID = 1L;
  38. @Id
  39. @GeneratedValue(strategy = GenerationType.AUTO)
  40. private long id;
  41. // The user email
  42. @NotNull
  43. private String email;
  44. // The user name
  45. @NotNull
  46. private String name;
  47. public long getId() {
  48. return id;
  49. }
  50. public void setId(long id) {
  51. this.id = id;
  52. }
  53. public String getEmail() {
  54. return email;
  55. }
  56. public void setEmail(String email) {
  57. this.email = email;
  58. }
  59. public String getName() {
  60. return name;
  61. }
  62. public void setName(String name) {
  63. this.name = name;
  64. }
  65. }
  66. /**接口*/
  67. package com.tian.dao;
  68. import org.springframework.data.repository.CrudRepository;
  69. import org.springframework.transaction.annotation.Transactional;
  70. import com.tian.entity.User;
  71. @Transactional
  72. public interface UserRepository extends CrudRepository<User, Long>{
  73. public User findByEmail(String email);
  74. }
  75. /**控制器*/
  76. package com.tian.boot;
  77. import org.springframework.beans.factory.annotation.Autowired;
  78. import org.springframework.beans.factory.annotation.Qualifier;
  79. import org.springframework.boot.SpringApplication;
  80. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  81. import org.springframework.boot.autoconfigure.SpringBootApplication;
  82. import org.springframework.stereotype.Controller;
  83. import org.springframework.web.bind.annotation.RequestMapping;
  84. import org.springframework.web.bind.annotation.ResponseBody;
  85. import com.tian.dao.UserRepository;
  86. import com.tian.entity.User;
  87. @RestController
  88. public class TianController {
  89. @Autowired
  90. private UserRepository userRepository;
  91. @RequestMapping("/tian")
  92. @ResponseBody
  93. public String hello(){
  94. User user=userRepository.findByEmail("zhangtian@163.com");
  95. System.out.println(user);
  96. return "applicationcontext";
  97. }
  98. public static void main(String[] args) {
  99. SpringApplication.run(TianController.class, args);
  100. }
  101. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 13:02

Copyright 2015-2025 djqfx

返回顶部