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