compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa', version: '1.5.2.RELEASE' compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.41'
一、JPA 的配置java
@EnableJpaRepositories(basePackages = "com.liwei.dao") @EntityScan(basePackages = "com.liwei.entity") public class JpaConfig { }
二、數據庫鏈接的配置和其它配置mysql
spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/spring-boot-security?useUnicode=true&characterEncoding=UTF-8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update
建立數據庫的 SQL 語句:spring
CREATE DATABASE `spring-boot-security` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
public interface UserRepository extends JpaRepository<User, Long> { User findByUserName(String userName); }
public interface UserRolesRepository extends JpaRepository<UserRole,Long> { }
@Table(name = "t_user") @Entity public class User { private Long userId; private String userName; private String password; private String email; private int enabled; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "user_id") public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } @Column(name = "user_name") 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 String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getEnabled() { return enabled; } public void setEnabled(int enabled) { this.enabled = enabled; } public User() { } public User(Long userId, String userName, String password, String email, int enabled) { this.userId = userId; this.userName = userName; this.password = password; this.email = email; this.enabled = enabled; } @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", enabled=" + enabled + '}'; } }
@Table(name = "t_user_roles") @Entity public class UserRole { private Long userRoleId; private Long userId; private String role; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "user_role_id") public Long getUserRoleId() { return userRoleId; } public void setUserRoleId(Long userRoleId) { this.userRoleId = userRoleId; } @Column(name = "user_id") public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } }
爲了便於測試,咱們還編寫了服務層的方法:sql
public interface IUserService { User findByUserName(String userName); }
@Service public class UserServiceImpl implements IUserService { @Autowired private UserRepository userRepository; @Override public User findByUserName(String userName) { return userRepository.findByUserName(userName); } }
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.2.RELEASE'
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringBootSecurityApplication.class) public class IUserServiceTest { @Autowired private IUserService userService; @Autowired private UserRepository userRepository; @Test public void initData() throws Exception { User user = new User(); user.setUserName("liwei"); user.setPassword("123456"); user.setEmail("121088825"); user.setEnabled(1); User user1 = userRepository.save(user); System.out.println(user1); } @Test public void findByUserName() throws Exception { User user = userService.findByUserName("liwei"); System.out.println(user); } }