springboot支持hibernate 04

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • mysql6.5

2、前提约束

  • 完成springboot创建web项目 https://www.jianshu.com/p/de979f53ad80
    注意:笔者创建项目的时候约束的包前缀是net.wanho.springboot.hibernatedemo,读者可以自行创建包名,只是要注意本文中的代码也要修改包名

3、修改pom.xml

在dependencies标签中加入以下依赖:

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            mysql
            mysql-connector-java
            5.1.47
        

4、在主入口类同等路径下分别创建entity、dao、service、controller包

在entity中加入User.java

package net.wanho.springboot.hibernatedemo.entity;


import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "t_user")
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

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

    public User(){
        
    }
    public User(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

在dao中加入UserDao.java

package net.wanho.springboot.hibernatedemo.dao;

import net.wanho.springboot.hibernatedemo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDao extends JpaRepository {

}

在service中加入UserService.java

package net.wanho.springboot.hibernatedemo.service;

import net.wanho.springboot.hibernatedemo.dao.UserDao;
import net.wanho.springboot.hibernatedemo.entity.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserService {

    @Resource
    private UserDao userdao;

    public void insert(User user)
    {
        userdao.save(user);
    }

    public List query()
    {
        return userdao.findAll();
    }
}

在controller中加入UserController.java

package net.wanho.springboot.hibernatedemo.controller;

import net.wanho.springboot.hibernatedemo.entity.User;
import net.wanho.springboot.hibernatedemo.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/insertuser")
    @ResponseBody
    public String insertUser(User user)
    {
             userService.insert(user);
             return "insert ok";
    }

    @RequestMapping("/queryusers")
    @ResponseBody
    public List queryUsers()
    {
        return userService.query();
    }
}

5、将application.properties改名为application.yml【官网建议使用yml格式,yml格式下键值对为树形结构】

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cms
    username: root
    # 笔者的数据库密码是zhangli,请读者根据自己数据库密码修改
    password: zhangli
  jpa:
    database : mysql
    show-sql : true
    hibernate.ddl-auto : create
    hibernate.dialect : org.hibernate.dialect.MySQL5Dialect

6、启动并测试。

首先新增用户,再查询用户,具体操作如下:


测试

至此,我们完成了springboot对hibernate的支持,并做了测试。

你可能感兴趣的:(springboot支持hibernate 04)