spring使用 hibernate jpa JpaRepository
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;@Overridepublic 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;} }
View Code
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;}@Beanpublic JdbcTemplate jdbcTemplate() {JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(getDataSource());return jdbcTemplate;}@Beanpublic PlatformTransactionManager transactionManager(DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@BeanLocalContainerEntityManagerFactoryBean 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;}}
View Code
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> {}
View Code
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 {@Autowiredprivate RoleDao roleDao;public List<Role> listAll() {List<Role> list = roleDao.findAll();return list;}}
View Code
测试:
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 + Bootstrap 搭建的博客系统
Spring MVC + Hibernate JPA + Bootstrap 搭建的博客系统 Demo 相关阅读: 1.Spring MVC+Hibernate JPA+ Bootstrap 搭建的博 ...
- Spring ORM示例 - JPA,Hibernate,Transaction
Spring ORM示例 - JPA,Hibernate,Transaction 欢迎来到Spring ORM示例教程.今天我们将使用Hibernate JPA事务管理来研究Spring ORM示例. ...
- Hibernate JPA中insert插入数据后自动执行select last_insert_id()解决方法
本文出处:http://blog.csdn.net/chaijunkun/article/details/8647281,转载请注明.由于本人不定期会整理相关博文,会对相应内容作出完善.因此强烈建议在 ...
- Spring Hibernate JPA 联表查询 复杂查询
(转自:http://www.cnblogs.com/jiangxiaoyaoblog/p/5635152.html) 今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的 ...
- Spring ORM+Hibernate?Out!换 Spring Data JPA 吧!
2019独角兽企业重金招聘Python工程师标准>>> 转载请注明出处:http://blog.csdn.net/anxpp/article/details/51415698,谢谢! ...
- spring 配置jdbc/hibernate/jpa
http://www.cnblogs.com/tazi/archive/2012/01/04/2311577.html Spring配置事务的三种方式 步骤:数据源配置-事务配置(xml配置方式或注解 ...
- Spring Boot&JPA&Hibernate&Oracle
在本教程中,我们将展示如何创建一个Spring Boot应用程序,该应用程序通过Hibernate与Oracle数据源进行通信. 先决条件: Eclipse IDE(最新版本) Maven的4 Jav ...
- Primefaces,Spring 4 with JPA(Hibernate 4 / EclipseLink)示例教程
Primefaces,Spring 4 with JPA(Hibernate 4 / EclipseLink)示例教程 Java Persistence API是标准规范.它提供了一个由不同实现者框架 ...
- Spring Boot Data JPA
Spring Data JPA简介 用来简化创建 JPA 数据访问层和跨存储的持久层功能. Spring Data JPA提供的接口 Repository:最顶层的接口,是一个空的接口,目的是为了统一 ...
- Spring Boot整合Jpa多数据源
Spring Boot整合Jpa多数据源 本文是Spring Boot整合数据持久化方案的最后一篇,主要和大伙来聊聊Spring Boot整合Jpa多数据源问题.在Spring Boot整合JbdcT ...
最新文章
- iOS开发几年了,你清楚OC中的这些东西么!!!?
- PXC DDL 操作阻塞写
- icmp报文_用侦察兵的故事趣讲ICMP和Ping,看完想忘都难!
- 高可用集群中的选举机制
- Lucene.Net 精品教程
- C语言之strstr函数类似Java字符串的contain函数
- Android屏幕计算正方形,Android Camera 正方形预览(二)
- 全面开放运营3个月,百度揭秘Apollo最新技术创新
- 50 行代码教你爬取猫眼电影 TOP100 榜所有信息
- 补码转源码 吐血总结
- c语言fltk图形库,FLTK编程模型
- 期刊论文发表会有证书吗
- Transfer learning for deep neural network-based partial differential equations solving论文笔记
- linux文件管理命令ppt,linux命令以及文件管理.ppt
- 用qrcode生成微信支付二维码
- 《MATLAB数学建模方法与实践(第3版)》第2章学习笔记
- ybt1003:对齐输出
- SSD6 exercise1 解题思路
- 文墨绘学:只有不会教的父母,没有教不好的孩子
- CKA和HCIE那个证书含金量高?
热门文章
- 曲奇云盘资源搜索引擎_工具集--任意资源搜索神器(不限速!!)
- linux上设置jar包加载顺序,SpringBoot配置加载顺序
- 【Django 2021年最新版教程20】python for循环遍历queryset
- java并发编程(12)-- 线程池 实际⽣产使⽤哪⼀个线程池 怎么设置
- thinkphp5 insertAll 插入的数据列不对 对应关系不对
- linux目录存 xml文件,将IDEA maven项目中src源代码下的xml等资源文件编译进classes文件夹...
- python语法学习第五天--函数(2)
- java rome,ROME - RSS聚合类库 - 组件类库 - JAVA开源项目 - 开源吧
- typora的安装和配置
- pve 虚拟环境 vi/vim不能右键粘贴设置方法