SpringBoot学习使用小总结1

SpringBoot学习使用小总结

  • 简洁的application.yml配置
  • SpringBoot自带的SLF4j+logback 日志框架
  • SpringBoot JPA 更多细节http://www.ityouknow.com/springboot/2016/08/20/spring-boo-jpa.html
  • pom.xml 导入lombok工具, 实体类加上@Data 注解。更多细节https://www.cnblogs.com/qnight/p/8997493.html
  • mysql 数据库timestamp的更新时间变化需要加上@DynamicUpdate注解。
  • @Test时,加@Transactional注解数据不入库。
  • Pageable 返回Page,PageRequest 继承 AbstractPageRequest 类 实现了 Pageable接口
  • 有参函数声明时也要带上无参构造函数

代码块1

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: xxxxxx
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/myproject?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  context-path: /mrz
logging:
  pattern:
    console: "%d - %msg%n"
  path: /var/log/tomcat/
  file: /var/log/tomcat/sell.log
  level:
    com.imooc.LoggerTest: debug
mybatis:
  mapper-locations: classpath:mapper/*.xml    

代码块2

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>
    <!-- 控制台日志输出 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>
    <!-- 输出日志 -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/var/log/tomcat/sell/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <!-- 错误日志 -->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 根据范围来过滤 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>  <!-- 只拦截ERROR -->
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/var/log/tomcat/sell/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />
    </root>

</configuration>

代码块3

public interface ProductInfoRepository extends JpaRepository<ProductInfo, String> {

    List<ProductInfo> findByProductStatus(Integer productStatus);
}

代码块4

@Entity
@DynamicUpdate
@Data
public class ProductCategory {

    /** 类目id. */
    @Id
    @GeneratedValue
    private Integer categoryId;

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

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

    private Date createTime;

    private Date updateTime;

    public ProductCategory() {
    }

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

代码块5

    @Test
    @Transactional
    public void saveTest() {
        ProductCategory productCategory = new ProductCategory("热销榜", 1);
        ProductCategory result = repository.save(productCategory);
        Assert.assertNotNull(result);
//        Assert.assertNotEquals(null, result);
    }

代码块6

   public void findAll() throws Exception {
        PageRequest request = new PageRequest(0, 2);
        Page<ProductInfo> productInfoPage = productService.findAll(request);
        Assert.assertNotEquals(0, productInfoPage.getTotalElements());
    }

代码块7

@Entity
@DynamicUpdate
@Data
public class ProductCategory {

    /** 类目id. */
    @Id
    @GeneratedValue
    private Integer categoryId;

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

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

    private Date createTime;

    private Date updateTime;

    public ProductCategory() {
    }

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


@Test
    public void findByCategoryTypeInTest() {
        List<Integer> list = Arrays.asList(2,3,4);

        List<ProductCategory> result = repository.findByCategoryTypeIn(list);
        Assert.assertNotEquals(0, result.size());
    }

猜你喜欢

转载自blog.csdn.net/qq_32583639/article/details/81435615