效果图:
代码区:
1 package com.wls.integrateplugs.jpa.primary.model; 2 3 /** 4 * Created by wls on 2017/8/24. 5 */ 6 import java.io.Serializable; 7 8 import javax.persistence.Column; 9 import javax.persistence.Entity; 10 import javax.persistence.GeneratedValue; 11 import javax.persistence.Id; 12 13 @Entity 14 public class User implements Serializable { 15 16 private static final long serialVersionUID = 1L; 17 @Id 18 @GeneratedValue 19 private Long id; 20 @Column(nullable = false, unique = true) 21 private String userName; 22 @Column(nullable = false) 23 private String passWord; 24 @Column(nullable = false, unique = true) 25 private String email; 26 @Column(nullable = true, unique = true) 27 private String nickName; 28 @Column(nullable = false) 29 private String regTime; 30 @Column(nullable = false) 31 private String name; 32 @Column(nullable = false) 33 private Integer age; 34 35 public User() { 36 super(); 37 } 38 39 40 public User(String name, Integer age) { 41 this.name = name; 42 this.age = age; 43 } 44 45 public User(String userName, String passWord, String email, String nickName, String regTime, String name, Integer age) { 46 this.userName = userName; 47 this.passWord = passWord; 48 this.email = email; 49 this.nickName = nickName; 50 this.regTime = regTime; 51 this.name = name; 52 this.age = age; 53 } 54 55 public Long getId() { 56 return id; 57 } 58 public void setId(Long id) { 59 this.id = id; 60 } 61 public String getUserName() { 62 return userName; 63 } 64 public void setUserName(String userName) { 65 this.userName = userName; 66 } 67 public String getPassWord() { 68 return passWord; 69 } 70 public void setPassWord(String passWord) { 71 this.passWord = passWord; 72 } 73 public String getEmail() { 74 return email; 75 } 76 public void setEmail(String email) { 77 this.email = email; 78 } 79 public String getNickName() { 80 return nickName; 81 } 82 public void setNickName(String nickName) { 83 this.nickName = nickName; 84 } 85 public String getRegTime() { 86 return regTime; 87 } 88 public void setRegTime(String regTime) { 89 this.regTime = regTime; 90 } 91 92 public String getName() { 93 return name; 94 } 95 96 public void setName(String name) { 97 this.name = name; 98 } 99 100 public Integer getAge() { 101 return age; 102 } 103 104 public void setAge(Integer age) { 105 this.age = age; 106 } 107 }
package com.wls.integrateplugs.jpa.primary.repository; import com.wls.integrateplugs.jpa.primary.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; /** * @author 程序猿DD * @version 1.0.0 * @date 16/3/23 下午2:34. * @blog http://blog.didispace.com */ public interface UserRepository extends JpaRepository<User, Long> { User findByName(String name); User findByNameAndAge(String name, Integer age); @Query("from User u where u.name=:name") User findUser(@Param("name") String name); }
package com.wls.test.integrateplugs.jpa; /** * Created by wls on 2017/8/24. */ import java.text.DateFormat; import java.util.Date; import com.wls.integrateplugs.jpa.primary.repository.IUserRepository; import com.wls.integrateplugs.jpa.primary.model.User; import com.wls.integrateplugs.jpa.primary.repository.UserRepository; import org.junit.Assert; 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.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class UserRepositoryTests { @Autowired private IUserRepository iUserRepository; @Autowired private UserRepository userRepository; @Test public void test() throws Exception { Date date = new Date(); DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG); String formattedDate = dateFormat.format(date); // iUserRepository.save(new User("aa","aa","aa","aa","aa","aa",12)); // iUserRepository.save(new User("bb","bb","bb","bb","bb","bb",13)); // iUserRepository.save(new User("cc","cc","cc","cc","cc","cc",14)); // Assert.assertEquals(3, iUserRepository.findAll().size()); Assert.assertEquals("aa", iUserRepository.findByUserNameOrEmail("aa", "aa").getNickName()); // iUserRepository.delete(iUserRepository.findByUserName("bb")); } @Test public void testUser() throws Exception { // 创建10条记录 userRepository.save(new User("AAA", 10)); userRepository.save(new User("BBB", 20)); userRepository.save(new User("CCC", 30)); userRepository.save(new User("DDD", 40)); userRepository.save(new User("EEE", 50)); userRepository.save(new User("FFF", 60)); userRepository.save(new User("GGG", 70)); userRepository.save(new User("HHH", 80)); userRepository.save(new User("III", 90)); userRepository.save(new User("JJJ", 100)); // 测试findAll, 查询所有记录 Assert.assertEquals(10, userRepository.findAll().size()); // 测试findByName, 查询姓名为FFF的User Assert.assertEquals(60, userRepository.findByName("FFF").getAge().longValue()); // 测试findUser, 查询姓名为FFF的User Assert.assertEquals(60, userRepository.findUser("FFF").getAge().longValue()); // 测试findByNameAndAge, 查询姓名为FFF并且年龄为60的User Assert.assertEquals("FFF", userRepository.findByNameAndAge("FFF", 60).getName()); // 测试删除姓名为AAA的User userRepository.delete(userRepository.findByName("AAA")); // 测试findAll, 查询所有记录, 验证上面的删除是否成功 Assert.assertEquals(9, userRepository.findAll().size()); } }
spring: datasource: primary: driver-class-name: com.mysql.jdbc.Driver # url: jdbc:mysql://192.168.159.128:3306/mydb url: jdbc:mysql://192.168.223.128:3306/db1 username: wls password: Wls141215! secondary: driver-class-name: com.mysql.jdbc.Driver # url: jdbc:mysql://192.168.159.128:3306/mydb url: jdbc:mysql://192.168.223.128:3306/db2 username: wls password: Wls141215! jpa: hibernate: ddl-auto: update show-sql: true
<!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>