spring boot集成hibernate的详细介绍及代码

在前面我们已经介绍了springboot,没看的朋友可以先看下:点我

其实springboot集成hibernate非常的简单,话不多说直接上代码,跟着我的步骤来,绝对可以跑起来

 

一、添加pom文件



  4.0.0

  cn.et
  spring-boot-demo
  1.0-SNAPSHOT
  war
  
  
  
    org.springframework.boot
    spring-boot-starter-parent
    1.5.9.RELEASE
  

  
    
    
      org.springframework.boot
      spring-boot-starter-web
    

      
      
          org.springframework.boot
          spring-boot-starter-data-jpa
      

    
    
      mysql
      mysql-connector-java
      5.1.44
    
  


 

二、编写一个项目启动类:

package cn.et;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//必须添加 @SpringBootApplication 启动spring的自动配置功能 必须要添加
@SpringBootApplication

public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}

 

三、在application.yml或者application.perproties里配置连接数据库的四要素,这两个文件在resouces目录下,springboot会自动去加载,在这里我是通过application.perproties,配置方式如下

#配置数据库四要素 键不可改变
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

四、编写一个对应数据库表的实体类

package cn.et.demo01.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
//@Entity用来标识这是一个实体类
@Entity
public class Student {
   //@Id  hibernate的特征,必须要一个id不然会报错
    @Id
    //@Column(name = "sid") 用来跟表的字段做个映射,如果名字相同就不需要写
    @Column(name = "sid")
    private String id;

    @Column(name = "sname")
    private String name;

    @Column(name = "gid")
    private String gid;

    @Column(name = "sage")
    private String age;

    @Column(name = "ssex")
    private String sex;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGid() {
        return gid;
    }

    public void setGid(String gid) {
        this.gid = gid;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

@Entity用来标识这是一个实体类

@Id  hibernate的特征,必须要一个id不然会报错

@Column(name = "sid") 用来跟表的字段做个映射,如果名字相同就不需要写

 

五、编写持久层,持久层什么都不用做,实现一个类就可以了,有两种实现方式分别是JpaRepository、CrudRepository在这里我用的事CrudRepository

package cn.et.demo01.mapper;

import cn.et.demo01.model.Student;
import org.springframework.data.repository.CrudRepository;

public interface StudentMapper extends CrudRepository {}

 

六、编写service层。这一层主要是用来实现一些业务逻辑的代码,我这里其实就是一些简单的增删改查,要是觉得麻烦可以不写,可以直接通过controller调用mapper类

package cn.et.demo01.service;

import cn.et.demo01.model.Student;

import java.util.List;

public interface StudentService {
    /**
     * 添加
     */
    void insertStudent(Student student);

    /**
     * 删除
     */
    void deleteStudent(String id);

    /**
     * 修改
     */
    void updateStudent(Student student);

    /**
     * 查询
     */
     List getStudent();

    /**
     * 查询单个
     */
    Student getStudentById(String id);

}

 

实现类

package cn.et.demo01.service.impl;

import cn.et.demo01.mapper.StudentMapper;
import cn.et.demo01.model.Student;
import cn.et.demo01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public void insertStudent(Student student) {
        studentMapper.save(student);
    }

    @Override
    public void deleteStudent(String id) {
        studentMapper.delete(id);
    }

    @Override
    public void updateStudent(Student student) {
        studentMapper.save(student);
    }

    @Override
    public List getStudent() {
        return (List) studentMapper.findAll();
    }

    @Override
    public Student getStudentById(String id) {
        return studentMapper.findOne(id);
    }
}

 

七、编写控制层

package cn.et.demo01.controller;

import cn.et.demo01.model.Student;
import cn.et.demo01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * RestController 这个注解是Controller的子类里面还包含了ResponseBody 所以返回的时候就不需要在方法上写ResponseBody了
 */
@RestController
@RequestMapping("demo01")
public class HibernateController {
    @Autowired
    StudentService studentService;

    /**
     * 添加
     * @return
     */
    @RequestMapping("insert")
    public String insert(){
        Student student = new Student();
        student.setId("77");
        student.setAge("77");
        student.setGid("77");
        student.setName("77");
        student.setSex("男");
        studentService.insertStudent(student);
        return "SUCCESS";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public String delete(String id){
        studentService.deleteStudent(id);
        return "SUCCESS";
    }

    /**
     * 修改
     * 修改跟添加调用的是同一个方法如果id相同就改变数据没有就创建数据
     * @param student
     * @return
     */
    @RequestMapping("update")
    public String update(Student student){
        studentService.updateStudent(student);
        return "SUCCESS";
    }

    /**
     * 查询
     * @return
     */
    @RequestMapping("getStudent")
    public List getStudent(){
        return studentService.getStudent();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("studentId")
    public Student getStudentById(String id){
        return studentService.getStudentById(id);
    }

}

 

到这里一些基本的增删改查就完成了、、、、、、

你可能感兴趣的:(Spring,boot)