spingboot集成jpa
Posted 胖子学习天地
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spingboot集成jpa相关的知识,希望对你有一定的参考价值。
springboot + jpa 练习
spingboot集成jpa(一):最基本的环境搭建
spingboot集成jpa(二):使用单元测试
1. pom.xml中添加依赖
<!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2. application.propertis添加配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
3. 实体类User.java创建
package com.king.entity; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="m_user") public class User{ @Id @Column(name="ID") private String id; @Column(name="user_code") private String code; @Column(name="user_name") private String username; @Column(name="user_pwd") private String password; @Column(name="update_tmsp") private Date updateTime; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @Override public String toString() { return "User [id=" + id + ", code=" + code + ", username=" + username + ", password=" + password + ", updateTime=" + updateTime + "]"; } }
4. dao层创建UserDao.java继承JpaRepository
package com.king.dao; import org.springframework.data.jpa.repository.JpaRepository; import com.king.entity.User; public interface UserDao extends JpaRepository<User, String>{ }
5. service层创建查询方法,并装备dao中的方法
UserService接口
package com.king.service; import java.util.List; import com.king.entity.User; public interface UserService { public User findOne(String id); public List<User> findAll(); }
UserService实现类
package com.king.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.king.dao.UserDao; import com.king.entity.User; import com.king.service.UserService; @Service("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Override public User findOne(String id) { return userDao.findOne(id); } @Override public List<User> findAll() { return userDao.findAll(); } }
6. controller层查询
package com.king.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.king.entity.User; import com.king.service.UserService; @RestController public class UserController { @Autowired private UserService userService; @RequestMapping("/queryUser") public User getUser(String id){ return userService.findOne(id); } @RequestMapping("/queryAll") public List<User> getAll(){ return userService.findAll(); } }
7. App中配置扫描
package com.king.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @ComponentScan(basePackages={"com.king.controller","com.king.service"}) @EnableJpaRepositories(basePackages="com.king.dao") @EntityScan(basePackages="com.king.entity") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
到此,所有的配置完成。
测试1:根据id查询单个对象,返回json对象
测试2: 测试查询所有对象,返回json数组
以上是关于spingboot集成jpa的主要内容,如果未能解决你的问题,请参考以下文章
Java集成PageOffice在线打开编辑word文件 - Spring Boot