Spring Boot 集成 JPA 的步骤
2017-05-02 00:19
585 查看
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); } }
相关文章推荐
- 从零开始学spring-boot(2)-集成spring-data-jpa
- Spring Boot 系列(九)数据层-集成Spring-data-jpa
- springboot集成mybatis的步骤---小白勿喷
- Spring Boot集成JPA的Column注解命名字段无效的问题
- 简单快速的用SpringBoot集成JPA
- springboot集成hibernate-jpa方式
- spring-boot集成JPA
- SpringBoot集成JPA的示例代码
- Spring boot 集成mybatis通用mapper配置步骤及注意事项
- SpringBoot-SpringData-JPA集成
- 基于spring boot 1.5.4 集成 jpa+hibernate+jdbcTemplate(详解)
- Springboot+Kotlin+Jpa集成项目
- Spring Boot集成JPA的Column注解命名字段无效的问题
- spring-boot集成spring-data-jpa
- spring-boot 集成jpa 持久层
- springboot集成hibernate-jpa方式
- Spring-Boot 集成Solr客户端的详细步骤
- springboot 集成 jpa 详解
- spring boot 添加扫包 集成 jpa
- Spring Boot(六):Spring Boot 集成 hibernate & JPA