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导入项目报错:文档中根元素前面的标记必须格式正确

    今天从git上面导入项目之后,由于是上周刚刚提交过的,本地也没有什么修改,于是就从gitlab上面直接下载下来了.可是项目启动时候,报错了... 文档中根元素前面的标记必须格式正确 想想 原来是上次提 ...

  2. C++异常安全

    转自:http://www.cnblogs.com/zgfLawliet/p/3417308.html   异常安全的代码是指,满足两个条件 1异常中立性 : 是指当你的代码(包括你调用的代码)引发异 ...

  3. C#复制粘贴

    用C#程序复制粘贴非常简单,这里为了实用,只介绍对文字的操作,其他情况类似: Clipboard.SetText(“我是需要复制到系统剪贴板的文字”); 执行以上代码后,即可ctrl+V进行粘贴.是不 ...

  4. TreeSet 题

    QUESTION 4Given:12. import java.util.*;13. public class Explorer2 {14. public static void main(Strin ...

  5. linux下设置eclipse中的项目和类名的字体大小

    由于刚装的eclipse中的项目和类名字体太小,并且windows中没有可以更改的方法, 所以参考了网上一些文章,终于修改成功,所以反馈进行分享,望国内开源风气如stackoverflow一样. 步骤 ...

  6. JAVA中的那些名词解释!

    1.JDO: (Java Data Object )是Java对象持久化的新的规范,也是一个用于存取某种数据仓库中的对象的标准化API.作用:用于存取某种数据仓库中的对象 2.JPA: JPA是Jav ...

  7. RabbitMq ack 与 nack

    1.ack 移除 2.nack 移除再次发送队列

  8. slider.js 滑动和点击事件在firefox下报错 event is not defined

    在使用layui的slider滑块控件的时候,firefox遇到了event is not defined 的情况.追究原因是因为layui的layui.js 的滑块功能依赖于silder.js,而官 ...

  9. WAKE-WIN10-SOFT-环境

    操作系统名称 Microsoft Windows 10 专业版版本 10.0.14393 版本 14393其他操作系统描述 没有资料操作系统制造商 Microsoft Corporation系统名称 ...

  10. ssh配置调试的必杀技

    我们知道,ssh客户端的文件及文件夹的权限会影响到身份验证是否通过,可能又不告诉我们为什么,这真是件烦心了事 所以,服务器调试执行就可以看到很多错误信息了 /usr/sbin/sshd -d -p 2 ...