Spring boot 集成Spring Data JPA+数据源

Spring boot 集成Spring Data JPA+数据源

看了很多篇springboot 集成jpa的博客,但都是千篇一律,按部就班的编码后运行各种错误,根据自己的研究以下是自己编码的一个demo

  1. 配置文件
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,JpaSpecificationExecutor {

    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 = 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 ```

加粗样式

你可能感兴趣的:(java)