SpringBoot+JPA简单入门demo

1.创建一个普通的maven工程

2.配置pom.xml

<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.xiao</groupId>
  <artifactId>SpringBootJPA</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>SpringBootJPA</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <!-- 版本控制 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    
  <dependencies>
    <!-- spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- spring-boot-configuration-processor -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
  </dependencies>
  
  <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3.配置数据源。在src/main/resources下新建一个application.properties文件

# 服务器访问端口
server.port=8080
# 数据库基本配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootjpa
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.database=MYSQL
# 显示后台处理的SQL语句
spring.jpa.show-sql=true
# 自动检查实体和数据库表是否一致,如果不一致则会进行更新数据库表
spring.jpa.hibernate.ddl-auto=update

4.配置应用程序启动类,Application.java

package com.xiao.SpringBootJPA;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * SpringBootJpa应用程序启动文件
* @author xiaosongsong: 
* @CreateTime 创建时间:2018年8月10日 下午9:21:23 
* 类说明
 */
@EnableAutoConfiguration
@EnableTransactionManagement
@ServletComponentScan
@ComponentScan
@EnableScheduling
public class Application  
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(Application.class, args);
    }
}
5.创建实体Employee.java

package com.xiao.SpringBootJPA.entity;

import java.io.Serializable;

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

/**
 * 职工类
* @author xiaosongsong: 
* @CreateTime 创建时间:2018年8月10日 下午9:23:58 
* 类说明
 */
@Entity
@Table(name="employee")
public class Employee implements Serializable {

    private static final long serialVersionUID = -2544247758732046249L;

    @Id
    @Column(name="id")
    private String id;
    
    @Column(name="name")
    private String name;
    
    @Column(name="gender")
    private String gender;
    
    @Column(name="age")
    private int age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    
    
}
6.创建repository接口

package com.xiao.SpringBootJPA.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.xiao.SpringBootJPA.entity.Employee;

/**
 * EmployeeRepository接口
* @author xiaosongsong: 
* @CreateTime 创建时间:2018年8月10日 下午9:27:08 
* 类说明
 */
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String>{

}
7.创建控制层

package com.xiao.SpringBootJPA.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xiao.SpringBootJPA.entity.Employee;
import com.xiao.SpringBootJPA.repository.EmployeeRepository;

/**
 * 职工Controller
* @author xiaosongsong: 
* @CreateTime 创建时间:2018年8月10日 下午9:30:30 
* 类说明
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    
    @Autowired
    private EmployeeRepository employeeRepository;

    @RequestMapping("/queryAll")
    @ResponseBody
    public List<Employee> queryAll(){
        List<Employee> list=new ArrayList<Employee>();
        list=employeeRepository.findAll();
        return list;
    }
    
}
8.启动应用程序,访问http://localhost:8080/employee/queryAll

posted @ 2018-08-11 10:47  小草1234  阅读(599)  评论(0编辑  收藏  举报