-
添加依赖 pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.28</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency>
-
修改application.properties文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3307/jpa?useSSL=true&serverTimezone=UTC&characterEncoding=UTF-8 #jpa connect mysql spring.jpa.database=mysql #jpa show sql spring.jpa.show-sql=true #jpa format sql spring.jpa.properties.hibernate.format_sql=true #create-drop create update validate spring.jpa.hibernate.ddl-auto=update
-
IUserDao.java
public interface IUserDao extends JpaRepository<User, Integer> { List<User> findByPassword(String password); List<User> findByUsername(String username); }
-
IUserService.java
public interface IUserService { List<User> getAllUsers(); void saveUsers(User u); List<User> getUsersByPassword(String password); List<User> getUsersByUsername(String username); }
-
UserServiceImpl.java
@Service public class UserServiceImpl implements IUserService { @Resource private IUserDao userDao; @Override public List<User> getAllUsers() { return userDao.findAll(); } @Override public void saveUsers(User u) { userDao.saveAndFlush(u); } @Override public List<User> getUsersByPassword(String password) { return userDao.findByPassword(password); } @Override public List<User> getUsersByUsername(String username) { return userDao.findByUsername(username); } }
-
TestUserService.java
//@RunWith(SpringRunner.class) @SpringBootTest public class TestUserService { @Resource private IUserService userService; @Test public void testSaveUser(){ User u = new User(0, "wangwu", "999999", "beijing"); userService.saveUsers(u); } @Test public void testGetUsersByUsername(){ for (User user : userService.getUsersByUsername("zhangsan")) { System.out.println(user); } } @Test public void testGetUsersByPassword(){ for (User user : userService.getUsersByPassword("999999")) { System.out.println(user); } } }
-
运行结果:
Hibernate: select user0_.col_uid as col_uid1_0_, user0_.addr as addr2_0_, user0_.col_password as col_pass3_0_, user0_.col_username as col_user4_0_ from tbl_user user0_ where user0_.col_username=? User(uid=1, username=zhangsan, password=999999, addr=beijing)
-
至此Springboot完成了数据库测试,继续写前端控制器,UserController.java
@RestController public class UserController { @Resource private IUserService userService; @GetMapping("/Users") public List<User> getAllUsers(){ return userService.getAllUsers(); } ```
-
访问http://localhost:8080/Users,得到如下的json字符串:
[{"uid":1,"username":"zhangsan","password":"999999","addr":"beijing"}, {"uid":2,"username":"lisi","password":"999999","addr":"beijing"}, {"uid":3,"username":"wangwu","password":"999999","addr":"beijing"}]
- 至此SpringBoot整合JPA完成
网友评论