一.开发环境搭建
先加入依赖
<!-- spring data jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.7.Final</version>
</dependency>
配置类
@Configuration//声明为配置类
@EnableTransactionManagement //启用事务管理
@EnableJpaRepositories(basePackages = "com.playmaker")
@ComponentScan("com.playmaker") //扫描这个包下的
public class SpringConfigNew {
@Autowired
private SpringJdbcProperties springJdbcProperties;
//1.配置数据源
@Bean
public DriverManagerDataSource getDriverManagerDataSource(){
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName(springJdbcProperties.getDriverClass());
driverManagerDataSource.setUrl(springJdbcProperties.getUrl());
driverManagerDataSource.setUsername(springJdbcProperties.getUser());
driverManagerDataSource.setPassword(springJdbcProperties.getPassword());
return driverManagerDataSource;
}
//2.配置EntityManagerFactory
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(getDriverManagerDataSource());
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setGenerateDdl(true);
jpaVendorAdapter.setDatabase(Database.MYSQL);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactoryBean.setPackagesToScan("com.playmaker");
//配置jpa的属性
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.ejb.naming_strategy","org.hibernate.cfg.ImprovedNamingStrategy");
jpaProperties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5InnoDBDialect");//什么数据库
jpaProperties.setProperty("hibernate.show_sql","true");//显示
jpaProperties.setProperty("hibernate.format_sql","true");//格式化
jpaProperties.setProperty("hibernate.hbm2ddl.auto","update");//自动生成
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
//3.配置事务管理器
@Bean
public JpaTransactionManager transactionManager(){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setDataSource(getDriverManagerDataSource());
transactionManager.setEntityManagerFactory(this.entityManagerFactory().getObject());
return transactionManager;
}
}
先开发实体类->自动生成数据表
@Entity//实体类
@Table(name = "employee")
public class Employee {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
@GeneratedValue//自增
@Id//id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length = 20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
测试类(现在数据库中并没有employee表,运行这个,看看控制台)
public class SpringConfigNewTest {
private ApplicationContext applicationContext = null;
@Before
public void init(){
applicationContext = new AnnotationConfigApplicationContext(SpringConfigNew.class);
System.out.println("init...");
}
@After
public void destroy(){
applicationContext=null;
System.out.println("destroy...");
}
@Test
public void testentityManagerFactory(){
}
}
二.起步程序入门
编写EmployeeRepository接口
interface EmployeeRepository extends Repository<Employee, Integer> {//类名 + 主键类型
//获取雇员对象通过名称
Employee findByName(String name);
}
测试类
public class EmployeeRepositoryTest {
private ApplicationContext applicationContext = null;
private EmployeeRepository employeeRepository = null;
@Before
public void init(){
applicationContext = new AnnotationConfigApplicationContext(SpringConfigNew.class);
employeeRepository = applicationContext.getBean(EmployeeRepository.class);
System.out.println("init...");
}
@After
public void destroy(){
applicationContext=null;
System.out.println("destroy...");
}
@Test
public void findByNametest(){
Employee employee = employeeRepository.findByName("test1");//先往数据库中插入一条数据
System.out.println(employee);
}
}
为什么接口只是写了一个方法,并没有实现它能够直接这样用?因为底层会根据一定规则来提供很好的服务,所以接口的方法不能随便改名字(改了就报错),方法名字有规则的
Repository:Spring Data核心类,演示的就是继承这个类
还有种方法是使用注解@RepositoryDefinition(domainClass = Employee.class,idClass = Integer.class),下章会讲
Repository Query Specification:查询时,方法命名不能乱写
Query Annotation:使用该注解,可以实现原生SQL查询
Update/Delete/Transaction:更新、删除操作,支持事务
Repository Hierarchy
CrudRepository:内置了新增、更新、删除、查询方法
PagingAndSortingRespository:分页和排序
JpaRepository
JpaSpecificationExcutor JPA的特殊操作