spring使用 hibernate jpa JpaRepository
使用JpaRepository需要两个架包:
<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-core</artifactId> <version>5.3.6.Final</version> </dependency>
1、创建实体类:Role
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.Jpa.entity; import com.sun.javafx.geom.transform.Identity; import javax.persistence.*; @Entity @Table public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; //比如数据库名字是names 指定设置为: @Column(name = "role_name") private String roleName; private String note; @Override public String toString() { return "Role{" + "id=" + id + ", roleName='" + roleName + '\'' + ", note='" + note + '\'' + '}'; } public Role() { } public Role(int id, String roleName, String note) { this.id = id; this.roleName = roleName; this.note = note; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } }
2、配置:JavaConfig
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.Jpa.config; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; 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 org.springframework.transaction.annotation.TransactionManagementConfigurer; import org.springframework.transaction.support.TransactionTemplate; import javax.sql.DataSource; import java.beans.PropertyVetoException; import java.util.Properties; @Configuration @ComponentScan("com.wbg.Jpa") @EnableTransactionManagement @EnableJpaRepositories(basePackages = {"com.wbg.Jpa.dao"}) public class JavaConfig { @Bean(name = "dataSource") public DataSource getDataSource() { ComboPooledDataSource dataSource = new ComboPooledDataSource(); try { dataSource.setDriverClass("org.mariadb.jdbc.Driver"); } catch (PropertyVetoException e) { e.printStackTrace(); } dataSource.setJdbcUrl("jdbc:mariadb://localhost:3306/wbg_logistics"); dataSource.setUser("root"); dataSource.setPassword("123456"); dataSource.setMaxPoolSize(30); return dataSource; } @Bean public JdbcTemplate jdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(getDataSource()); return jdbcTemplate; } @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource){ LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean(); bean.setDataSource(dataSource); bean.setPackagesToScan("com.wbg.Jpa.entity"); bean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); Properties properties = new Properties(); /** * validate 加载hibernate时,验证创建数据库表结构 * create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。。 * create-drop 加载hibernate时创建,退出是删除表结构 * update 加载hibernate自动更新数据库结构 */ properties.setProperty("hibernate.hbm2ddl.auto","update"); //格式化输出语句 /**列如 * Hibernate: select role0_.id as id1_0_, role0_.note as note2_0_, role0_.role_name as role_nam3_0_ from Role role0_ *格式化韦 * select * role0_.id as id1_0_, * role0_.note as note2_0_, * role0_.role_name as role_nam3_0_ * from * Role role0_ */ properties.setProperty("hibernate.format_sql","true"); //显示执行sql语句 properties.setProperty("hibernate.show_sql","true"); //设置方言 properties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect"); bean.setJpaProperties(properties); return bean; } }
3、接口RoleDao
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.Jpa.dao; import com.wbg.Jpa.entity.Role; import org.springframework.data.jpa.repository.JpaRepository; public interface RoleDao extends JpaRepository<Role,Integer> { }
4、实现类:RoleService
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wbg.Jpa.dao; import com.wbg.Jpa.entity.Role; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class RoleService { @Autowired private RoleDao roleDao; public List<Role> listAll() { List<Role> list = roleDao.findAll(); return list; } }
测试:
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfig.class); RoleService roleDao = applicationContext.getBean(RoleService.class); for (Role role : roleDao.listAll()) { System.out.println(role); }