Spring Boot 集成 JPA 的步骤
Spring Boot 集成 JPA 的步骤
配置依赖
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'
编写配置文件
1、JPA 的配置
@EnableJpaRepositories(basePackages = "com.liwei.dao")
@EntityScan(basePackages = "com.liwei.entity")
public class JpaConfig {
}
2、数据库连接的配置和其它配置
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 语句:
CREATE DATABASE `spring-boot-security` DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
编写 dao 层和 entity 的代码
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;
}
}
为了便于测试,我们还编写了服务层的方法:
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);
}
}