1. 程式人生 > >springboot-jpa|hibernate|springdata

springboot-jpa|hibernate|springdata

一、Concept

JPA java persistence API
通過5.0的註解或者xml描述物件到關係表的對映關係,並將執行期的實體物件持久化到資料庫中
Hibernate
jpa是一種規範,hibernate是它的實現
對jdbc輕量級封裝,將pojo與資料庫表建立對映關係,全自動orm框架
springData
簡化對資料庫訪問,支援雲服務的開源框架
springDataJpa
是springData的一個模組, 簡化jpa的寫法,除了curd外,還有分頁、排序功能,只需要實現一個介面就可以curd操作

二、springboot+jpa

1.add pom dependencies

	<!-- 新增MySQL資料庫驅動依賴包. -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
	<!-- 新增Spring-data-jpa依賴. -->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId
>
spring-boot-starter-data-jpa</artifactId> </dependency>

2.Add database configuration information in application.properties

########################################################
###datasource config
########################################################
spring.datasource.url =
jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root 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 -- Spring jpa config ######################################################## # 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 = update # 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.Entity class

@Entity//標註實體類
@Id//主鍵
@GeneratedValue(Strategy=GenerationType.AUTO)//主鍵生成策略

4.Reponsitory interface

在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述

//最頂層空介面Reponstory
public interface XxxReponsitory extends CurdReponsitory<Xxx,Integer>{
	//已經繼承了curd介面的所有方法

	//可以兩種方法自定義查詢
	//1.查詢方法以find,get,read開頭
	public Xxx findByXxxName(String xxxName);
	//2.JPQL語句 類似hql
	@Query("from Xxx where xxxName = :na);
	public Xxx findByXxxName2(@Params("na" String xxxName);
}

in service

@Autowired
private XxxReponsitory xxxReponsitory;

@Transaction//進行事務繫結

xxxReponsitory.save(Xxx);
xxxReponsitory.delete(1)
List<Xxx> list = xxxReponsitory.findAll();

三、springboot + jdbcTemplate

1.add pom dependencies

	<!-- 新增MySQL資料庫驅動依賴包. -->
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
	</dependency>
	<!-- 新增Spring-data-jdbc依賴. -->
	<!-- 當然,如何已經加入jpa,則不用引入jdbc-->
	<dependency>
	    <groupId>org.springframework.boot</groupId>
	    <artifactId>spring-boot-starter-data-jdbc</artifactId>
	</dependency>

2.Method of use

@Reponsitory
public class DemoDao{
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public Demo getById(long id){
		String sql = "select * from Demo where id=?";
		RowMapper<Demo> rowMapper = new BeanPropertyRowMapper<Demo>(Demo.class);
		return jdbcTemplate.queryForObject(sql,rowMapper);
	}
}