直接上代码:

pom.xml

        <!-- hibernate start -->
<!-- spring data jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<!-- hibernate end -->

项目结构:

the_data_jpa 包 里面的 各类 代码

User

package com.oukele.the_data_jpa.entity;

import org.springframework.stereotype.Component;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "Id")
private int id; @Column(name = "userName")
private String name;
private String password; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

UserDao

package com.oukele.the_data_jpa.dao;

import com.oukele.the_data_jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserDao extends JpaRepository<User, Integer> { User findByNameAndPassword(String name,String password); }

UserService

package com.oukele.the_data_jpa.service;

import com.oukele.the_data_jpa.dao.UserDao;
import com.oukele.the_data_jpa.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class UserService {
@Autowired
private UserDao userDao; public User findNameAndPassword(String name,String password){
return userDao.findByNameAndPassword(name, password);
} public List<User> list(){
List<User> list = userDao.findAll();
return list;
}
}

SpringConfig

package com.oukele.the_data_jpa;

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.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
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 javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.util.Properties; @Configuration//声明当前配置类
@ComponentScan(basePackages = "com.oukele.the_data_jpa")// 扫描当前包 使用 spring 注解
@PropertySource("classpath:jdbc.properties")//加载 资源文件
@EnableJpaRepositories(basePackages = "com.oukele.the_data_jpa.dao")//扫描 使用 jpa 注解的接口
public class SpringConfig { //配置数据源
@Bean
DataSource dataSource(Environment env) throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(env.getProperty("jdbc.driver"));
dataSource.setJdbcUrl(env.getProperty("jdbc.url"));
dataSource.setUser(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.password"));
return dataSource;
} // @Bean
// public JdbcTemplate jdbcTemplate() {
// JdbcTemplate jdbcTemplate = new JdbcTemplate();
// jdbcTemplate.setDataSource(dataSource1);
// return jdbcTemplate;
// } @Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} //SqlSessionFactory
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){ LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource);
//加载 实体类
bean.setPackagesToScan("com.oukele.the_data_jpa.entity");
//设置 适配器
bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); Properties properties = new Properties();
//更新表结构
properties.setProperty("hibernate.hbm2ddl.auto", "update");
//设置方言
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
bean.setJpaProperties(properties); return bean;
} }

Main ( 测试 类)

package com.oukele.the_data_jpa;

import com.oukele.the_data_jpa.entity.User;
import com.oukele.the_data_jpa.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.List; public class Main { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService service = context.getBean(UserService.class); User user = service.findNameAndPassword("oukele","oukele");
System.out.println(user.getName()); List<User> list = service.list();
for (User user1 : list) {
System.out.println(user1.getName()+" - "+user1.getPassword());
}
}
}

测试结果:

 oukele
oukele - oukele

jdbc.properties文件:

jdbc.driver=org.mariadb.jdbc.Driver
jdbc.url=jdbc:mariadb://localhost:3306/test
jdbc.user=oukele
jdbc.password=oukele

示例代码下载:https://github.com/oukele/spring-hibernate-spring-data-jpa

Spring整合Hibernate实现Spring Data JPA (简单使用)的更多相关文章

  1. Spring整合Hibernate实现Spring Data JPA (介绍和使用)

    Spring Data JPA是Spring基于Hibernate开发的一个JPA框架.如果用过Hibernate或者MyBatis的话,就会知道对象关系映射(ORM)框架有多么方便. 但是Sprin ...

  2. 【SSH框架】系列之 Spring 整合 Hibernate 框架

    1.SSH 三大框架整合原理 Spring 与 Struts2 的整合就是将 Action 对象交给 Spring 容器来负责创建. Spring 与 Hibernate 的整合就是将 Session ...

  3. Spring Data Jpa简单了解

    原文来源:http://www.cnblogs.com/xuyuanjia/p/5707681.html 以下是自己简单整理原有文章,其实就是在原来文章基础上化重点以及可能会有所删减的方式进行整理,需 ...

  4. Spring 整合 Hibernate

    Spring 整合 Hibernate •Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. •Spring 对这些 OR ...

  5. 3、Spring整合Hibernate

    经过前面的两节分析:1.Hibernate之生成SessionFactory源码追踪 和 2.Spring的LocalSessionFactoryBean创建过程源码分析 .我们可以得到这样一个结论, ...

  6. Spring学习笔记六:Spring整合Hibernate

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html  前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...

  7. Spring整合Hibernate的方法

    一.基本支持 Spring 支持大多数流行的 ORM 框架, 包括 Hibernate JDO, TopLink, Ibatis 和 JPA. Spring 对这些 ORM 框架的支持是一致的, 因此 ...

  8. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  9. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

随机推荐

  1. 【Linux开发】linux设备驱动归纳总结(四):1.进程管理的相关概念

    linux设备驱动归纳总结(四):1.进程管理的相关概念 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  2. mac环境提示:make sure that /usr/local/bin is in your path

    今天我在Mac环境下安装了Homebrew和node.js后,输入node -v.brew.npm都提示: command not found,然后我查看了下Homebrew的安装日志,发现日志里有个 ...

  3. spring data 返回任意字段

    在spring boot + spring data查询数据库的过程中,有时候总会出现一些复杂的查询,我们希望数据库返回的字段能随意改变.这个需求在mybatis里很好解决,只需要用map接收就可以, ...

  4. luoguP3390(矩阵快速幂模板题)

    链接:https://www.luogu.org/problemnew/show/P3390 题意:矩阵快速幂模板题,思路和快速幂一致,只需提供矩阵的乘法即可. AC代码: #include<c ...

  5. Spark中分布式使用HanLP(1.7.0)分词示例

    HanLP分词,如README中所说,如果没有特殊需求,可以通过maven配置,如果要添加自定义词典,需要下载“依赖jar包和用户字典". 分享某大神的示例经验: 是直接"java ...

  6. Ubuntu - Ubuntu应用记录(2)

    1.安装Ubuntu16.04的一种分区分案(240G固态硬盘小例) 1.创建boot分区(引导分区)-> 512M ->逻辑分区->空间起始位置->Ext4日志文件系统-&g ...

  7. mysql语法难点

    select * from emp where comm is null or comm=0;/*没有提成的员工*/ 查询有提成的员工所有信息 select * from emp where comm ...

  8. CSP 最大的矩形(201312-3)

    问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...

  9. 阻塞I/O、非阻塞I/O和I/O多路复用、怎样理解阻塞非阻塞与同步异步的区别?

    “阻塞”与"非阻塞"与"同步"与“异步"不能简单的从字面理解,提供一个从分布式系统角度的回答.1.同步与异步 同步和异步关注的是消息通信机制 (syn ...

  10. centos7 上pip install mysqlclient的时候报错OSError: mysql_config not found,

    yum install mysql-devel gcc gcc-devel python-devel