SpringBoot学习随笔记录3(mysql配置、MVC-DAO、lombok)

mysql配置
1.项目添加mysql依赖,配置操作数据库包


WechatIMG1.jpeg
   
        
            mysql
            mysql-connector-java
        

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

2.把xx/src/main/resources/下的 application.properties改名为application.yml,因为yml使代码更加简洁。
application.yml中配置数据库内容

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver   #设置数据库驱动
    username: root                             #用户名
    password: 123456                           #密码
    url: jdbc:mysql://192.168.1.137/sell?characterEncoding=utf-8&useSSL=false  #数据库地址,characterEncoding防止中文乱码,useSSL忽略非SSL安全协议警告
  jpa:
    show-sql: true            #因为是开发环境所以打印sql语句

MVC架构:DAO-service-Controller
MVC - DAO要点:
a.数据库表名和项目中类名关系


屏幕快照 2019-04-04 下午5.05.23.png

虽然类名驼峰写,表名是下划线写法,但是spring-boot-starter-data-jpa会自动识别将两者关联
如果想类名与表名不一致可以这样写:


WechatIMG2.jpeg
  b.数据库映射成对象需要在类中加上@Entity注解
  c.@Id   //主键.    @GeneratedValue   //自增,   command+n设置get/set/toString方法

单元测试

  1. 选中接口右键->Go to->Test, create new test
    2.Springboot2.0后findOne(id)方法被废除,使用findById(id).get()代替, findOne(S)用来查找对象S
    3.repository.save(productCategory);增加数据时,如果不设置id,使用自增id,直接保存会报错。需要在自增id注解中加 strategy = GenerationType.IDENTITY,如:@GeneratedValue(strategy = GenerationType.IDENTITY) //自增
    4.自动更新时间需要在表类中添加@DynamicUpdate注解(否则表类中有createTime、updateTime属性时,更新方法不会自动更新时间)
    5.添加构造方法,command+n -> Constructor
    6.@Transactional注解可以是测试内容不插入表中
    @Test
    @Transactional //设置测试内容不插入表中
    public void testTest(){
        //构造方法添加数据
        ProductCategory productCategory = new ProductCategory("老人最爱",4);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);//不为空表示成功
    }

7.list条件查询,ProductCategoryRepository接口添加

public interface ProductCategoryRepository extends JpaRepository {

    //list查询,一次查多个数据,查的结果为list,通过CategoryType查,In代表在其范围内的,查询条件为categoryTypeList。即:查询CategoryType在categoryTypeList范围内的数据
    List findByCategoryTypeIn(List categoryTypeList);
}

注意:使用list条件查询时需要有一个无参的构造方法

lombok插件使用
lombok以简单的注解形式来简化java代码,提高开发人员的开发效率,如生成构造器、getter/setter、equals、hashcode、toString等等
使用方法:pom.xml添加依赖

        
        
            org.projectlombok
            lombok
        

( IDEA需要添加下载插件:
preferences->搜索Plugins->搜索lombok->安装)
*使用时需要在表类中加注解@Data ,@Data包含了getter/setter、equals、hashcode、toString等等方法。

表类:

package com.gang.sell.dataobject;

import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.util.Date;

/*
*类目
*
**/
@Entity
@DynamicUpdate
@Data
public class ProductCategory {

    /* 类目id */
    @Id   //主键
    @GeneratedValue(strategy = GenerationType.IDENTITY)   //自增
    private  Integer categoryId;

    /* 类目名字 */
    private  String categoryName;

    /* 类目编号 */
    private  Integer categoryType;

//    private Date createTime;
//
//    private Date updateTime;


    //    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 +
//                '}';
//    }

    //构造方法

    //无参构造方法
    public ProductCategory() {
    }

    public ProductCategory(String categoryName, Integer categoryType) {
        this.categoryName = categoryName;
        this.categoryType = categoryType;
    }
}

接口类:

package com.gang.sell.repository;

import com.gang.sell.dataobject.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProductCategoryRepository extends JpaRepository {

    //list查询,一次查多个数据,查的结果为list,通过CategoryType查,In代表在其范围内的,查询条件为categoryTypeList。即:查询CategoryType在categoryTypeList范围内的数据
    List findByCategoryTypeIn(List categoryTypeList);
}

测试类:

package com.gang.sell.repository;

import com.gang.sell.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 javax.transaction.Transactional;
import java.sql.Array;
import java.util.Arrays;
import java.util.List;


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

    @Autowired
    private  ProductCategoryRepository repository;

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

    @Test
    public void saveTest() {//新增数据
        //1
//        //增加数据时,如果不设置id,使用自增id,直接保存会报错。需要在自增id注解中加    strategy = GenerationType.IDENTITY
//        ProductCategory productCategory = new ProductCategory();
//        productCategory.setCategoryName("女生最爱");
//        productCategory.setCategoryType(3);
//        repository.save(productCategory);


        //2
        //构造方法添加数据
        ProductCategory productCategory = new ProductCategory("女生最爱",3);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);//不为空表示成功
//      等价于  Assert.assertNotEquals(null,result);
    }

    @Test
    public void updateTest(){//更新数据
        //1
//        ProductCategory productCategory = new ProductCategory();
//        productCategory.setCategoryId(2); //更新数据需要设置id
//        productCategory.setCategoryName("男生最爱");
//        productCategory.setCategoryType(3);
//        repository.save(productCategory);
        //2
        //查数据
        ProductCategory productCategory = repository.findById(2).get();
        //改数据
        productCategory.setCategoryType(9);
        //保存
        repository.save(productCategory);

    }

    @Test
    @Transactional //设置测试内容不插入表中
    public void testTest(){
        //构造方法添加数据
        ProductCategory productCategory = new ProductCategory("老人最爱",4);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);//不为空表示成功
    }


    @Test
    //条件查询
    public void findByCategoryTypeInTest(){
        List list = Arrays.asList(2,3,4);//CategoryType为2或3或4的数据
        List result = repository.findByCategoryTypeIn(list);
        Assert.assertNotEquals(0,result.size()); //查出结果大于0

    }
}

你可能感兴趣的:(SpringBoot学习随笔记录3(mysql配置、MVC-DAO、lombok))