Hibernate是什么?
Hibernate与MyBatis都是流行的持久层开发框架,只不过Hibernate是全自动ORM框架,不需要关心sql编写。
Hibernate 是一个高性能的对象/关系映射(ORM)持久化存储和查询的服务,不仅负责从Java类到数据库表的映射 (还包括从Java数据类型到SQL数据类型的映射),还提供了面向对象的数据查询检索机制,从而极大地缩短了手动处理SQL和JDBC上的开发时间。 同时,Hibernate还实现了JPA规范,在SpringBoot中,JPA的默认实现就是使用的Hibernate。
hibernate和mybatis的区别?
1.hibernate是全自动,而mybatis是半自动。 2. hibernate数据库移植性远大于mybatis。 3. hibernate拥有完整的日志系统,mybatis则欠缺一些。 4. mybatis相比hibernate需要关心很多细节。 5. sql直接优化上,mybatis要比hibernate方便很多。
实践操作
1.创建一个springboot项目(勾选两个依赖) 2.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>
3.在application.properties文件中加入配置
# ============================== MySQL配置 =============================================================================
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/user?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
4.创建一个返回工具类result
package com.cst.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;
}
}
5.创建实体类user
package com.cst.entity;
import javax.persistence.*;
@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 +
'}';
}
}
6.创建dao层接口UserRepository
package com.cst.dao;
import com.cst.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository(value = "userRepository")
public interface UserRepository extends JpaRepository<User,Integer> {
List<User> getByNameAndAge(String name,int age);
}
7.创建service接口
package com.cst.service;
import com.cst.entity.User;
import com.cst.util.Result;
public interface UserService {
Result saveUser(User user);
Result queryAll();
Result getByNameAndAge(String name, int age);
}
8.创建serviceimpl实现层
package com.cst.service.impl;
import com.cst.dao.UserRepository;
import com.cst.entity.User;
import com.cst.service.UserService;
import com.cst.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);
}
}
9.创建控制层controller
package com.cst.controller;
import com.cst.entity.User;
import com.cst.service.UserService;
import com.cst.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;
@PostMapping("/saveUser")
public Result saveUser(@RequestBody User user){
return userService.saveUser(user);
}
@GetMapping("/queryAll")
public Result queryAll(){
return userService.queryAll();
}
@GetMapping("/getByNameAndAge")
public Result getByNameAndAge(String name,int age){
return userService.getByNameAndAge(name,age);
}
}
10.以上步骤全部完成,启动项目就会自动生成对应你entity的一张表,然后使用测试工具进行 测试即可~! 源码参考地址:码云
|