在pom文件中添加spring data jpa依赖
org.springframework.data
spring-data-jpa
3.1.4
org.hibernate
hibernate-core
6.3.1.Final
org.apache.derby
derby
10.14.2.0
test
junit
junit
4.13.1
test
这里使用的是spring data jpa的3.1.4版本,另外引入了hibernate、derby、junit等包
在src/test/java的cn.horse.chapter01.entity包下创建User实体类
package cn.horse.chapter01.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity(name = "T_USER")
public class User {
@Id
private String id;
private String username;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}
@Entity:实体表映射,name用于指定表名称
@Id:指定主键
在src/test/java的cn.horse.chapter01.repository包下创建UserRepository持久化类
package cn.horse.chapter01.repository;
import cn.horse.chapter01.entity.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository {
}
UserRepository继承CrudRepository类,这样UserRepository类将可以使用父类中提供的CRUD方法;另外继承CrudRepository类需要指定泛型,第一个泛型类型是持久化实体类型,第二个泛型类型是持久化主键类型
在src/test/java的cn.horse.chapter01.config包下创建ApplicationConfig配置类
package cn.horse.chapter01.config;
import jakarta.persistence.EntityManagerFactory;
import org.hibernate.cfg.AvailableSettings;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableJpaRepositories(basePackages = "cn.horse.chapter01.repository")
@EnableTransactionManagement
class ApplicationConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
driverManagerDataSource.setUrl("jdbc:derby:demo;create=true");
driverManagerDataSource.setUsername("");
driverManagerDataSource.setPassword("");
return driverManagerDataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("cn.horse.chapter01.entity");
factory.setDataSource(dataSource());
Properties jpaProperties = new Properties();
jpaProperties.put(AvailableSettings.HBM2DDL_AUTO, "create");
factory.setJpaProperties(jpaProperties);
return factory;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory);
return txManager;
}
}
@EnableJpaRepositories:配置可用持久化类,另外我们还配置了持久化类扫描的包路径(cn.horse.chapter01.repository)
@EnableTransactionManagement:配置可用事务管理
另外还将数据源(DataSource)、实体管理工厂(EntityManagerFactory)、Jpa事务管理(PlatformTransactionManager)实例注册到Spring容器中。
其中实体管理工厂(EntityManagerFactory)实例的创建由LocalContainerEntityManagerFactoryBean对象完成,LocalContainerEntityManagerFactoryBean对象创建的过程中设置了实体类扫描的包路径(cn.horse.chapter01.entity),属性中还设置了DDL方式(create),此方式会先删除表后再创建新表。
在src/test/resources的cn/horse/chapter01目录下创建data.sql脚本
INSERT INTO T_USER(id, username) VALUES('1', '张三'),('2', '李四'),('3', '王五');
在src/test/java的cn.horse.chapter01包下创建SpringDataJpaTest测试类
package cn.horse.chapter01;
import cn.horse.chapter01.entity.User;
import cn.horse.chapter01.repository.UserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Iterator;
public class SpringDataJpaTest {
static final ApplicationContext applicationContext;
static final UserRepository userRepository;
static {
applicationContext = new AnnotationConfigApplicationContext("cn.horse.chapter01.config");
userRepository = applicationContext.getBean(UserRepository.class);
try {
ScriptUtils.executeSqlScript(applicationContext.getBean(DataSource.class).getConnection(), new ClassPathResource("cn/horse/chapter01/data.sql"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Test
public void testFindAll() {
Iterator iterator = userRepository.findAll().iterator();
Assert.assertEquals("1", iterator.next().getId());
Assert.assertEquals("2", iterator.next().getId());
Assert.assertEquals("3", iterator.next().getId());
Assert.assertEquals(false, iterator.hasNext());
}
@Test
public void testCount() {
Assert.assertEquals(3, userRepository.count());
}
@Test
public void testInsert() {
long historyCount = userRepository.count();
User user = new User();
user.setId("4");
user.setUsername("赵六");
userRepository.save(user);
Assert.assertEquals(historyCount + 1, userRepository.count());
Assert.assertNotNull(userRepository.findById("4").orElse(null));
Assert.assertEquals("赵六", userRepository.findById("4").map(User::getUsername).orElse(null));
}
@Test
public void testUpdate() {
long historyCount = userRepository.count();
User user = new User();
user.setId("1");
user.setUsername("张三2");
userRepository.save(user);
Assert.assertEquals(historyCount, userRepository.count());
Assert.assertNotNull(userRepository.findById("1").orElse(null));
Assert.assertEquals("张三2", userRepository.findById("1").map(User::getUsername).orElse(null));
}
@Test
public void testDelete() {
long historyCount = userRepository.count();
userRepository.deleteById("3");
Assert.assertEquals(historyCount - 1, userRepository.count());
Assert.assertNull(userRepository.findById("3").orElse(null));
}
}
执行单元测试通过