spring boot - 整合jpa
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency>
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName= com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true
@Entity @Table(name="t_user") public class JpaUser { @Column(name="name") private String name ; @Column(name="address") private String address ; @Column(name="id") @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id ; @Column(name="age1") private Integer age ; @Column(name="age11") private Integer age1 ; public JpaUser(String name, String address, Integer age, Integer age1,Integer id) { this.name = name; this.address = address; this.age = age; this.age1 = age1; this.id = id; } public JpaUser() { } }
public interface JpaUserRespotory extends JpaRepository<JpaUser,Integer> { }
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = springBoot.class) public class JpaTest { @Autowired JpaUserRespotory jpaUserRespotory ; @Test public void test1 () { JpaUser jpaUser = new JpaUser("名称","地址",10,20,40); jpaUserRespotory.save(jpaUser); } }