1毕莱、環(huán)境約束
- win10 64位操作系統(tǒng)
- idea2018.1.5
- maven-3.0.5
- jdk-8u162-windows-x64
- mysql6.5
2、前提約束
- 完成springboot創(chuàng)建web項(xiàng)目 http://www.reibang.com/p/de979f53ad80
注意:筆者創(chuàng)建項(xiàng)目的時(shí)候約束的包前綴是net.wanho.springboot.hibernatedemo姐刁,讀者可以自行創(chuàng)建包名沪编,只是要注意本文中的代碼也要修改包名
3贫途、修改pom.xml
在dependencies標(biāo)簽中加入以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
4擂达、在主入口類同等路徑下分別創(chuàng)建entity绢淀、dao萤悴、service、controller包
在entity中加入U(xiǎn)ser.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中加入U(xiǎn)serDao.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<User, Integer> {
}
在service中加入U(xiǎn)serService.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<User> query()
{
return userdao.findAll();
}
}
在controller中加入U(xiǎn)serController.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<User> queryUsers()
{
return userService.query();
}
}
5皆的、將application.properties改名為application.yml【官網(wǎng)建議使用yml格式覆履,yml格式下鍵值對(duì)為樹形結(jié)構(gòu)】
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cms
username: root
# 筆者的數(shù)據(jù)庫密碼是zhangli,請(qǐng)讀者根據(jù)自己數(shù)據(jù)庫密碼修改
password: zhangli
jpa:
database : mysql
show-sql : true
hibernate.ddl-auto : create
hibernate.dialect : org.hibernate.dialect.MySQL5Dialect
6、啟動(dòng)并測試费薄。
首先新增用戶硝全,再查詢用戶,具體操作如下:
至此楞抡,我們完成了springboot對(duì)hibernate的支持伟众,并做了測試。