spring使用 hibernate jpa JpaRepository
使用JpaRepository需要两个架包:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.6.Final</version>
</dependency>
1、创建实体类:Role
package com.wbg.Jpa.entity; import com.sun.javafx.geom.transform.Identity; import javax.persistence.*; @Entity
@Table
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
//比如数据库名字是names 指定设置为:
@Column(name = "role_name")
private String roleName;
private String note; @Override
public String toString() {
return "Role{" +
"id=" + id +
", roleName='" + roleName + '\'' +
", note='" + note + '\'' +
'}';
} public Role() {
} public Role(int id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getRoleName() {
return roleName;
} public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getNote() {
return note;
} public void setNote(String note) {
this.note = note;
}
}
2、配置:JavaConfig
package com.wbg.Jpa.config; import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
import org.springframework.transaction.support.TransactionTemplate; import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties; @Configuration
@ComponentScan("com.wbg.Jpa")
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.wbg.Jpa.dao"})
public class JavaConfig { @Bean(name = "dataSource")
public DataSource getDataSource() {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
dataSource.setDriverClass("org.mariadb.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
dataSource.setJdbcUrl("jdbc:mariadb://localhost:3306/wbg_logistics");
dataSource.setUser("root");
dataSource.setPassword("123456");
dataSource.setMaxPoolSize(30);
return dataSource;
} @Bean
public JdbcTemplate jdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(getDataSource());
return jdbcTemplate;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){
LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource);
bean.setPackagesToScan("com.wbg.Jpa.entity");
bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); Properties properties = new Properties();
/**
* validate 加载hibernate时,验证创建数据库表结构
* create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。。
* create-drop 加载hibernate时创建,退出是删除表结构
* update 加载hibernate自动更新数据库结构
*/
properties.setProperty("hibernate.hbm2ddl.auto","update");
//格式化输出语句
/**列如
* Hibernate: select role0_.id as id1_0_, role0_.note as note2_0_, role0_.role_name as role_nam3_0_ from Role role0_
*格式化韦
* select
* role0_.id as id1_0_,
* role0_.note as note2_0_,
* role0_.role_name as role_nam3_0_
* from
* Role role0_
*/
properties.setProperty("hibernate.format_sql","true");
//显示执行sql语句
properties.setProperty("hibernate.show_sql","true"); //设置方言 properties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
bean.setJpaProperties(properties); return bean;
} }
3、接口RoleDao
package com.wbg.Jpa.dao; import com.wbg.Jpa.entity.Role;
import org.springframework.data.jpa.repository.JpaRepository; public interface RoleDao extends JpaRepository<Role,Integer> { }
4、实现类:RoleService
package com.wbg.Jpa.dao; import com.wbg.Jpa.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List; @Service
public class RoleService {
@Autowired
private RoleDao roleDao; public List<Role> listAll() {
List<Role> list = roleDao.findAll();
return list;
} }
测试:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig.class);
RoleService roleDao = applicationContext.getBean(RoleService.class);
for (Role role : roleDao.listAll()) {
System.out.println(role);
}
demo:https://github.com/weibanggang/hibernatejpaJpaRepository.git
spring使用 hibernate jpa JpaRepository的更多相关文章
- Spring MVC+Hibernate JPA搭建的博客系统项目中所遇到的坑
标签: springmvc hibernate 2016年12月21日 21:48:035133人阅读 评论(0) 收藏 举报 分类: Spring/Spring MVC(6) Hibernate ...
- spring mvc 的jpa JpaRepository数据层 访问方式汇总
本文转载至:http://perfy315.iteye.com/blog/1460226 AppleFramework在数据访问控制层采用了Spring Data作为这一层的解决方案,下面就对Spri ...
- Spring Hibernate JPA 联表查询 复杂查询(转)
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...
- Spring Hibernate JPA 联表查询 复杂查询
今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...
- Spring整合Hibernate实现Spring Data JPA (介绍和使用)
Spring Data JPA是Spring基于Hibernate开发的一个JPA框架.如果用过Hibernate或者MyBatis的话,就会知道对象关系映射(ORM)框架有多么方便. 但是Sprin ...
- Spring + SpringMVC + Druid + JPA(Hibernate impl) 给你一个稳妥的后端解决方案
最近手头的工作不太繁重,自己试着倒腾了一套用开源框架组建的 JavaWeb 后端解决方案. 感觉还不错的样子,但实践和项目实战还是有很大的落差,这里只做抛砖引玉之用. 项目 git 地址:https: ...
- spring data jpa hibernate jpa 三者之间的关系
JPA规范与ORM框架之间的关系是怎样的呢? JPA规范本质上就是一种ORM规范,注意不是ORM框架——因为JPA并未提供ORM实现,它只是制订了一些规范,提供了一些编程的API接口,但具体实现则由服 ...
- spring+hibernate+jpa+Druid的配置文件,spring整合Druid
spring+hibernate+jpa+Druid的配置文件 spring+hibernate+jpa+Druid的完整配置 spring+hibernate+jpa+Druid的数据源配置 spr ...
- 【实验一 】Spring Boot 集成 hibernate & JPA
转眼间,2018年的十二分之一都快过完了,忙于各类事情,博客也都快一个月没更新了.今天我们继续来学习Springboot对象持久化. 首先JPA是Java持久化API,定义了一系列对象持久化的标准,而 ...
随机推荐
- JqueryEasyUI $.Parser
Parser(解析器) 对象的属性和方法: 使用: <link href="~/jquery-easyui-1.5.2/themes/bootstrap/easyui.css" ...
- [shell]管理 Sphinx 启动|停止|重新生成索引的脚本
对于启动sphinx的服务,可以直接输入如下命令 /usr/bin/searchd -c /etc/sphinx/sphinx.conf <!-- /usr/local/bin/searchd ...
- spring 代理
java动态代理实现 1. Java自带的动态代理,反射生成字节码 2. Cglib调用asm生成子类 spring 中代理实现 1. 如果类实现了接口,使用java动态代理 2. 没有实现接口,使用 ...
- hdu 1011 树形背包
http://blog.csdn.net/libin56842/article/details/9876503 这道题和poj 1155的区别是: poj1155是边的价值,所以从边的关系入手 hdu ...
- python之from 和import执行过程分析
原文链接:http://blog.csdn.net/lis_12/article/details/52883729 问题1 同一个目录下,有两个Python文件,A.py,B.py #A.py fro ...
- PAT 1044. Shopping in Mars
#include <cstdio> #include <cstdlib> #include <vector> #include <climits> #i ...
- linux系统下php扩展的安装
0. 这里以php安装redis扩展为例 1. 首先下载并解压redis扩展包 [root@xxx ~]# cd /usr/local/src [root@xxx src]# wget https:/ ...
- Luogu3403: 跳楼机
题面 传送门 Sol 有一个显然的想法 处理出\(y, z\)能凑出的高度 然后这些高度凑一些\(x\)就可以得到其它的高度 那么可以把这些\(y, z\)凑出的高度对\(x\)取模,其它的用\(x\ ...
- cf1037E. Trips(图论 set)
题意 题目链接 Sol 倒着考虑!倒着考虑!倒着考虑! 显然,一个能成为答案的子图一定满足,其中任意节点的度数\(>= k\) 那么倒着维护就只用考虑删除操作,如果一个点不合法的话就把它删掉,然 ...
- javascript多浏览器的兼容
一.document.formName.item(”itemName”) 问题 问题说明:IE下,可以使用 document.formName.item(”itemName”) 或 document. ...