11
package com.imooc.dataobject;
//import javax.persistence.Table;
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* Created by Administrator on 2019/3/5 0005.
* 现在表名称是 product_category 刚好默认对应
* 如果是 db_product_category
*/
//@Table(name = "db_product_category") 这就要写
@Table(name = "product_category") // 表名一致 这行是可以不写的 下划线直接转成驼峰
@Entity // 数据库映射为对象 必须加这个注解
@DynamicUpdate //update_time字段 自动更新
@Data //不用写get set toString 都有了 了
public class ProductCategory {
// 数据库的下划线修改为驼峰 类目id
@Id//主键
@GeneratedValue//自增类型
private Integer categoryId;//变量和表名一样 下划线修改驼峰
/* 类目名字 */
private String categoryName;
/* 类目编号 */
private Integer categoryType;
// 不知道为什么增加这2个属性 就无法正常更新时间了
private Date createTime; //先不增加 用得到了在增加
private Date updateTime;
//pom文件中做一下修改就不用每次生成get set toString了
//1.pom文件中增加<groupId>org.projectlombok</groupId>
//2.安装插件 setting-》plugin lombok plugin
//3.实体类开头增加注释@Data 爆红色 就需要刷新pom文件
// public Date getCreateTime() {
// return createTime;
// }
//
// public void setCreateTime(Date createTime) {
// this.createTime = createTime;
// }
//
// public Date getUpdateTime() {
// return updateTime;
// }
//
// public void setUpdateTime(Date updateTime) {
// this.updateTime = updateTime;
// }
//
//
// // get set
//
//
// public Integer getCategoryId() {
// return categoryId;
// }
//
// public void setCategoryId(Integer categoryId) {
// this.categoryId = categoryId;
// }
//
// public String getCategoryName() {
// return categoryName;
// }
//
// public void setCategoryName(String categoryName) {
// this.categoryName = categoryName;
// }
//
// public Integer getCategoryType() {
// return categoryType;
// }
//
// public void setCategoryType(Integer categoryType) {
// this.categoryType = categoryType;
// }
// @Override
// public String toString() {
// return "ProductCategory{" +
// "categoryId=" + categoryId +
// ", categoryName='" + categoryName + '\'' +
// ", categoryType=" + categoryType +
//
// '}';
// }
}
repository 就是dao
package com.imooc.repository;
import com.imooc.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* Created by Administrator on 2019/3/5 0005.
* 类名有点长别写错了 不知道为什么老是强调这个 ....
*/
// 这里是interface 不是class spring的Jpa我以前有讲过 springJpa有一个在慕课网上有自己看吧
public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer> {
//选中接口->GoTo->Test->选中ok 这样就可以进行单元测试了
//默认存在的方法是无法满足需求的 需要自定义一些方法 //这个是categoryType 不是categoryId!!!!!!
List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);//根据传入的categroyTypeList查询出类目list
//findBy 有提示的 CategoryTypeIn 有提示的 拆成多个就有提示了
//List<ProductCategory> findByCategoryTypeInAndCategoryName();
}
findOne
save 保存和更新都是这个方法