springboot集成mybatis,日志处理logback
package com.xxh.springbootmybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
/**
* Hello world!
*
*/
@SpringBootApplication
@MapperScan("com.xxh.mapper")
@ComponentScan("com.xxh.controller,com.xxh.service")
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
package com.xxh.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xxh.service.HelloService;
@Controller
public class HelloController {
private static Logger logger=LoggerFactory.getLogger(HelloController.class);
@Autowired
private HelloService helloService;
@RequestMapping(value="/hello",method=RequestMethod.GET)
@ResponseBody
public String hello() {
logger.debug("come into controller");
return helloService.sayHello();
}
}
package com.xxh.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xxh.entity.HelloBean;
import com.xxh.mapper.HelloMapper;
@Service("helloService")
public class HelloService {
@Autowired
private HelloMapper helloMapper;
public String sayHello() {
HelloBean org=helloMapper.selectByPrimaryKey("26668");
return org.toString();
}
}
package com.xxh.mapper;
import com.xxh.entity.HelloBean;
public interface HelloMapper {
int deleteByPrimaryKey(String id);
int insertSelective(HelloBean record);
HelloBean selectByPrimaryKey(String id);
int updateByPrimaryKeySelective(HelloBean record);
int updateByPrimaryKey(HelloBean record);
}
package com.xxh.entity;
public class HelloBean {
private String id;
private String name;
private String code;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code == null ? null : code.trim();
}
@Override
public String toString() {
return "HelloBean [id=" + id + ", name=" + name + ", code=" + code
+ "]";
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.xxh.mapper.HelloMapper" >
<resultMap id="BaseResultMap" type="com.xxh.entity.HelloBean" >
<id column="ID" property="id" jdbcType="VARCHAR" />
<result column="CODE" property="code" jdbcType="VARCHAR" />
<result column="NAME" property="name" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
ID,CODE,NAME
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from hello_table
where ID = #{id,jdbcType=VARCHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from hello_table
where ID = #{id,jdbcType=VARCHAR}
</delete>
<insert id="insertSelective" parameterType="com.xxh.entity.HelloBean" >
insert into hello_table
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
ID,
</if>
<if test="name != null" >
NAME,
</if>
<if test="code != null" >
CODE,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=VARCHAR},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="code != null" >
#{code,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.xxh.entity.HelloBean" >
update hello_table
<set >
<if test="name != null" >
NAME = #{name,jdbcType=VARCHAR},
</if>
<if test="code != null" >
CODE = #{code,jdbcType=VARCHAR},
</if>
</set>
where ID = #{id,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.xxh.entity.HelloBean" >
update hello_table
set
NAME = #{name,jdbcType=VARCHAR},
CODE = #{code,jdbcType=VARCHAR}
where ID = #{id,jdbcType=VARCHAR}
</update>
</mapper>
server.port=14000
#公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources
mybatis.mapperLocations=classpath:mapper/*.xml
# 使用druid数据源
spring.datasource.type =com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = your_jdbc_url
spring.datasource.username = your_username
spring.datasource.password = your_password
spring.datasource.show_sql = true
<?xml version="1.0"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<property name="rootPath" value="${catalina.base}/logs" />
<!-- controller debug appender -->
<appender name="app_debugAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${rootPath}/app_debug.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
${rootPath}/app_debug-%d{yyyy-MM-dd}-%i.log.gz
</FileNamePattern>
<MaxHistory>31</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%t] [%-5p] %logger{36} | %msg%n
</pattern>
</layout>
</appender>
<!-- controller error appender -->
<appender name="app_errorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${rootPath}/app_error.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>
${rootPath}/app_error-%d{yyyy-MM-dd}-%i.log.gz
</FileNamePattern>
<MaxHistory>31</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<MaxFileSize>100MB</MaxFileSize>
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%t] [%-5p] %logger{36} | %msg %n
</pattern>
</layout>
</appender>
<!-- other appender -->
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%t] [%-5p] %logger{36} | %msg%n
</pattern>
</encoder>
</appender>
<!-- loggers -->
<logger name="com.xxh.controller" additivity="false">
<level value="ALL" />
<appender-ref ref="app_debugAppender"/>
<appender-ref ref="app_errorAppender"/>
<appender-ref ref="consoleAppender" />
</logger>
<!-- default Root logger -->
<root>
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
</root>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xxh</groupId>
<artifactId>springbootmybatis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootmybatis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>