title: 如何单独使用spring data jpa

引用pom文件:

        <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.3.5.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>

编写配置类:

package com.config;

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.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager; import javax.persistence.EntityManagerFactory;
import java.util.Properties; /**
* @author Zhai
* 2019/04/02 15:10
*/ @Configuration
@ComponentScan(basePackages = {"com"})
// 指定Repository所在的包
@EnableJpaRepositories(basePackages = {"com.domain"})
public class JpaConfig { // 名字必须是entityManagerFactory,或者把@bean中name属性设置为entityManagerFactory
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
// 设置数据库(如果在hibernate中配置了连接池,则不需要设置)
// em.setDataSource(dataSource());
// 指定Entity所在的包
em.setPackagesToScan("com.domain");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
// 配置属性
Properties properties = new Properties();
properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.setProperty("hibernate.connection.url", "jdbc:mysql://10.8.3.38:3306/test");
properties.setProperty("hibernate.connection.username", "root");
properties.setProperty("hibernate.connection.password", "root");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
properties.setProperty("hibernate.c3p0.min_size", "1");
properties.setProperty("hibernate.c3p0.max_size", "10");
properties.setProperty("hibernate.hbm2ddl.auto", "create");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("format_sql", "true");
em.setJpaProperties(properties);
return em;
}
// 名字必须是transactionManager,或者把@bean中name属性设置为transactionManager
@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}

测试代码:

package com;

import com.config.JpaConfig;
import com.domain.Student;
import com.domain.StudentRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.util.List; /**
* @author Zhai
* 2019/04/02 14:27
*/ public class JpaTest {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JpaConfig.class);
// 获取repository
StudentRepository studentRepository = context.getBean(StudentRepository.class);
Student student1 = new Student();
studentRepository.save(student1); List<Student> students = studentRepository.findAll();
System.out.println(students); }
}

Spring : JPA的单独使用的更多相关文章

  1. spring jpa 实体互相引用返回restful数据循环引用报错的问题

    spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...

  2. spring jpa 自定义查询数据库的某个字段

    spring jpa 提供的查询很强大, 就看你会不会用了. 先上代码, 后面在解释吧 1. 想查单个表的某个字段 在repository中 @Query(value = "select i ...

  3. Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题

    1. 使用过程 2. 背景 3. 遇到问题 3.1 不指定Hibernate数据库方言,默认SQL生成方式 3.2 抛出异常Hibernate加入了@Transactional事务不会回滚 3.3 H ...

  4. Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者

    JPA Audit 在spring jpa中,支持在字段或者方法上进行注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,从字面意思 ...

  5. Spring JPA学习笔记

    目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...

  6. Spring JPA实现逻辑源码分析总结

    1.SharedEntityManagerCreator: entitymanager的创建入口 该类被EntityManagerBeanDefinitionRegistrarPostProcesso ...

  7. 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序

    显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...

  8. spring jpa和mybatis整合

    spring jpa和mybatis整合 前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目 后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查 ...

  9. 一篇搞定spring Jpa操作数据库

    开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...

随机推荐

  1. Idea工具使用

    Idea Project与module的理解 1.基础环境的搭建 1.1.IDEA使用--字体.编码和基本设置 2.插件的安装 2.1.在IDEA中配置Gauge环境 2.2.IdeaVim的安装:: ...

  2. Hadoop Federation联邦

    背景概述 单 NameNode 的架构使得 HDFS 在集群扩展性和性能上都有潜在的问题,当集群大到一定程度后,NameNode 进程使用的内存可能会达到上百 G,NameNode 成为了性能的瓶颈. ...

  3. 200. Number of Islands + 695. Max Area of Island

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  4. nfs 笔记

    问题:客户端在nfs文件目录下读写文件提示Permission denied: 解决方法: 修改/etc/exports 中 文件共享方式为 no_root_squash no_root_squash ...

  5. bzoj3718 [PA2014]Parking

    Description 你的老板命令你将停车场里的车移动成他想要的样子.停车场是一个长条矩形,宽度为w.我们以其左下角顶点为原点,坐标轴平行于矩形的边,建立直角坐标系.停车场很长,我们可以认为它一直向 ...

  6. Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C. Connect Three 【模拟】

    传送门:http://codeforces.com/contest/1087/problem/C C. Connect Three time limit per test 1 second memor ...

  7. 使用Timer组件_实现定时更改窗体颜色

    1 向窗体拖入Timer组件 2 更改其Enable属性为true 3 其interval属性为300 4 在Tick事件中写入随机变色代码 private void timer1_Tick(obje ...

  8. web.xml中Filter的作用

    Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml文件配置好要 ...

  9. vue中图片返回404时,显示默认的图片

    图片返回404时候的处理 <img :src="userMsg.portrait" ref="img" alt=""> _thi ...

  10. mysql5.7.22tar包安装

    mysql5.7.22tar包安装 #卸载系统自带的Mariadb [root@ ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x ...