spring boot 使用 spring data jpa
2016-10-17 16:28
696 查看
1. pom.xml加入jpa和相关数据库依赖(以下为mysql):
2. 在application.properties加入配置:
3. 创建实体类
采用@Entity注解注明为实体类,@Id标注主键,@GeneratedValue标注主键自动生成方式为自增,hibernate将自动映射为实体表。
4. 创建数据访问接口
PagingAndSortingRepository提供了标准增删改查的功能之外,还允许分页排序
5. 使用jpa提供的基本增删改查方法
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
2. 在application.properties加入配置:
//数据库驱动类型 spring.datasource.driver-class-name=com.mysql.jdbc.Driver //数据库连接地址 spring.datasource.url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=utf-8&useSSL\=true spring.datasource.username=root spring.datasource.password=123 spring.jpa.database=mysql //发现实体类属性变动表结构也更新 spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
3. 创建实体类
采用@Entity注解注明为实体类,@Id标注主键,@GeneratedValue标注主键自动生成方式为自增,hibernate将自动映射为实体表。
4. 创建数据访问接口
public interface UserRepository extends PagingAndSortingRepository<User,Integer>(){ }
PagingAndSortingRepository提供了标准增删改查的功能之外,还允许分页排序
5. 使用jpa提供的基本增删改查方法
@Autowired private UserRepository userRepository; //查询单条 User user = userRepository.findOne(id); //分页查询 Page<User> users = userRepository.findAll(Pagable pageable); //新建 User user = userRepository.save(User user); ...
相关文章推荐
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot中使用Spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- • Spring Boot 进 阶- 数 据 访 问-使用spring-data-jpa 操作数据库
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- springboot(五):spring data jpa的使用
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- spring boot(五):spring data jpa的使用
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- 深入学习spring-boot系列(二)--使用spring-data-jpa
- Spring Boot中使用Spring-data-jpa让数据访问更简单、更优雅
- 深入学习spring-boot系列(二)--使用spring-data-jpa