Spring boot 集成Spring Data JPA+数据源
2018-09-19 10:52
176 查看
Spring boot 集成Spring Data JPA+数据源
看了很多篇springboot 集成jpa的博客,但都是千篇一律,按部就班的编码后运行各种错误,根据自己的研究以下是自己编码的一个demo
- 配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://192.168.3.114:3306/har-hshare?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull spring.datasource.name=root spring.datasource.password=root spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource spring.datasource.dbcp2.max-wait-millis=10000 spring.datasource.dbcp2.min-idle=5 spring.datasource.dbcp2.initial-size=5 #spring.datasource.dbcp2.validation-query=SELECT x spring.datasource.dbcp2.connection-properties=characterEncoding=utf8 spring.data.jpa.repositories.enabled=true spring.jpa.show-sql=true spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect spring.jpa.database=mysql spring.jpa.generate-ddl=true spring.jpa.open-in-view=true
2.dao
public interface AppCustomerDao extends JpaRepository<AppCustomerEntity,Long>,JpaSpecificationExecutor<AppCustomerEntity> { AppCustomerEntity findByAppCode(String code); AppCustomerEntity findByAppCodeAndAppType(String appCode,String appType); }``` 3.启动入口类
@SpringBootApplication(exclude ={DataSourceAutoConfiguration.class})
@ComponentScan(basePackages ={“com.pccb”})
@EnableJpaRepositories(basePackages = “com.pccb”)
public class BusinessApplication {
public static void main(String[] args) {
SpringApplication.run(BusinessApplication.class, args);
}
}
4 .ApplicationConfig
@Configuration
@EnableJpaRepositories(basePackages = “com.pccb.dao”)
@EnableTransactionManagement
public class ApplicationConfig {
@Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.name}") private String userName; @Value("${spring.datasource.password}") private String pwd; @Value("${spring.datasource.driver-class-name}") private String driverClass; @Bean(name="dataSource",destroyMethod = "") public DataSource dataSource() { //EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); BasicDataSource basicDataSource = new BasicDataSource(); basicDataSource.setUrl(url); basicDataSource.setDriverClassName(driverClass); basicDataSource.setUsername(userName); basicDataSource.setPassword(pwd); return basicDataSource; } @Bean public EntityManagerFactory entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); //vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("com.pccb.entity"); factory.setDataSource(dataSource()); factory.afterPropertiesSet(); return factory.getObject(); } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; }
}
5.test类 @RunWith(SpringRunner.class) @SpringBootTest(classes = BusinessApplication.class) public class CustomerServiceTest { @Autowired private CustomerService customerService; @Transactional @Test public void queryCustomer() { List<AppCustomerEntity> appCustomerEntity = customerService.queryCustomer(); System.out.println("------------------------------【】" + appCustomerEntity); } } ![在这里插入图片描述](https://img-blog.csdn.net/20180919103641343?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2xkazEwODMyMzM0NDc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) pom文件com.com.pccb common 0.0.1-SNAPSHOT org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java 5.1.24 org.projectlombok lombok io.jsonwebtoken jjwt 0.9.0 org.apache.commons commons-io 1.3.2 org.mvel mvel2 2.3.1.Final eu.bitwalker UserAgentUtils 1.20 org.apache.commons commons-dbcp2 2.1.1 ```
加粗样式
阅读更多相关文章推荐
- 从零开始学spring-boot(2)-集成spring-data-jpa
- Spring Boot + Spring Data JPA项目配置多数据源
- SpringBoot学习-(4)集成SpringDataJpa
- Spring Boot 系列(九)数据层-集成Spring-data-jpa
- spring boot集成data-jpa
- Spring Boot 系列(九)数据层-集成Spring-data-jpa
- 从零开始学spring-boot(2)-集成spring-data-jpa
- spring-boot集成spring-data-jpa
- spring-boot学习笔记(二)spring-data-jpa的简介以及项目集成
- springboot集成spring-data-jpa
- SpringBoot | 第三十章:Spring-data-jpa的集成和使用
- spring boot 集成spring data jpa 时报错java.lang.ClassNotFoundException: org.hibernate.OptimisticLockExcep
- 4. Spring Boot 1.2.5,Spring Data JPA多数据源支持
- 在spring Boot中使用Spring-data-jpa操作数据库
- 74. Spring Data JPA方法定义规范【从零开始学Spring Boot】
- spring boot(五):spring data jpa的使用
- SpringBoot使用Spring-Data-Jpa实现CRUD操作
- SpringBoot22-spingboot数据访问-Spring Data JPA
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 使用spring-boot-starter-data-jpa 怎么配置使运行时输出SQL语句