1.为什么我们需要打印操作数据的sql语句?
答:不打印操作数据库sql语句的项目,简直就是维护人员的噩梦,调试难度及其困难,如果是二手代码,简直真的是祖传代码是屎山,我前几天维护别人的屎山代码,然后啥日志不打印,把我搞得心态爆炸。所以我决定把这个问题解决!
2.目录结构:
2.pom文件这样写
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
<!-- 请不要使用1.0.0版本,因为还不支持拦截器插件 -->
</dependency>
<!-- alibaba druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- MySql数据库驱动 -->
<dependency>
<groupId> mysql</groupId>
<artifactId> mysql-connector-java</artifactId>
<version> 5.1.46</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.application.yml配置文件这样写
spring:
# profiles:
# active: dev
datasource:
druid:
min-idle: 1
max-pool-prepared-statement-per-connection-size: 20
test-while-idle: true
time-between-eviction-runs-millis: 60000
max-active: 20
validation-query: SELECT 'x'
filters: stat
max-wait: 60000
url: jdbc:mysql://172.21.13.212:3306/zlb?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
test-on-borrow: false
password: coship
pool-prepared-statements: false
initial-size: 1
min-evictable-idle-time-millis: 300000
driver-class-name: com.mysql.jdbc.Driver
username: root
test-on-return: false
name: zlb
# type: com.alibaba.druid.pool.DruidDataSource
mybatis:
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: com.example.demo.model
#mybatis:
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#整合mybatis 显示打印sql以及参数(最重要)
logging:
level:
com.example.demo.dao : debug
5.java类这样写
(1)controller
package com.example.demo.controller;
import com.example.demo.model.User;
import com.example.demo.service.serviceImpl.UserServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("zlb")
public class UserController {
@Autowired
private UserServiceImpl userService;
private Logger logger = Logger.getLogger(UserController.class);
@GetMapping("/userInfo")
public User getUserInfo(){
return userService.getUserInfo();
}
}
(2)service
package com.example.demo.service;
import com.example.demo.model.User;
public interface UserService {
User getUserInfo();
}
package com.example.demo.service.serviceImpl;
import com.example.demo.dao.UserDao;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
private static final Logger logger = Logger.getLogger(UserServiceImpl.class);
@Autowired
private UserDao userDao;
@Override
public User getUserInfo() {
return userDao.getUserInfo();
}
}
(3)dao
package com.example.demo.dao;
import com.example.demo.model.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
//@Repository
@Mapper
public interface UserDao {
User getUserInfo();
}
(4)model
package com.example.demo.model;
import lombok.Data;
@Data
public class User {
private int id;
private String name;
private String address;
private String idcard;
}
(5)userMapper.xml
<?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.example.demo.dao.UserDao">
<select id="getUserInfo" resultType="com.example.demo.model.User">
select * from users ;
</select>
</mapper>
(6)建表语句这样写:
CREATE TABLE `users` (
`id` int(32) NOT NULL,
`name` varchar(64) NOT NULL,
`address` varchar(64) NOT NULL DEFAULT '' COMMENT '人员信息表',
`idcard` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
随便插入一条数据
然后运行
得到这样的结果:
以及:
{"id":1,"name":"sbsb","address":"www","idcard":"523049616169165xx"}