SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象

 

 

5.1添加依赖

添加mysql和JPA的依赖jar包


    org.springframework.boot
    spring-boot-starter-data-jpa



    mysql
    mysql-connector-java

5.2配置数据库信息

将application.properties改为application.yml,使用yml格式配置将变得更简单

SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第1张图片

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://192.168.204.128/WeiXinSell?characterEncoding=utf-8&useSSl=false
  jpa:
    show-sql: true

5.3建立数据表映射过来的对象

5.3.1将数据库映射成对象须加入注解@Entity,将id设为主键需要加入注解@Id和@GeneratedValue

添加getter,setter和tostring方法

 SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第2张图片

 

5.3.2Dao层

创建接口ProductCategoryRepository,需要继承JpaRepository<继承的类,主键类型>

SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第3张图片

 

 
   

5.3.3编写单元测试

右键接口ProductCategoryRepositorygo on →test

 

编写查询一条记录测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {

    @Autowired
    private ProductCategoryRepository repository;

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

 

报错:could not initialize proxy [com.imooc.sell.dataobject.ProductCategory#1] - no Session

解决办法:https://www.cnblogs.com/ZHONGZHENHUA/p/9065152.html

 

查询结果

SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第4张图片

 插入记录

@Test
    public void saveTest(){
        ProductCategory productCategory = new ProductCategory();
        productCategory.setCategoryId(2);
        //productCategory.setCategoryName("女生最爱");
        //productCategory.setCategoryType(3);
        //productCategory.setCategoryName("男生最爱");
        //productCategory.setCategoryType(4);
        productCategory.setCategoryName("老少咸宜");
        productCategory.setCategoryType(5);
        repository.save(productCategory);
    }

更新记录,更新createtime和updatetime需要在实体类上添加注解@DynamicUpdate

@Test
public void updateTest(){
    ProductCategory productCategory=repository.getOne(2);
    productCategory.setCategoryType(3);
    repository.save(productCategory);
}

 SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第5张图片

老司机带来干货了!!!!!

实体类对象不用再写getter和setter方法了。在pom.xml依赖里面引进lombok工具。


    org.projectlombok
    lombok

 添加完之后你在命令行里面进行maven打包它已经能够起作用了。但是你希望在我们那个编译器里面能够跑起来的话还是需要装一个插件才行。

 

 

 SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第6张图片

 Lombok Plugin是一个完全免费的一个插件。

 SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第7张图片

 SpringBoot微信点餐系统—5、买家端的开发-买家类目(上)—添加依赖、配置数据库信息、建立数据表映射过来的对象_第8张图片在代码里面如何使用呢?

package com.imooc.sell.dataobject;
//import javax.persistence.Table;

import lombok.Data;import lombok.Getter;import lombok.Setter;import lombok.ToString;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;import java.util.Date;
/**
 * 类目
 * Created by zhongzh
 * 2018-05-20 9:31
 * s_product_category
 *///@Table(name = "s_product_category")
@Entity//把数据库映射成对象
@Proxy(lazy = false)
@DynamicUpdate //@Data//@Data包含生成getter、setter和toString()方法//@Getter//如果只是需要Getter那就引入Getter//@Setter//如果只是需要Setter那就引入Setter//@ToString//如果只是需要ToString那就引入ToStringpublic class ProductCategory{
     /** 类目id. */
     @Id//Id是主键,自增类型的。
     //@GeneratedValue//相当于调用了native策略
     //@GeneratedValue(strategy = GenerationType.AUTO)
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Integer categoryId;//字段名的命名方式也是一样的,把下划线改成为空。
     /** 类目名字. */
     private String categoryName;

     /** 类目编号. */
     private Integer categoryType;
     /** 创建时间. */
     private Date createTime;
     /** 修改时间. */
     private Date updateTime;
    //不要忘了Getter和Setter方法
    /*
    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;
    }

    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;
    }

    @Override
    public String toString() {
        return "ProductCategory{" +
                "categoryId=" + categoryId +
                ", categoryName='" + categoryName + '\'' +
                ", categoryType=" + categoryType +
                '}';
    }
    */
}

 

欢迎进群交流258897306或关注公众号“IT群英汇

你可能感兴趣的:(Java)