IDEA写maven微信购物端项目的过程和注意重点(一)

2.

4. 买家类目(ProductCategory)

4.1、 yml文件中标红

因为pom文件中引入的插件木有刷新,右键pom→maven→reimport

4.2、 如何解决idea的Could not autowire. No beans of ‘xxxx’ type found

原因:编译的过程中却没有报错,这个就判断应该是是个警告类似的级别错误,不是很严重。所以我需要降低这个编辑的检查级别。
方法:降低Autowired检测的级别,将Severity的级别由之前的error改成warning或其它可以忽略的级别。
这里写图片描述

4.3、 写买家类目的过程

4.3.1、pom引入插件,右键更新

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
</dependency>

4.3.2、写application.yml配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  context-path: /sell

4.3.3、 写数据库实体类ProductCategory

重点标签:
@Entity //将数据库映射成对象的标签【重要】
@DynamicUpdate //动态更新标签【重要】
@Id //主键标签
@GeneratedValue //自增标签
@Data //引入lombok插件中的标签 【重要】用了@Data之后不用写具体setget方法了
@Getter
@Setter
理解:
1。实体类中只存放查询需要的数据库信息,比如如果不查更新时间,就不用写updateTime了
2。写构造方法,简化其它需要new该实体类时的赋值操作

/**
 * 类目实体类
 * 如果需要查询和修改时间的功能,需要下面生成private Date updateTime;
 * 但这样的话,更新操作,因为没有对updateTime单独修改,修改数据后,updateTime就不能自动变了
 * 所以加上@DynamicUpdate 动态更新标签,
 */
 
import ...;

@Entity  //将数据库映射成对象的标签
@DynamicUpdate      //动态更新标签
@Data//自动get set标签!
public class ProductCategory {
    /** 类目id*/
    @Id     //主键标签
    @GeneratedValue     //自增标签
    private Integer categoryId;
    /** 类目名字*/
    private String categoryName;
    /** 类目编号*/
    private Integer categoryType;
    // ProductCategoryRepository接口中写其它方法时需要无参的构造函数!
    public ProductCategory() {
    }
	// 简化new实体类的赋值操作
    public ProductCategory(String categoryName, Integer categoryType) {
        this.categoryName = categoryName;
        this.categoryType = categoryType;
    }
    /** 时间,项目中用不到*/
    //private Date createTime;
    //private Date updateTime;
	
	//set、get方法...用了lombok后就不用写了。 强无敌
}

4.3.4、写一个ProductCategoryRepository接口(相当于dao)

接口继承JpaRepository < ProductCategory,Integer> 插入productcategory实体类类,主键类型Integer
repository:仓库
需要其它的查询方法,可以自己写,但是findByCategoryTypeIn的命名方法是规定好的,不能改;
test中的方法名xxxxTest

public interface ProductCategoryRepository extends JpaRepository<ProductCategory,Integer>{
	// 通过类目编号查询
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
}

JpaRepository支持接口规范方法名查询。意思是如果在接口中定义的查询方法符合它的命名规则,就可以不用写实现,目前支持的关键字如下。
之后就使用这个接口,不用写具体一句sql语句,就可以实现数据库操作了!

4.3.5、写一个ProductCategoryRepository接口的测试类,测试功能

右键ProductCategoryRepository,选择转去→测试,自动在test中生成测试类,
重点标签:
@RunWith(SpringRunner.class)//测试类固定写法
@SpringBootTest //测试类固定写法
@Autowired //注入标签
@Test //测试标签
@Transactional //回滚标签,测试完成后数据删除,

理解:

  • @Autowired 标签对成员变量、方法和构造函数进行标注,来完成自动装配的工作,即可使用jpa中数据库方法
  • 测试类中生成ProductCategoryRepository对象repository,
    ProductCategoryRepository继承了JPA,又用@Autowired标签自动注入装配,所以repository中有很多数据库的方法了。
    比如repository.findOne(1);//查找
    repository.save(productCategory);//执行保存、更新操作
  • 保存操作中,新建ProductCategory productCategory = new ProductCategory();对象,因为新建一个对象,才能通过repository操作数据库保存进去。

注意报错内容:
org.springframework.orm.jpa.JpaSystemException: No default constructor for entity: :
需要在ProductCategoryRepository 设置一个无参构造函数!!!

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
    @Autowired  //相当于引入dao?
    private ProductCategoryRepository repository;       //ProductCategoryRepository继承了JPA,所以repository中有很多数据库的方法了。

    @Test
    public void findOneTest() {
        ProductCategory productCategory = repository.findOne(1);
        System.out.println(productCategory.toString());
    }

    @Test
    @Transactional  //回滚,测试完成后数据删除,
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryName("女生最爱");
        productCategory.setCategoryType(3);
        repository.save(productCategory);
    }
    @Test   //通过类目编号查询
    public void findByCategoryTypeIn() {
        List<Integer> list = Arrays.asList(2,3,4);
        List<ProductCategory> result = repository.findByCategoryTypeIn(list);

        Assert.assertNotEquals(0, result.size());
    }

4.3.6、 写CategoryService和CategoryService的impl实现类

service代码,接口中定义好方法,impl中继承接口,写具体实现方法

重点标签:
@Service //service端的注解标签,impl实现类中需要引入

public interface CategoryService {
    // 查询一条记录
    ProductCategory findOne(Integer categoryId);
    // 查询所有
    List<ProductCategory> findAll();
    // 通过类目编号查询
    List<ProductCategory> findByCategoryTypeIn(List<Integer> categoryTypeList);
    // 保存
    ProductCategory save(ProductCategory productCategory);
}
@Service    //service端的注解
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private ProductCategoryRepository repository;

    @Override
    public ProductCategory findOne(Integer categoryId) {
        return repository.findOne(categoryId);
    }
    ....
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值