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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springws</groupId>
<artifactId>springws</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8080
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF-8
##charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
##set to false for hot refresh
#spring.thymeleaf.cache=false
debug=true
#配置文件必須去除沒必要要的空格,不然會有莫名其妙的錯誤
spring.datasource.url=jdbc:mysql://localhost:3306/****
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true
定義 entity
package com.ws.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity //這個註解必需要
@Table(name="ccvms_course",catalog="ccvms")
//表名爲 ccvms_course 數據庫名爲 ccvms 生成的數據表名稱爲 ccvms.ccvms_course
public class Course{
@Id
@GeneratedValue
private Integer id;
@Column(name="name") //對應的數據表中的名字,通常能夠不寫
private String name;
@Column
private Integer uid;
@Column
private String code;
@Column
private Long time;
@Column
private Integer grade;
public Course() {}
/**
*
* @param name
* @param uid
* @param code
* @param time
* @param grade
*/
public Course(String name, Integer uid, String code, Long time, Integer grade) {
super();
this.name = name;
this.uid = uid;
this.code = code;
this.time = time;
this.grade = grade;
}
/**
*
* @param id
* @param name
* @param uid
* @param code
* @param time
* @param grade
*/
public Course(Integer id, String name, Integer uid, String code, Long time, Integer grade) {
super();
this.id = id;
this.name = name;
this.uid = uid;
this.code = code;
this.time = time;
this.grade = grade;
}
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 getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
@Override
public String toString() {
return "Course [id=" + id + ", name=" + name + ", uid=" + uid + ", code=" + code + ", time=" + time + ", grade="
+ grade + "]";
}
public Integer getGrade() {
return grade;
}
public void setGrade(Integer grade) {
this.grade = grade;
}
}
建立 repository
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.ws.bean.Course;
//這邊能夠加上@Repository , 不加也不會報錯
public interface CourseDao extends JpaRepository<Course, Integer>{
//hql查詢,傳入分頁信息
@Query("from com.ws.bean.Course u where u.id>1758 order by u.id desc")
List<Course> getCourse(Pageable pageable);
// 前三條,傳入排序信息
List<Course> findTop3ByName(String name,Sort sort);
//詳細狀況請看官方文檔
}
controller調用
package com.ws.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ws.bean.Course;
import com.ws.repository.CourseDao;
@RestController
public class IndexController {
private final CourseDao courseDao;
@Autowired
public IndexController(CourseDao courseDao) {
this.courseDao = courseDao;
}
@GetMapping("/")
public List<Course> index(){
return courseDao.findTop3ByName("test",new Sort(Direction.DESC,"id"));
}
@GetMapping("/save")
public Course saveCourse() {
Course course = new Course("test", 12, "123213123", 12312312L, 123);
return courseDao.save(course);
}
}
啓動類
package com.ws;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String ... args){
SpringApplication.run(App.class,args);
}
}