springboot 集成 spring-data-jpa

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);
    }
}

猜你喜欢

转载自my.oschina.net/u/2528821/blog/1618852