直接上代码:

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

最新文章

  1. Linux移植的一般过程
  2. [转]Oracle 修改或者删除临时表 ORA-14452: 试图创建, 更改或删除正在使用的临时表中的索引
  3. windows8.1 App中webView 使用定位
  4. ubuntu下安装postgres
  5. Live555 分析(二):服务端
  6. 利用python3.5 构建流媒体后台音视频切换的服务端程序
  7. 【JAVAWEB学习笔记】22_ajax
  8. 搭建arm交叉工具链
  9. BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】
  10. percona-toolkit 之 【pt-archiver】
  11. vue-cli脚手架build目录中的webpack.prod.conf.js配置文件
  12. python turtle库的几个小demo
  13. 【原创】大叔经验分享(41)hdfs开启kerberos之后报错Encryption type AES256 CTS mode with HMAC SHA1-96 is not supported/enabled
  14. [Codeforces113C]Double Happiness(数论)
  15. 四则运算 C 语言
  16. DOM-Element对象
  17. sql 思路
  18. 这可能是最全的禁用win10自动更新了
  19. NIO基本概念
  20. 查杀病毒实战----------------》ddg.223 and AnXQV

热门文章

  1. TensorFlow实战第七课(dropout解决overfitting)
  2. 4 基于优化的攻击——CW
  3. Tensor Flow基础(2.0)
  4. ZooKeeper原理及介绍
  5. plpython 中文分词Windows 版
  6. ubuntu 18.04 配置notebook远程连接的坑
  7. Luogu P5221 Product
  8. c++学习笔记之类和对象(三、static静态成员变量和静态成员函数)
  9. jmeter 获取图形验证码接口测试
  10. Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量