spring-boot、spring-data-jpa整合

1、Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員再也不須要定義樣板化的配置。經過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成爲領導者。html

2、Spring Boot 在配置上面都作了不少的簡化。配置文件在resource目錄裏面,會自動作加載,優先resource下面的config目錄配置文件加載,而且,spring-boot裏面提供了許多相關的配置包,不須要在像mvc那樣配置不少的xml配置文件java

3、JPA這裏我很少作介紹,能夠參考,我原來經過springmvc和spring-data-jpa的整合:http://www.cnblogs.com/ll409546297/p/6992188.htmlmysql

4、這裏介紹spring-boot和JPA的相關配置(簡易配置)。看一下目錄結構web

5、pom.xml配置spring

 

<?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.troy</groupId>
    <artifactId>springboot</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-Java</artifactId>
            <version>5.1.9</version>
        </dependency>
    </dependencies>
</project>

註釋:裏面的一些包能夠不用加入,結合本身進行增刪包sql

6、yml配置部分,這裏只須要配置須要的部分,idea會根據提示來。apache

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/model?useUnicode=true&amp;characterEncoding=utf8
    username: root
    password: root
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update

7、配置啓動入口application把這個類放到目錄的最外層,他會自動掃描相應的子包api

package com.troy.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer{

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

8、針對於entity、dao層進行控制的配置springboot

package com.troy.boot.config;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@EnableJpaRepositories(basePackages = "com.troy.boot.repository")
@EntityScan(basePackages = "com.troy.boot.entity")
public class DataSourceConfig {

}

注意:這裏也提供了@Configuration來配置具體屬性,固然這是jpa提供的mvc

9、entity層

package com.troy.boot.entity;

import javax.persistence.*;

@Entity
@Table(name = "USER")
public class User {

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column(name = "NAME")
    private String name;
    @Column(name = "AGE")
    private String age;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getAge() {
        return age;
    }

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

10、repository層

BaseRepository.class

package com.troy.boot.repository;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.io.Serializable;

@NoRepositoryBean
public interface BaseRepository<T,I extends Serializable> extends PagingAndSortingRepository<T,I>,JpaSpecificationExecutor<T>{
}
UserRepository.class
package com.troy.boot.repository;

import com.troy.boot.entity.User;

public interface UserRepository extends BaseRepository<User,Long> {

}

11、service層

package com.troy.boot.service;

import com.troy.boot.entity.User;

import java.util.List;

public interface UserService {

    /**
     * 查詢所有用戶
     * @return
     */
    public List<User> queryUsers();
}

Impl

package com.troy.boot.service.Impl;

import com.troy.boot.entity.User;
import com.troy.boot.repository.UserRepository;
import com.troy.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<User> queryUsers() {
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> querys = new ArrayList<Predicate>();
                Predicate[] predicates = new Predicate[querys.size()];
                return cb.and(querys.toArray(predicates));
            }
        };
        List<User> list = userRepository.findAll(specification);
        return list;
    }
}

12、controller層

package com.troy.boot.controller;

import com.troy.boot.entity.User;
import com.troy.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/api/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/init")
    public String init() {
        return "hello world!";
    }

    @RequestMapping(value = "/queryUsers")
    public List<User> queryUsers() {
        return userService.queryUsers();
    }
}

基本上的spring-boot加JPA的配置和使用過程就這個樣子了。spring-boot的使用主要是在高效和應用方面。能很好的使用相應的功能,而且簡化配置

相關文章
相關標籤/搜索