JPA BLOG 01
记录一次spring boot + spring data jap的的一次示例项目,数据库使用的是MySQL 8,spring boot的版本选择的是2.1.6
在IDEA上从initialzr创建spring boot项目。选择了以下依赖(web依赖没有用到)
pom.xml按照默认生成的没有作更改。
-
下面第一步:配置数据源,只有但数据源的配置比较简单,数据库连接池使用的也是默认的hikari,想用druid等其他数据库连接池网上一搜一大把,只是示例都按照默认来了。
删除application.properties,新建application.yml
spring: datasource: url: jdbc:mysql://[ip]:[port]/[database name] username: xxx password: xxx driver-class-name: com.mysql.cj.jdbc.Driver jpa: show-sql: true database-platform: org.hibernate.dialect.MySQL8Dialect hibernate: ddl-auto: update
在写yml配置文件时要注意缩进,冒号后面要有空格
-
创建实体类
性别使用枚举规定每个值代表的意义
package common; public enum Gender { /** * unknown */ UNKNOWN(0), /** * male */ MALE(1), /** * female */ FEMALE(2); private final int value; Gender(int value) { this.value = value; } public int getValue() { return value; } public static Gender genderOf(int genderValue) { for (Gender gender : Gender.values()) { if (gender.value == genderValue) { return gender; } } return Gender.UNKNOWN; } }
接下来是实体类
package com.example.jpademo.entity; import common.Gender; import lombok.Data; import javax.persistence.*; import javax.validation.constraints.NotNull; import java.io.Serializable; import java.util.Date; @Entity @Data public class Customer implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotNull private String name; private String address; @NotNull private Gender gender; @Column(name = "tel_num") private String telNum; @Temporal(TemporalType.DATE) @Column(name = "birth_date") private Date birthDate; }
其中使用了lombok的注解*@Data*,按照官网的解释为
@Data All together now: A shortcut for @ToString, @EqualsAndHashCode, @Getter on all fields, and @Setter on all non-final fields, and @RequiredArgsConstructor!
也就是上面出现的这些注解的集合体。再有就是jpa规范的一些注解,以后会出一篇有关jpa注解的文
-
创建dao层并测试
package com.example.jpademo.dao; import com.example.jpademo.entity.Customer; import org.springframework.data.repository.CrudRepository; public interface CustomerDao extends CrudRepository<Customer, Long> { }
进行单元测试
package com.example.jpademo.dao; import com.example.jpademo.entity.Customer; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class CustomerDaoTest { @Autowired private CustomerDao customerDao; /** * test if insert works */ @Test public void insertTest() { Customer customer = new Customer(); customer.setAddress("addr"); customer.setName("name lastname"); customer.setGender(Gender.MALE); customerDao.save(customer); Assert.assertNotNull(customer.getId()); } /** * test if findAll works */ @Test public void testFindAll() { customerDao.findAll().forEach(System.out::println); } /** * test find by id */ @Test public void testFindById() { System.out.println(customerDao.findById(1L)); } }
注意一定要加上@Test,IDEA的提示不加也可以运行,但是会报No tests found matching Method。
最后贴一下目录结构