[五]SpringBoot 之 连接数据库(JPA-Hibernate)
在具体介绍之前,先了解下什么是JPA
JPA全称JavaPersistence API.JPA通过JDK5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
下面具体介绍怎么配置
第一种方式(最简单最快速的实现连接 推荐使用第二种)
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)的更多相关文章
- SpringBoot之使用jpa/hibernate
Springboot版本是2.1.3.RELEASE 1.依赖 List-1.1 <dependency> <groupId>org.springframework.boot& ...
- SpringBoot + Jpa(Hibernate) 架构基本配置
1.基于springboot-1.4.0.RELEASE版本测试 2.springBoot + Hibernate + Druid + Mysql + servlet(jsp) 一.maven的pom ...
- springboot 集成 jpa/hibernate
pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- javaweb各种框架组合案例(六):springboot+spring data jpa(hibernate)+restful
一.介绍 1.springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之间 ...
- 五、spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate
1.pom添加依赖 <!-- spring data jpa,会注入tomcat jdbc pool/hibernate等 --> <dependency> <group ...
- [读书笔记] 四、SpringBoot中使用JPA 进行快速CRUD操作
通过Spring提供的JPA Hibernate实现,进行快速CRUD操作的一个栗子~. 视图用到了SpringBoot推荐的thymeleaf来解析,数据库使用的Mysql,代码详细我会贴在下面文章 ...
- (转)Spring Boot (十五): Spring Boot + Jpa + Thymeleaf 增删改查示例
http://www.ityouknow.com/springboot/2017/09/23/spring-boot-jpa-thymeleaf-curd.html 这篇文章介绍如何使用 Jpa 和 ...
- spring boot(十五)spring boot+thymeleaf+jpa增删改查示例
快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...
- Spring Boot + Jpa(Hibernate) 架构基本配置
本文转载自:https://blog.csdn.net/javahighness/article/details/53055149 1.基于springboot-1.4.0.RELEASE版本测试 2 ...
随机推荐
- 洛谷 P3369 【模板】普通平衡树(Treap/SBT)
题目描述 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多 ...
- HBase 第四章 HBase原理
1 体系图 HBase中的每张表都通过行键按照一定的范围被分割成多个子表(HRegion),默认一个HRegion超过256M就要被分割成两个,这个过程由HRegionServer管理,而HRegi ...
- 开源工具 | 手游自动化框架GAutomator,新增iOS系统和UE4引擎支
WeTest 导读 GAutomator是腾讯WeTest推出的手游自动化测试框架,已用于腾讯多个手游项目组的自动化测试. 1.GAutomator诞生背后 研究过手游自动化测试的同学都知道,虽然市场 ...
- 五、利用EnterpriseFrameWork快速开发基于WebServices的接口
回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 前面几章已完成EnterpriseFrameWo ...
- Maven私库
<server> <id>releases</id> <username>admin</username> <password> ...
- OpenLDAP备份和恢复
OpenLDAP中数据备份一般分为二种: 1)通过slapcat 指令进行备份 2)通过phpLDAPadmin控制台进行备份 备份方式1: 1)slapcat -v -l openldap-back ...
- Digital Roots:高精度
C - Digital Roots Description The digital root of a positive integer is found by summing the digits ...
- 238. [LeetCode] Product of Array Except Self
Given an array nums of n integers where n > 1, return an array output such that output[i] is equ ...
- USACO 1.3.4 Prime Cryptarithm 牛式(模拟枚举)
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- Coin Game
Problem Description After hh has learned how to play Nim game, he begins to try another coin game wh ...