SpringBoot学习使用小总结
代码块1
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: root
password: xxxxxx
url: jdbc:mysql:
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
代码块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);
}
代码块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());
}