<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.49version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
在你的数据库(本文数据库名称为test)下运行如下sql文件创建一个test_table表并插入数据
create table `test_table`(
`test_id` int not null auto_increment,
`test_name` varchar(32) not null,
`test_date` timestamp not null,
primary key(`test_id`)
);
insert into test_table values(null, "测试", now());
package com.wechat.sell.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TestsBean {
private Integer testId;
private String testName;
private Date testDate;
}
代码如下:
package com.wechat.sell.mapper;
import com.wechat.sell.bean.TestsBean;
public interface TestsMapper {
//根据id获取数据
public TestsBean getBean(int id);
}
package com.wechat.sell.service;
import com.wechat.sell.bean.TestsBean;
import com.wechat.sell.mapper.TestsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestsService {
@Autowired
TestsMapper testsMapper;
public TestsBean getBean(int id){
return testsMapper.getBean(id);
}
}
package com.wechat.sell.controller;
import com.wechat.sell.bean.TestsBean;
import com.wechat.sell.service.TestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestsController {
@Autowired
TestsService testsService;
@ResponseBody
@GetMapping("/test")
public TestsBean test(@RequestParam("id")Integer id){
return testsService.getBean(id);
}
}
<mapper namespace="com.wechat.sell.mapper.TestsMapper">
<select id="getBean" resultType="com.wechat.sell.bean.TestsBean">
select * from `test_table` where test_id = #{id}
select>
mapper>
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
configuration: # 指定mybatis全局配置文件中的相关配置项
map-underscore-to-camel-case: true
<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>ERRORlevel>
<onMatch>DENYonMatch>
<onMismatch>ACCEPTonMismatch>
filter>
<encoder>
<pattern>%msg%npattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:/study/offer/Java/log/info.%d.logFileNamePattern>
rollingPolicy>
appender>
<appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERRORlevel>
filter>
<encoder>
<pattern>%msg%npattern>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>D:/study/offer/Java/log/error.%d.logFileNamePattern>
rollingPolicy>
appender>
<root level="info">
<appender-ref ref="consoleLog"/>
<appender-ref ref="fileInfoLog"/>
<appender-ref ref="fileErrorLog"/>
root>
configuration>
package com.wechat.sell;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LogTest {
// private final Logger logger = LoggerFactory.getLogger(LogTest.class);
@Test
public void test() {
String name = "test";
String password = "123456";
log.debug("debug...");
log.info("name:{}, password:{}", name, password);
log.info("info...");
log.error("error...");
log.warn("warning...");
}
}