一、Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。

二、Spring Boot 在配置上面都做了很多的简化。配置文件在resource目录里面,会自动做加载,优先resource下面的config目录配置文件加载,并且,spring-boot里面提供了许多相关的配置包,不需要在像mvc那样配置很多的xml配置文件

三、JPA这里我不多做介绍,可以参考,我原来通过springmvc和spring-data-jpa的整合:http://www.cnblogs.com/ll409546297/p/6992188.html

四、这里介绍spring-boot和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>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>

注释:里面的一些包可以不用加入,结合自己进行增删包

六、yml配置部分,这里只需要配置需要的部分,idea会根据提示来。

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

七、配置启动入口application把这个类放到目录的最外层,他会自动扫描相应的子包

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

八、针对于entity、dao层进行控制的配置

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提供的

九、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;
}
}

十、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> {

}

十一、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;
}
}

十二、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的使用主要是在高效和应用方面。能很好的使用相应的功能,并且简化配置

spring-boot、spring-data-jpa、hibernate整合的更多相关文章

  1. 初识在Spring Boot中使用JPA

    前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...

  2. 使用spring boot中的JPA操作数据库

    前言 Spring boot中的JPA 使用的同学都会感觉到他的强大,简直就是神器一般,通俗的说,根本不需要你写sql,这就帮你节省了很多时间,那么下面我们来一起来体验下这款神器吧. 一.在pom中添 ...

  3. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  4. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  5. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available

    Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of ...

  6. Spring Boot 2.0 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  7. 基于Spring Boot,使用JPA动态调用Sql查询数据

    在<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>,<基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合 ...

  8. 基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合

    在上一篇<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>中完成了使用JPA对实体数据的CRUD操作. 那么,有些情况,会把一些查询语句写在存储过程中,由 ...

  9. [权限管理系统(四)]-spring boot +spring security短信认证+redis整合

    [权限管理系统]spring boot +spring security短信认证+redis整合   现在主流的登录方式主要有 3 种:账号密码登录.短信验证码登录和第三方授权登录,前面一节Sprin ...

  10. 255.Spring Boot+Spring Security:使用md5加密

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

随机推荐

  1. 使用npoi插件将excel文件导出

    大致流程:前端使用URL地址的方式跳转到action后返回file类型数据 js: window.location.href = '/Home/index?Id=' + id 后台代码: /// &l ...

  2. 手把手教你自定义attr

    最近在学习的过程中遇到了自定义的attr和自定义的style.因此各种百度,各种博客的学习,算是有了一个系统的了解.在这里记录下自己的收获. 一.为什么要使用自定义attr以及本文定位 在androi ...

  3. android开发者您还在为模拟器犯愁吗?神级android模拟器---Genymotion一个更快、接近完美的模拟器……

    摘要:Android系统非常特别,App须要进行模拟化測试.即使这样仍然有解决的办法---虚拟化技术. 之前的模拟器比方eclipse自带的是非常慢的一种,并且模拟器的版本号并非最新的.开机.能够说差 ...

  4. 【[SHOI2015]脑洞治疗仪】

    我太sb啦 合并的时候又漏了,又漏了,又漏了 我个sb 这是个板子题,并不知道为什么SHOI2015会考这么板子的题,但是我又sb了,又sb了,又sb了,又没有1A 显然我是凉了 这道题有三个操作 区 ...

  5. PHP代码的多继承 -》 PHP代码复用新的姿势 trait

    本文参考:  http://php.net/language.oop5.traits 一.什么是trait 从PHP 5.4.0 开始 PHP 实现了一种新的代码复用方式 trait. 二.trait ...

  6. linux-资料汇集

    1.http://www.debian.org/doc/ 2.鸟哥的私房菜 3.The Linux Command Line by William E. Shotts, Jr. 4.https://d ...

  7. 【绝迹篇】RSA加密算法(私钥加签公钥验签)

    对于上上篇博客中我讲的一个故事,本文引用: https://www.cnblogs.com/ButterflyEffect/p/9851403.html 故事中提到的关于加密会出现,私钥加密,公钥解密 ...

  8. OpenCV 中CV_IMAGE_ELEM 的使用

    CV_IMAGE_ELEM 是一个宏函数,基本形式: CV_IMAGE_ELEM(image,elemtype,row,col) 其中,image为指针数组,elemtype为数据的存取类型,row为 ...

  9. Office365学习笔记—创建WikiPage

    1,项目有个需求:项目表每更新一次,就把跟该项目有关的任务创建一个静态页(历史版本功能)! 注意事项:需要在页面上拖一个ContentEditer!将代码放在ContentEditer里面,因为我试过 ...

  10. http1.X与2.0

    HTTP HTTP 1.X HTTP是建立在TCP协议上的,HTTP协议的瓶颈及优化都是基于TCP协议本身的特性. TCP建立连接时有三次握手 会有1.5RTT的延迟,为了避免每次请求都经历握手待来的 ...