- 第一步引入pom.xml依赖和application.properties信息
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.hanweb</groupId>
<artifactId>bbts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties:
server.port=8081
#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useunicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=admine
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
- 第二步建实体类
package com.example.bbts.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Administrator
*
*/
import com.example.bbts.common.Tables;
@Entity
@Table(name = Tables.STUDENT)
public class Student implements Serializable {
/**
* 持久化参数
*/
private static final long serialVersionUID = -8447170587360305408L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer 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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- 第三步建立完整的mvc层
controller层:
package com.example.bbts.controller;
import java.util.HashMap;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.bbts.entity.Student;
import com.example.bbts.service.StudentService;
/**
*
* @author Administrator
*
*/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value="/student")
public HashMap<String, Object> getStudentList() {
HashMap<String,Object> map = new HashMap<String, Object>();
List<Student> list = studentService.get();
map.put("student:", list);
map.put("total", list.size());
return map;
}
}
service层
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
/**
*
* @return
*/
public List<Student> get(){
List<Student> list = studentDao.findAll();
return list;
}
}
dao层(看好了兄弟),看好了再回来跟你说一下啊,hibernate是全自动orm持久层框架,所以他已经封装了很多增删改查的方法,只需要直接调取就行了。如果非要自己写,那就session实例,在query塞入语句,进行操作。
package com.example.bbts.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.bbts.entity.Student;
/**
*
* @author Administrator
*
*/
@Repository
public interface StudentDao extends JpaRepository<Student, Integer>{
}
- 最重要的一步
![](https://i-blog.csdnimg.cn/blog_migrate/8eed3c93845822b420e7d28050336d1f.png)
- 展示效果
![](https://i-blog.csdnimg.cn/blog_migrate/de2fd74d2623285479852beee9c3506f.png)