Spring : JPA的单独使用
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的单独使用的更多相关文章
- spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...
- spring jpa 自定义查询数据库的某个字段
spring jpa 提供的查询很强大, 就看你会不会用了. 先上代码, 后面在解释吧 1. 想查单个表的某个字段 在repository中 @Query(value = "select i ...
- Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题
1. 使用过程 2. 背景 3. 遇到问题 3.1 不指定Hibernate数据库方言,默认SQL生成方式 3.2 抛出异常Hibernate加入了@Transactional事务不会回滚 3.3 H ...
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
JPA Audit 在spring jpa中,支持在字段或者方法上进行注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,从字面意思 ...
- Spring JPA学习笔记
目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...
- Spring JPA实现逻辑源码分析总结
1.SharedEntityManagerCreator: entitymanager的创建入口 该类被EntityManagerBeanDefinitionRegistrarPostProcesso ...
- 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序
显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...
- spring jpa和mybatis整合
spring jpa和mybatis整合 前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目 后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查 ...
- 一篇搞定spring Jpa操作数据库
开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...
随机推荐
- java、C语言实现数组模拟栈
java: public class ArrayStack { private int[] data; private int top; private int size; public ArrayS ...
- 【Leetcode】【Easy】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- QT的mouseMoveEvent事件失效
void TalkWindow::enterEvent(QEvent *event){ grabMouse();}void TalkWindow::leaveEvent(QResizeEvent *e ...
- Java集合工具类
import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map ...
- 双十一问题:kafka消费能力低下原因思考
抛去cpu.内存等机器原因,在每个分区皆分配一个进程消费的情况下,利用扩机器来提高kafka消费速率已无能为力 此时发现,在实际洪峰时段的消费速率元达不到先前压测时的消费速率 原因思考: 1.洪峰时段 ...
- 027class_part1
因为有基础,我直接简单写了##定义类,创建对象,调用对象方法,返回值 class person: def speak(self,x): print('love',x) return x + '**** ...
- php生成csv文件并提供下载及相关注意事项
1.生成文件过程略,只要逗号分割就可以了 2.提供下载加上如下代码: header("Content-type: application/octet-stream"); heade ...
- linux 里的`反引号
Shell中可以将数字或字符直接赋予变量,也可以将Linux命令的执行结果赋予变量,如下: (1) $ count=9 #将数字赋予变量count (2) $ name=" ...
- WebSphere集群环境修改IHS端口号的方法
参考资料:http://wenku.baidu.com/link?url=E9BkuEjJ16i9lg7l91L0-xhKCYkHV0mAnlwAeSlDCFM4TjZyk4ZVxmUu64BGd4F ...
- 从数据库反向生成django的models
有办法实现django 数据库反向生成models的方法吗?答案是肯定的. 1. 配置 settings.py 中的数据库配置部分 DATABASES = { 'default': { 'ENGINE ...