多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 一.引入依赖 ~~~ <!-- springBoot JPA的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ~~~ ### 二.数据库配置信息 ~~~ spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC username: root password: admin123 jpa: hibernate: ddl-auto: update show-sql: true ~~~ ***** ### 三.创建userBean ~~~ package com.youworker.domain; import lombok.Data; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * @author: youworker * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-05 20:04 */ @Entity @Data public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; private String realname; } ~~~ ***** ### 四.创建 repository ~~~ package com.youworker.repository; import com.youworker.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; /** * @author: youworker * @qq: 46914685 * @email: 46914685@qq.com * @date: 2020-01-05 20:18 */ public interface UserRepository extends JpaRepository<User, Long> { public List<User> findAll(); } ~~~ ***** ### 五.测试 ~~~ package com.youworker; import com.youworker.domain.User; import com.youworker.repository.UserRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class JpaTest { @Resource UserRepository userRepository; @Test public void index(){ List<User> all = userRepository.findAll(); System.out.println(all.size()); System.out.println(all); } } ~~~