总依赖组件坐标:

    <properties>
<spring.version>5.2.8.RELEASE</spring.version>
<hibernate.version>5.0.7.Final</hibernate.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency> <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency> <dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency> <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.3.2.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.8.RELEASE</version>
</dependency> <dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency> <dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency> </dependencies>

设置一个配置类的包

cn.xxx.config

设置总配置加载类

package cn.echo42.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 10:20
*/ @Configuration
@Import({SpringDataJpaConfiguration.class,DataSourceConfiguration.class})
@ComponentScan("cn.echo42")
public class ApplicationConfiguration {
}

其中导入了数据源和SpringDataJpa的配置类

数据源配置:

太懒,没写配置文件了

package cn.echo42.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import javax.sql.DataSource;
import java.beans.PropertyVetoException; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 10:22
*/ @Configuration
public class DataSourceConfiguration { @Bean("dataSource")
public DataSource getDataSource(
@Value("com.mysql.cj.jdbc.Driver") String driverClass,
@Value("jdbc:mysql:///oa?serverTimezone=Asia/Shanghai") String url,
@Value("root") String user,
@Value("123456") String password) throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setDriverClass(driverClass);
comboPooledDataSource.setJdbcUrl(url);
comboPooledDataSource.setUser(user);
comboPooledDataSource.setPassword(password);
return comboPooledDataSource;
} }

SpringDataJpa配置:

package cn.echo42.config;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
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.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 11:06
*/ @Configuration
//@Import(DataSourceConfiguration.class)
@EnableJpaRepositories(
basePackages = "cn.echo42.dao",
transactionManagerRef = "jpaTransactionManager",
entityManagerFactoryRef = "entityManagerFactory"
)
public class SpringDataJpaConfiguration { @Bean("persistenceProvider") /* Jpa实现者 即规范供应商 */
public HibernatePersistenceProvider getProvider(){
return new HibernatePersistenceProvider();
} @Bean("jpaDialect") /* Jpa方言对象 */
public HibernateJpaDialect getHibernateJpaDialect(){
return new HibernateJpaDialect();
} @Bean("adapter") /* 实现者适配器 */
public HibernateJpaVendorAdapter getHibernateJpaVendorAdapter(){
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setDatabase(Database.MYSQL);
adapter.setGenerateDdl(false);
adapter.setShowSql(true);
adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
return adapter;
} @Bean("entityManagerFactory")
public LocalContainerEntityManagerFactoryBean getEntityManagerFactoryBean (
DataSource dataSource,
@Value("cn.echo42.entity") String scanPath,
HibernatePersistenceProvider hibernatePersistenceProvider,
HibernateJpaDialect hibernateJpaDialect) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan(scanPath);
factoryBean.setPersistenceProvider(hibernatePersistenceProvider);
factoryBean.setJpaDialect(hibernateJpaDialect);
return factoryBean;
} @Bean("jpaTransactionManager") /* Jpa事务管理器 */
public JpaTransactionManager getJpaTransactionManager(EntityManagerFactory factoryBean) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(factoryBean);
return jpaTransactionManager;
} }

用户实体类:

package cn.echo42.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; import javax.persistence.*; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 10:54
*/ @Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "sys_user")
public class User { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer user_id; // @Column(name = "user_id")
private String user_name; // @Column(name = "user_name")
private String user_password; // @Column(name = "user_password")
private Integer user_status; // @Column(name = "user_status")
private Integer user_is_del; // @Column(name = "user_is_del")
}

用户持久化接口:

package cn.echo42.dao;

import cn.echo42.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 10:58
*/
public interface UserDao extends JpaRepository<User,Integer> , JpaSpecificationExecutor<User> { }

最后的测试类:

import cn.echo42.config.ApplicationConfiguration;
import cn.echo42.dao.UserDao;
import cn.echo42.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; /**
* @author DaiZhiZhou
* @file SpringDataJpa
* @create 2020-07-31 10:52
*/ @RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(locations = "classpath:Bean-Context.xml") // XML加载
@ContextConfiguration(classes = ApplicationConfiguration.class) // JavaConfig
public class SpringDataJpaTest { @Autowired
private UserDao userDao; @Test
public void quickStart(){
List<User> userList = userDao.findAll();
for (User user : userList) {
System.out.println(user);
}
}
}

测试结果:

User(user_id=1, user_name=admin, user_password=admin, user_status=1, user_is_del=0)
User(user_id=2, user_name=user01, user_password=123456, user_status=1, user_is_del=0)
User(user_id=3, user_name=user02, user_password=123456, user_status=1, user_is_del=0)
User(user_id=4, user_name=user03, user_password=123456, user_status=1, user_is_del=0)
User(user_id=5, user_name=admin02, user_password=admin02, user_status=1, user_is_del=0)

如果需要日志打印:

在Resource目录种配置日志【log4j.properties】

log4j.rootLogger=DEBUG,console,file
# Settings About ConsoleOutput
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Target=System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
# Settings About FileOutput
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./resources/log/logging.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
# LogOutput Level
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

【Spring Data JPA】06 全注解配置(非SpringBoot整合)的更多相关文章

  1. Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

    上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...

  2. Springboot spring data jpa 多数据源的配置01

    Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制) 例: user数据库   global 数据库 ...

  3. Spring Data Jpa 详解 (配置篇)

    前言: JPA全称Java Persistence API,即Java持久化API,它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据,结合其他ORM的使用,能达到简化开发 ...

  4. Spring Boot,Spring Data JPA多数据源支持配置

    1 配置文件 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver wisely.primary.datasource. ...

  5. Spring Data JPA 中常用注解

    一.java对象与数据库字段转化 1.@Entity:标识实体类是JPA实体,告诉JPA在程序运行时生成实体类对应表 2.@Table:设置实体类在数据库所对应的表名 3.@Id:标识类里所在变量为主 ...

  6. spring data jpa使用@Transactional注解开启事务后失败不回滚

    如题,在数据库批量操作方法上使用@Transactional注解,其中一条数据抛出异常了,却死活不回滚. 批量操作方法是公有的,spring也是默认支持事务的,排除代码层面问题,那么就看看数据库是否支 ...

  7. Spring Data JPA之@Query注解

    比如有个实体类对象,类名为Book,对应数据表的表名为book 1. 一个使用@Query注解的简单例子:占位符?1和?2 @Query(value = "select name,autho ...

  8. Spring Data Jpa使用@Query注解实现模糊查询(LIKE关键字)

    /** * * @param file_name 传入参数 * @return */ @Query(value = "select * from user where name LIKE C ...

  9. 【Spring】Spring Data JPA

    原始JDBC操作数据库 传统JDBC方式实现数据库操作 package com.imooc.util; import java.io.InputStream; import java.sql.*; i ...

  10. Spring Data JPA 和MyBatis比较

    现在Dao持久层的解决方案中,大部分是采用Spring Data JPA或MyBatis解决方案,并且传统企业多用前者,互联网企业多用后者. Spring Data JPA 是Spring Data ...

随机推荐

  1. Python BeautifulSoup定位取值

    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ...

  2. AT_agc044_c

    problem & blog 由于看到和三进制有关的操作,可以想到建造每个结点都有三个儿子的 Trie.考虑维护两种操作. 1.Salasa 舞 对于这种操作,就是把每一个节点的第一个儿子和第 ...

  3. TiDB 多集群告警监控-初章-监控融合、自动告警处理

    author:longzhuquan 背景 随着公司XC改造步伐的前进,越来越多的业务选择 TiDB,由于各个业务之间需要物理隔离,避免不了的 TiDB 集群数量越来越多.虽然每套 TiDB 集群均有 ...

  4. Spring扩展——BeanPostProcessor(BPP)

    BeanPostProcess简介 在Spring中 BeanPostProcessor 是一个非常重要的接口,它用于在每个bean对象初始化前后修改Bean的属性信息,比如我们最常用的@Autowi ...

  5. k8s健康检查(探针Probe)之LivenessProbe、ReadinessProbe和StartupProbe

    背景 集群正常服务时,会出现容器死掉问题,如宿主机故障.资源不足.下游故障等.这个时候容器需要从endpoints摘除(容器挂了就不能接流了),并执行它的restart策略. LivenessProb ...

  6. 03-Python数据类型

    None类型 Python3中没有NULL,取而代之的是空类型None.空列表.空字典等. None是一个特殊的Python对象,表示无. None的类型是NoneType. 如果只想声明变量,而不想 ...

  7. 【FAQ】HarmonyOS SDK 闭源开放能力 —IAP Kit(2)

    1.问题描述: 应用内支付IAP Kit和Payment Kit的区别以及适用场景? 解决方案: IAP Kit是四方支付,仅支持在线虚拟商品,如会员,游戏钻石等,双框架支持全球,目前单框架暂时只支持 ...

  8. spark中各窗口函数对于窗口的要求

    窗口参数: class WindowSpec private[sql]( partitionSpec: Seq[Expression], orderSpec: Seq[SortOrder], fram ...

  9. 【基础整理】Mapping representation 机器人所用地图种类及相关介绍

    参考与前言 本文主要介绍 建图 Mapping 方面的一些 基础知识介绍与相关下游任务使用 涉及知识较为基础,SLAM大佬们可以提前退出了 主要针对应用为移动机器人与物流无人驾驶车:提前申明:大部分文 ...

  10. vue - ES6模块化、promise、webpack打包(所在在学的朋友们先看这篇,看了不吃亏)

    首先我要说明一下,没错,还是没有进入vue,刘备请诸葛亮三次都可以了吧,我这也是第三次了,也绝对是最后一次了,我应经摸透了因为,最后的webpack打包加上一个git学了过后我就去vue了. 为什么要 ...