springboot使用Jpa连接数据库

springboot使用Jpa连接数据库

1.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>com.example</groupId>
    <artifactId>spring-data_JpaAndJdbcTemplate</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-mybatis</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.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</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</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.21</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </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>

2.application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.Student:

package com.example.springmybatis.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {

    @Id
    @GeneratedValue
    private String name;
    @Column(nullable = true)
    private int age;
    @Column(nullable = true)
    private String address;

    private String collage;

    private String sex;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCollage() {
        return collage;
    }

    public void setCollage(String collage) {
        this.collage = collage;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                ", collage='" + collage + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

4.UserRepository:

package com.example.springmybatis.service;

import com.example.springmybatis.entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserRepository extends JpaRepository<Student, Long> {

    List<Student> findByName(String name);

    Student findByNameAndAge(String name, Integer age);

    int findByAge(int age);

    Page<Student> findAll(Pageable pageable);

}

5.UserController:

package com.example.springmybatis.Controller;


import com.example.springmybatis.entity.Student;
import com.example.springmybatis.service.UserRepository;
import com.example.springmybatis.service_jdbc.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;
/**
     * spring-data-jpa连接数据库
     * @param name
     * @param age
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/name", method = RequestMethod.GET)
    @ResponseBody
    public Student GetGoodList(@RequestParam("name") String name, @RequestParam(value = "age", required = false,
            defaultValue = "27") int age)throws Exception{

        Student user = userRepository.findByNameAndAge(name, age);
        return user;
    }


    @RequestMapping(value = "/names", method = RequestMethod.GET)
    @ResponseBody
    public List<Student> GetName(@RequestParam(value = "name", defaultValue = "易水寒") String name)throws Exception{

        List<Student> user = userRepository.findByName(name);
        return user;
    }
     /**
     * jpa排序分页
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/finds", method=RequestMethod.GET)
    public Page<Student> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                          @RequestParam(value = "size", defaultValue = "15") Integer size) {

        Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "age");
        return userRepository .findAll(pageable);
    }
}

6.spring boot启动项:

package com.example.springmybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringDataJpaAPP {

    public static void main(String[] args) {
        SpringApplication.run(SpringDataJpaAPP.class, args);
    }
}

7.sql:

CREATE TABLE `student` (
  `name` varchar(100) NOT NULL,
  `age` int(10) NOT NULL,
  `address` varchar(100) NOT NULL,
  `collage` varchar(100) NOT NULL,
  `sex` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

posted @ 2018-08-13 20:16  何其小静  阅读(5222)  评论(0编辑  收藏  举报