mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 jpa 示例
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE `users` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
|
||||
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
|
||||
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `idx_username` (`username`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||||
|
||||
@@ -6,7 +6,8 @@ import java.util.Date;
|
||||
/**
|
||||
* 用户 DO
|
||||
*/
|
||||
@Entity(name = "users")
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserDO {
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package cn.iocoder.springboot.lab13.jpa.repository;
|
||||
|
||||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public interface UserRepository03 extends PagingAndSortingRepository<UserDO, Integer> {
|
||||
|
||||
UserDO findByUsername(String username);
|
||||
|
||||
Page<UserDO> findByCreateTimeAfter(Date createTime, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab13.jpa.repository;
|
||||
|
||||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
public interface UserRepository04 extends PagingAndSortingRepository<UserDO, Integer> {
|
||||
|
||||
@Query("SELECT u FROM UserDO u WHERE u.username = ?1")
|
||||
UserDO findByUsername01(String username);
|
||||
|
||||
@Query("SELECT u FROM UserDO u WHERE u.username = :username")
|
||||
UserDO findByUsername02(@Param("username") String username);
|
||||
|
||||
@Query(value = "SELECT * FROM users u WHERE u.username = :username", nativeQuery = true)
|
||||
UserDO findByUsername03(@Param("username") String username);
|
||||
|
||||
@Query("UPDATE UserDO u SET u.username = :username WHERE u.id = :id")
|
||||
@Modifying
|
||||
int updateUsernameById(Integer id, String username);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.iocoder.springboot.lab13.jpa.repository;
|
||||
|
||||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class UserRepository03Test {
|
||||
|
||||
@Autowired
|
||||
private UserRepository03 userRepository;
|
||||
|
||||
@Test
|
||||
public void testFindByUsername() {
|
||||
UserDO user = userRepository.findByUsername("yunai");
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByCreateTimeAfter() {
|
||||
// 创建分页条件
|
||||
Pageable pageable = PageRequest.of(1, 10);
|
||||
// 执行分页操作
|
||||
Date createTime = new Date(2018 - 1990, Calendar.FEBRUARY, 24); // 临时 Demo ,实际不建议这么写
|
||||
Page<UserDO> page = userRepository.findByCreateTimeAfter(createTime, pageable);
|
||||
// 打印
|
||||
System.out.println(page.getTotalElements());
|
||||
System.out.println(page.getTotalPages());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.iocoder.springboot.lab13.jpa.repository;
|
||||
|
||||
import cn.iocoder.springboot.lab13.jpa.dataobject.UserDO;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class UserRepository04Test {
|
||||
|
||||
@Autowired
|
||||
private UserRepository04 userRepository;
|
||||
|
||||
@Test
|
||||
public void testFindIdByUsername01() {
|
||||
UserDO user = userRepository.findByUsername01("yunai");
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindIdByUsername02() {
|
||||
UserDO user = userRepository.findByUsername02("yunai");
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindIdByUsername03() {
|
||||
UserDO user = userRepository.findByUsername03("yunai");
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
// 更新操作,需要在事务中。
|
||||
// 在单元测试中,事务默认回滚,所以胖友可能怎么测试,事务都不更新。
|
||||
@Transactional
|
||||
public void testUpdateUsernameById() {
|
||||
userRepository.updateUsernameById(5, "yudaoyuanma");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user