创建工程
- 新建一个springboot工程
目录结构如下
集成 hibernate
-
在pom文件中导入相关依赖
1
2
3
4
5
6
7
8
9
10
11
12
13 1 <!--数据库-->
2 <dependency>
3 <groupId>mysql</groupId>
4 <artifactId>mysql-connector-java</artifactId>
5 <scope>runtime</scope>
6 </dependency>
7 <!--jpa-->
8 <dependency>
9 <groupId>org.springframework.boot</groupId>
10 <artifactId>spring-boot-starter-data-jpa</artifactId>
11 </dependency>
12
13
-
在 application.properties 添加 MySQL 配置 和 jpa 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1# ============================== MySQL配置 =============================================================================
2spring.datasource.url = jdbc:mysql://127.0.0.1:3306/hello?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
3spring.datasource.username = root
4spring.datasource.password = root
5spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
6
7# =================================== JPA ===============================================================================
8# Specify the DBMS
9spring.jpa.database = MYSQL
10# Show or not log for each sql query 是否打印 sql 语句
11spring.jpa.show-sql = true
12# Hibernate ddl auto (create, create-drop, update) 慎用 create ,因为create每次启动都会删除之前的表,再重新创建,数据会丢失
13spring.jpa.hibernate.ddl-auto = update
14# Naming strategy
15spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy
16# stripped before adding them to the entity manager 修改方言
17#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
18spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
19
20
-
创建工具类 Result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 1package com.example.demojpa.util;
2
3public class Result {
4 private int code = 200;
5 private String msg = "操作成功";
6 private Object data;
7
8 public Result(int code,String msg){
9 this.code = code;
10 this.msg = msg;
11 }
12
13 public Result(Object data){
14 this.data = data;
15 }
16
17 public int getCode() {
18 return code;
19 }
20
21 public void setCode(int code) {
22 this.code = code;
23 }
24
25 public String getMsg() {
26 return msg;
27 }
28
29 public void setMsg(String msg) {
30 this.msg = msg;
31 }
32
33 public Object getData() {
34 return data;
35 }
36
37 public void setData(Object data) {
38 this.data = data;
39 }
40}
41
42
43
-
创建实体类 User
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 1package com.example.demojpa.entity;
2
3import javax.persistence.*;
4
5/**
6 * user 实体类
7 */
8@Entity
9@Table(name = "t_user")
10public class User {
11 @Id
12 @GeneratedValue(strategy = GenerationType.AUTO)
13 Integer id;
14
15 @Column(name = "name")
16 String name;
17
18 @Column(name = "age")
19 int age;
20
21 public Integer getId() {
22 return id;
23 }
24
25 public void setId(Integer id) {
26 this.id = id;
27 }
28
29 public String getName() {
30 return name;
31 }
32
33 public void setName(String name) {
34 this.name = name;
35 }
36
37 public int getAge() {
38 return age;
39 }
40
41 public void setAge(int age) {
42 this.age = age;
43 }
44
45 @Override
46 public String toString() {
47 return "User{" +
48 "id=" + id +
49 ", name='" + name + '\'' +
50 ", age=" + age +
51 '}';
52 }
53}
54
55
56
-
创建服务层 UserService
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 1package com.example.demojpa.service;
2
3import com.example.demojpa.entity.User;
4import com.example.demojpa.util.Result;
5
6/**
7 * 接口层:负责逻辑处理访问数据库层
8 */
9public interface UserService {
10 /**
11 * 保存 user 对象
12 * @param user
13 * @return
14 */
15 Result saveUser(User user);
16
17 /**
18 * 查询所有user
19 * @return
20 */
21 Result queryAll();
22
23 /**
24 * 通过name和age查询
25 * @param name
26 * @param age
27 * @return
28 */
29 Result getByNameAndAge(String name,int age);
30}
31
32
33
-
创建实现类 UserServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 1package com.example.demojpa.service.impl;
2
3import com.example.demojpa.dao.UserRepository;
4import com.example.demojpa.entity.User;
5import com.example.demojpa.service.UserService;
6import com.example.demojpa.util.Result;
7import org.springframework.stereotype.Service;
8
9import javax.annotation.Resource;
10import java.util.List;
11
12/**
13 * 实现接口的抽象方法
14 */
15@Service
16public class UserServiceImpl implements UserService {
17 @Resource
18 UserRepository userRepository;
19
20 @Override
21 public Result saveUser(User user) {
22 User save = userRepository.save(user);
23 return new Result(save);
24 }
25
26 @Override
27 public Result queryAll() {
28 List <User> list = userRepository.findAll();
29 return new Result(list);
30 }
31
32 @Override
33 public Result getByNameAndAge(String name, int age) {
34 List <User> list = userRepository.getByNameAndAge(name, age);
35 return new Result(list);
36 }
37}
38
39
40
-
创建 UserRepository 数据访问接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package com.example.demojpa.dao;
2
3import com.example.demojpa.entity.User;
4import org.springframework.data.jpa.repository.JpaRepository;
5import org.springframework.stereotype.Repository;
6
7import java.util.List;
8
9/**
10 * 继承JpaRepository<User,Integer>
11 * User:要操作的表格对象
12 * Integer:主键 ID 的数据类型
13 *
14 * 继承的这个类里面封装了基本的增删改查方法直接调用即可,
15 * 如果集成的方法没法满足需求,可以在此封装,但是一定要注意 “命名规范”
16 */
17@Repository(value = "userRepository")
18public interface UserRepository extends JpaRepository<User,Integer> {
19 List<User> getByNameAndAge(String name,int age);
20}
21
22
23
-
创建 controller 控制器 UserController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 1package com.example.demojpa.controller;
2
3import com.example.demojpa.entity.User;
4import com.example.demojpa.service.UserService;
5import com.example.demojpa.util.Result;
6import org.springframework.beans.factory.annotation.Autowired;
7import org.springframework.web.bind.annotation.GetMapping;
8import org.springframework.web.bind.annotation.PostMapping;
9import org.springframework.web.bind.annotation.RequestBody;
10import org.springframework.web.bind.annotation.RestController;
11
12@RestController
13public class UserController {
14 @Autowired
15 UserService userService;
16
17 /**
18 * 保存user
19 * @param user
20 * @return
21 */
22 @PostMapping("/saveUser")
23 public Result saveUser(@RequestBody User user){
24 return userService.saveUser(user);
25 }
26
27 /**
28 * 查询所有user
29 * @return
30 */
31 @GetMapping("/queryAll")
32 public Result queryAll(){
33 return userService.queryAll();
34 }
35
36 /**
37 * 通过name和age查询
38 * @param name
39 * @param age
40 * @return
41 */
42 @GetMapping("/getByNameAndAge")
43 public Result getByNameAndAge(String name,int age){
44 return userService.getByNameAndAge(name,age);
45 }
46}
47
48
49
- 最后直接使用postman测试一下就好了,在这就不贴效果了,总的目录结构
总结
hibernate配置默认会自动创建表格,但是不是创建数据库,如果自动创建表失败,可能是方言设置有问题,ddl-auto = update有几种值,(create, create-drop, update),create相关的慎用,因为每次启动都会清空表。还有一定要注意 hibernate 命名规范。