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,定义了一系列对象持久化的标准,而 ...
随机推荐
- Vue-Router的使用(一)
1.首先,安装vue-router npm install vue-router --save-dev 2.创建一个route.js文件 // 1. 定义路由组件 // 可以自己写的,或者导入的,大部 ...
- BFC(Box Formatting Context)的原理
BFC 已经是一个耳听熟闻的词语了,网上有许多关于 BFC 的文章,介绍了如何触发 BFC 以及 BFC 的一些用处(如清浮动,防止 margin 重叠等).虽然我知道如何利用 BFC 解决这些问题, ...
- 把连接中传的参数截取出来变成一个json对象
获取url function test() { var url=window.location.search; if(url.indexOf("?")!=-1) { var str ...
- 服务器端渲染VS浏览器端渲染
1)浏览器渲染和服务器渲染区别:何为渲染?如果我们只是想显示一堆不变的数据,那么我们直接写一个a.html丢到服务器上让客户端访问就可以了.但这是基本不可能的事情,数据一般是变化的.你不可能为每套数据 ...
- IDEA maven 多项目 出现 java 程序包找不到
使用idea 多项目引入的时候,出现报错信息:Error java程序包找不到 解决办法: 我在引入多个项目的时候,他们是在一个目录里面的.我把整个目录引入了进去.结果报依赖包找不到. 把引入的全部项 ...
- mongodb远程连接
1.关闭防火墙 firewall-cmd --state 查看防火墙是否运行中 systemctl stop firewalld.service 关闭防火墙服务 2.使用bind_ip指定 ...
- 浏览器环境下的javascript DOM对象继承模型
这张图是我直接在现代浏览器中通过prototype原型溯源绘制的一张浏览器宿主环境下的javascript DOM对象模型,对于有效学习和使用javascript DOM编程起到高屋建瓴的指导作用, ...
- “云中论道”之——使用开源技术和Azure公有云服务快速搭建云端IoT解决方案(上)
“云中论道”技术课堂第一课开讲啦!微软各路技术咖们齐聚一堂,为大家带来干货不断!作为“云中论道“课堂的开课之作,我们首先邀请到了微软Azure专家级的架构师:槐长清,他为我们带来了关于“使用开源技术和 ...
- 微软宣布在Azure上支持更多的开放技术和选择
微软和我都热爱Linux,并且就在情人节过去几天之后,我非常高兴能用几个激动人心的消息来表达这种对Linux的热爱,您将会看到在Azure上的云部署将具有更加开放的选择性和灵活性. 这些激动人心的消息 ...
- 加密算法IV的作用
使用随机数产生的初始化向量才能达到语义安全(散列函数与消息验证码也有相同要求),并让攻击者难以对同一把密钥的密文进行破解 初始化向量的值依密码算法而不同.最基本的要求是“唯一性”,也就是说同一把密钥不 ...