文章目录
0. 简介
商品类目ProductCategory的Dao层的实现:实现查询商品类目,添加商品类目的功能
1. pom.xml中引入依赖
由于需要使用jpa,mysql,lombok等,因此引入相关依赖
<dependencies>
<!--SpringBoot整合jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--SpringBoot整合web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql连接数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--SpringBoot整合单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 配置文件application.properties
###########配置数据源###########
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.2.114/sell?characterEncoding=utf-8&useSSL=false
#运行时输出jpa执行的sql
spring.jpa.show-sql=true
#项目路径(2.0以上使用这个)
server.servlet.context-path=/sell
3. 与数据库表映射的实体类ProductCategory
此为买家类目实体类,使用几个重要的注解:
- @DynamicUpdate
jpa的注解,如果我们在更新表时,只想更新某个字段,就不要加 @DynamicUpdate。
如果我们更新某个字段时,更新所有的字段,就可以加上@DynamicUpdate. - @Proxy(lazy = false)
jpa的注解, 设置lazy=false,load的时候即会把所有普通属性全部读取进来。而且,返回的将是一个真正的该类型的对象(如Person),而不是代理类。 - @Entity
jpa的注解,表明该实体类(ProductCategory)是与数据库中的表映射的类,它默认对应数据库中的表名是product_category。 - @Data
lombok的注解,使用此注解可以不用再写get(),set(),toString()方法 - @NoArgsConstructor
lombok的注解,使用此注解可以不用再写无参构造方法 - @AllArgsConstructor
lombok的直接,使用此注解可以不用再写带参构造方法(所有参数) - @Id
jpa的注解,设置此属性为主键 - @ @GeneratedValue(strategy = GenerationType.IDENTITY)
jpa的注解,指明主键自增
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Proxy;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
//如果我们更新某个字段时,想更新所有的字段,就可以加上@DynamicUpdate.
@DynamicUpdate
//设置懒加载为false,否则会报错
@Proxy(lazy = false)
//表明该实体类是与数据库中的表映射的类
@Entity
//getter,setter,toString()方法
@Data
//无参构造函数
@NoArgsConstructor
public class ProductCategory {
//主键
@Id
//主键自增
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer categoryId;
//类目名字
private String categoryName;
//类目编号
private Integer categoryType;
public ProductCategory(String categoryName,Integer categoryType) {
this.categoryName = categoryName;
this.categoryType = categoryType;
}
}
3. 继承JPA接口的ProductCategoryRepository接口
import com.hh.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
//写一个接口继承接口JpaRepository<ProductCategory,Integer>,第一个参数为实体类,第二个参数为主键类型
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
//根据商品类目编号列表来查询商品类目
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}
4. 测试类ProductCategoryTest
注意,在测试查询方法时,需要向数据库中国添加一条数据:
import com.hh.dataobject.ProductCategory;
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;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryTest {
@Autowired
ProductCategoryRepository repository;
//查询方法
@Test
public void getOneTest(){
ProductCategory productCategory = repository.getOne(1);
System.out.println(productCategory.toString());
}
//添加方法
@Test
public void saveTest(){
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryName("女生最爱");
productCategory.setCategoryType(3);
repository.save(productCategory);
}
//更新方法,相对添加方法需要有主键
@Test
public void saveTest1(){
ProductCategory productCategory = new ProductCategory();
productCategory.setCategoryId(2);
productCategory.setCategoryName("男生最爱");
productCategory.setCategoryType(3);
repository.save(productCategory);
}
//使用构造函数测试添加方法
@Test
//这个注解表明测试的数据不会添加到数据库,不论成功还是失败都会回滚
@Transactional
public void saveTest2(){
//使用带参构造函数
ProductCategory productCategory =
new ProductCategory("女生最爱",3);
ProductCategory result = repository.save(productCategory);
Assert.assertNotNull(result);
}
//测试根据商品类目编号列表查询商品类目
@Test
public void findByCategoryTypeInTest(){
List<Integer> list = Arrays.asList(2,3,4,5);
List<ProductCategory> result = repository.findByCategoryTypeIn(list);
Assert.assertNotEquals(0,result.size());
}
}
补充:@Entity的用法
[@Entity]
必须与@Id注解 结合使用
否则 No identifier specified for entity:
name 属性
(可选)实体名称。 缺省为实体类的非限定名称。
该名称用于引用查询中的实体。
该名称不能是Java持久性查询语言中的保留字面值。
不与@Table结合的话 表名 默认为 SnakeCaseStrategy(命名策略 )为表名
若使用 name属性 且没有与@Table结合 则表名为 name值的SnakeCaseStrategy(命名策略 )
例如:
@Entity
public class UserEntity{...} 表名 user_entity
@Entity(name="UE")
public class UserEntity{...} 表名 ue
@Entity(name="UsEntity")
public class UserEntity{...} 表名 us_entity
例如: