IDEA2018.2 MySQL5.6 JDK1.8
package net.zjitc.aspect;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/30 21:14
* @Version 1.0
**/
@Aspect // 使用@Aspect注解声明一个切面
@Component //配置bean
public class WebLogAspect {
private Logger logger = Logger.getLogger(getClass());
@Pointcut("execution(public * net.zjitc.web.controller..*.*(..))")
public void webLog() {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
logger.info("---------------request----------------");
logger.info("URL : " + request.getRequestURL().toString());
logger.info("HTTP_METHOD : " + request.getMethod());
logger.info("IP : " + request.getRemoteAddr());
Enumeration<String> enu = request.getParameterNames();
while (enu.hasMoreElements()) {
String name = (String) enu.nextElement();
logger.info("name:" + name + "value" + request.getParameter(name));
}
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
logger.info("---------------response----------------");
// 处理完请求,返回内容
logger.info("RESPONSE : " + ret);
}
}
package net.zjitc.dbconfig;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/31 10:43
* @Version 1.0
**/
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
public class DruidDBConfig {
private Logger logger = Logger.getLogger(DruidDBConfig.class);
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.initialSize}")
private int initialSize;
@Value("${spring.datasource.minIdle}")
private int minIdle;
@Value("${spring.datasource.maxActive}")
private int maxActive;
@Value("${spring.datasource.maxWait}")
private int maxWait;
@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;
@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;
@Value("${spring.datasource.validationQuery}")
private String validationQuery;
@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;
@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;
@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;
@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;
@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;
@Value("${spring.datasource.filters}")
private String filters;
@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;
@Bean //声明其为Bean实例
@Primary //在同样的DataSource中,首先使用被标注的DataSource
public DataSource dataSource(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(this.dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
//configuration
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
logger.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);
return datasource;
}
}
package net.zjitc.mapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/30 10:55
* @Version 1.0
**/
public interface UserMapper {
}
package net.zjitc.model;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/29 18:37
* @Version 1.0
**/
public class User {
}
package net.zjitc.service.impl;
import net.zjitc.mapper.UserMapper;
import net.zjitc.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/29 23:55
* @Version 1.0
**/
@Service //service组件
@Transactional
public class UserServiceImpl implements IUserService {
@Autowired // 自动注入
private UserMapper userMapper;
}
package net.zjitc.service;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/29 23:55
* @Version 1.0
**/
public interface IUserService {
}
package net.zjitc.web.controller;
import net.zjitc.service.IUserService;
import net.zjitc.service.impl.UserServiceImpl;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author ꧁ʚVVcatɞ꧂
* @Date 2019/8/29 18:40
* @Version 1.0
*
**/
@Controller//相当于声明Controller - 提共restful 风格 控制器
@RequestMapping("user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("login")
public String login(){
return "user/list";
}
}
package net.zjitc;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
/**
* Hello world!
*
*/
@EnableAutoConfiguration //自动配置,不需要写spring的配置文件
//@ComponentScan(basePackages = {"net.zjitc.datasource","net.zjitc.test1","net.zjitc.test2","net.zjitc.web"}) //还可以这样写
@ComponentScan(basePackages = "net.zjitc")
@MapperScan(basePackages = "net.zjitc.mapper")
public class App
{
public static void main( String[] args )
{
//启动springboot项目
SpringApplication.run(App.class, args);
}
// 启动访问网址:http://localhost:8080/test/user/login
}
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#连接池的配置信息
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,log4j
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#spring.freemarker.suffix=.ftl
#spring.freemarker.templateEncoding=UTF-8
#spring.freemarker.templateLoaderPath=classpath:/templates/
#spring.freemarker.content-type=text/html
spring.freemarker.request-context-attribute=request
log4j.appender.File.File = 自己设置缓存地址
info.log 为文件名和文件后缀格式
log4j.rootLogger=INFO,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n
log4j.appender.File = org.apache.log4j.RollingFileAppender
#自己设置缓存地址
log4j.appender.File.File = E:/JAVA/JAVAEE/info.log
#设置文件的缓存大小
log4j.appender.File.MaxFileSize = 10MB
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c - %L]%m%n
# 注意冒号后的只能用空格,不能用tab
server:
# 修改端口号
port: 8080
# 修改路径
context-path: /test
Hello World!!!
4.0.0
net.zjitc
Demo
1.0-SNAPSHOT
Demo
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
1.7
1.7
junit
junit
4.11
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
com.alibaba
druid
1.0.25
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-jta-atomikos
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
1.3.8.RELEASE
org.springframework.boot
spring-boot-starter-aop
org.aspectj
aspectjweaver
1.8.9
src/main/resources
**/*.*