在具体介绍之前,先了解下什么是JPA

JPA全称JavaPersistence API.JPA通过JDK5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化数据库中。

http://baike.baidu.com/link?url=LdqIXvzTr0RDjY2yoRdpogDdzaZ_L-DrIOpLLzK1z38quk6nf2ACoXEf3pWKTElHACS7vTawPTmoFv_QftgT_q

下面具体介绍怎么配置

第一种方式(最简单最快速的实现连接 推荐使用第二种)

1.引入jar包

pom.xml配置:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

2.配置application.properties

关于application.properties

springboot中允许修改默认的配置,一般在resource文件下添加application.properties文件,修改相关的配置

########################################################
###datasource
########################################################

spring.datasource.url = jdbc:mysql://123.206.228.200:3306/test

spring.datasource.username = shijunjie

spring.datasource.password = *******

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

########################################################

### Java Persistence Api

########################################################

# Specify the DBMS

spring.jpa.database = MYSQL

# Show or not log for each sql query

spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)

spring.jpa.hibernate.ddl-auto = create-drop

# Naming strategy

#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect

3.编写实体类

package me.shijunjie.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity //加入这个注解,Demo就会进行持久化了
@Table(name="t_demo")
public class Demo { public Demo() {
} public Demo(long id, String name) {
this.id = id;
this.name = name;
} @Id
@GeneratedValue
private long id; @Column(name="tname")
private String name; 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;
} }

4.编写DAO

package me.shijunjie.dao;

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

import me.shijunjie.entity.Demo;

public interface DemoDao extends JpaRepository<Demo, Long> {

}

5.编写Service接口及其实现类

接口:

package me.shijunjie.service;

import me.shijunjie.entity.Demo;

public interface DemoService {
public void save(Demo demo);
}

实现类:

package me.shijunjie.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import me.shijunjie.dao.DemoDao;
import me.shijunjie.entity.Demo;
import me.shijunjie.service.DemoService; @Service
public class DemoServiceImpl implements DemoService { @Autowired
private DemoDao demoDao; public void save(Demo demo){
demoDao.save(demo);
}
}

6.编写Controller

package me.shijunjie.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import me.shijunjie.entity.Demo;
import me.shijunjie.service.DemoService; @RestController
@RequestMapping("/demo")
public class DemoController { @Resource
private DemoService demoService; /** * 测试保存数据方法. * @return */ @RequestMapping("/save")
public String save(){
Demo d = new Demo();
d.setName("Angel");
demoService.save(d);//保存数据.
return "ok.DemoController.save"; }
}

7.编写入口

package me.shijunjie.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling; @ComponentScan(basePackages={"me.shijunjie"}) // 扫描该包路径下的所有spring组件
@EnableJpaRepositories("me.shijunjie.dao") // JPA扫描该包路径下的Repositorie
@EntityScan("me.shijunjie.entity") // 扫描实体类
@SpringBootApplication
@EnableScheduling
public class App extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

输入spring-boot:run进行测试

打开浏览器输入http://localhost:8080/demo/save

查看数据库表中是否存入了数据

运行成功!

第二种方式(推荐)

参照http://blog.csdn.net/u012373815/article/details/53240946

[五]SpringBoot 之 连接数据库(JPA-Hibernate)的更多相关文章

  1. SpringBoot之使用jpa/hibernate

    Springboot版本是2.1.3.RELEASE 1.依赖 List-1.1 <dependency> <groupId>org.springframework.boot& ...

  2. SpringBoot + Jpa(Hibernate) 架构基本配置

    1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...

  3. springboot 集成 jpa/hibernate

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  4. javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful

    一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...

  5. 五、spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate

    1.pom添加依赖 <!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> <dependency> <group ...

  6. [读书笔记] 四、SpringBoot中使用JPA 进行快速CRUD操作

    通过Spring提供的JPA Hibernate实现,进行快速CRUD操作的一个栗子~. 视图用到了SpringBoot推荐的thymeleaf来解析,数据库使用的Mysql,代码详细我会贴在下面文章 ...

  7. (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例

    http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...

  8. spring boot(十五)spring boot+thymeleaf+jpa增删改查示例

    快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...

  9. Spring Boot + Jpa(Hibernate) 架构基本配置

    本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...

随机推荐

  1. MySQL入门篇(一)之MySQL部署

    MySQL 二进制免编译安装 (1)下载二进制免编译版本mysql 5.6.35 [root@localhost tools]# wget http://mirrors.sohu.com/mysql/ ...

  2. 查询系统日期和时间(mysql)

    select current_date  --不带时间select sysdate()   或 SELECT NOW();  --带时间

  3. 我们一起学习WCF 第四篇单通讯和双向通讯

    前言:由于个人原因很久没有更新这个系列了,我会继续的更新这系列的文章.这一章是单向和双向通讯.所谓的单向就是只有发送却没有回复,双向是既有发送还有回复.就是有来无往代表单向,礼尚往来表示双向.下面我用 ...

  4. CentOS 6.5关闭防火墙

    关闭命令:  service iptables stop 永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后查看防火墙关闭状态 service iptables ...

  5. javaweb(十四)——JSP原理

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  6. Redash二次开发-开发环境搭建

    环境:win7+pycharm 2018.2 +redash 1.安装pycharm并如何正常使用,找度娘. 2.配置pycharm vcs,设置github用户,从github新建redash项目 ...

  7. Chrome 字体模糊解决

    新的电脑装了Chorm后发现字体很模糊,看起来比较累效果是这样的: 大多数都是说使用chrome://flags/中的DirectWrite开关来使其正常显示,我打开chrome://flags/没找 ...

  8. 订单号生成逻辑,C#和JAVA双版

    五年没写过博客了,倒是天天在看 转来转去,又转回技术 原来一直在使用微软爸爸的东西,最近一两年开始玩android,玩java,还有PostgreSQL 都有些应用了,倒是可以整理些随笔出来,这就是其 ...

  9. 深入理解C++中的Const,Mutable以及Volatile

    我一直认为const表示一个常量,常量就是一个无法被修改的值,但是没有深入理解const的实现,甚至不知道mutable和volatile的存在,最近在书中看到了这一部分的知识,所以本文将详细解析这几 ...

  10. Linux工作管理

    工作管理? 其实也就是把程序放到后台来管理,在windows中也就是最小化,在Linux中是通过命令把程序放到后台中.jobs命令查看后台程序. 对于第一点注意事项,mysql启动是例外的,要是叉掉了 ...