master
luffy9412 7 years ago
commit c26418aaf8
  1. 120
      pom.xml
  2. 17
      src/main/java/org/pqh/aop/InsertAdvice.java
  3. 17
      src/main/java/org/pqh/aop/InsertAdvisor.java
  4. 22
      src/main/java/org/pqh/aop/LogAspect.java
  5. 11
      src/main/java/org/pqh/aop/MatcherRule.java
  6. 60
      src/main/java/org/pqh/config/HibernateConfig.java
  7. 27
      src/main/java/org/pqh/config/SpringConfig.java
  8. 51
      src/main/java/org/pqh/config/WebConfig.java
  9. 49
      src/main/java/org/pqh/controller/AbstractController.java
  10. 22
      src/main/java/org/pqh/controller/ParamController.java
  11. 35
      src/main/java/org/pqh/dao/BaseDao.java
  12. 11
      src/main/java/org/pqh/model/AbstractModel.java
  13. 47
      src/main/java/org/pqh/model/Config.java
  14. 83
      src/main/java/org/pqh/model/Param.java
  15. 11
      src/main/java/org/pqh/util/DBAction.java
  16. 26
      src/main/resources/log4j2.xml
  17. 10
      src/main/webapp/WEB-INF/jsp/index.jsp
  18. 41
      src/test/java/JunitTest.java

@ -0,0 +1,120 @@
<?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>
<groupId>org.pqh</groupId>
<artifactId>webCrawler</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<description>简单的网络爬虫客户端</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mysql.version>6.0.6</mysql.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>LATEST</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-test</artifactId>-->
<!--<version>LATEST</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>junit</groupId>-->
<!--<artifactId>junit</artifactId>-->
<!--<version>LATEST</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,17 @@
package org.pqh.aop;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* Created by reborn on 2017/8/3.
*/
public class InsertAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("类:"+o.getClass()+",方法:"+method.getName()+"被调用,传入参数:"+ Arrays.asList(objects));
}
}

@ -0,0 +1,17 @@
package org.pqh.aop;
import org.pqh.dao.BaseDao;
import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
import java.lang.reflect.Method;
/**
* Created by reborn on 2017/8/3.
*/
public class InsertAdvisor extends StaticMethodMatcherPointcutAdvisor {
@Override
public boolean matches(Method method, Class<?> aClass) {
return method.getName().equals("get")&& BaseDao.class.isAssignableFrom(aClass);
}
}

@ -0,0 +1,22 @@
package org.pqh.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
/**
* Created by reborn on 2017/8/3.
*/
@Aspect
@Component
public class LogAspect {
@Before(value = "org.pqh.aop.MatcherRule.biliDaoRule()")
public void curdCallAction(JoinPoint joinPoint){
for(Object arg:joinPoint.getArgs()){
System.out.println("参数"+arg);
}
}
}

@ -0,0 +1,11 @@
package org.pqh.aop;
import org.aspectj.lang.annotation.Pointcut;
/**
* Created by reborn on 2017/8/3.
*/
public class MatcherRule {
@Pointcut("execution(* org.pqh.dao.BaseDao.*(..))")
public void biliDaoRule(){}
}

@ -0,0 +1,60 @@
package org.pqh.config;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.io.IOException;
import java.util.Properties;
/**
* Created by reborn on 2017/7/28.
*/
@Configuration
@EnableTransactionManagement
public class HibernateConfig {
private LocalSessionFactoryBean sessionFactoryBean;
private BasicDataSource dataSource;
public HibernateConfig() {
dataSource=new BasicDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUsername("root");
dataSource.setPassword("123456");
sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
Properties properties=new Properties();
properties.setProperty("hibernate.connection.pool_size","5");
properties.setProperty("hibernate.jdbc.fetch_size","50");
properties.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL57Dialect");
sessionFactoryBean.setHibernateProperties(properties);
sessionFactoryBean.setPackagesToScan("org.pqh.model");
try {
sessionFactoryBean.afterPropertiesSet();
} catch (IOException e) {
e.printStackTrace();
}
}
@Bean("transactionManager")
public HibernateTransactionManager hibernateTransactionManager(){
HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(this.sessionFactoryBean.getObject());
return hibernateTransactionManager;
}
@Bean(name="hibernateTemplate")
public HibernateTemplate hibernateTemplate(){
HibernateTemplate hibernateTemplate=new HibernateTemplate();
hibernateTemplate.setSessionFactory(sessionFactoryBean.getObject());
return hibernateTemplate;
}
}

@ -0,0 +1,27 @@
package org.pqh.config;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* Created by reborn on 2017/7/28.
*/
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.pqh")
@EnableAspectJAutoProxy
@Import(HibernateConfig.class)
public class SpringConfig{
@Bean("viewReslover")
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver internalResourceViewResolver=new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}

@ -0,0 +1,51 @@
package org.pqh.config;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.*;
import java.util.EnumSet;
/**
* Created by reborn on 2017/7/28.
*/
public class WebConfig implements WebApplicationInitializer {
private String suffix="*.miku";
private String fileEncoding="UTF-8";
public void onStartup(ServletContext servletContext) throws ServletException {
//基于注解配置的上下文
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
//注册Spring容器配置类
context.register(SpringConfig.class);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
//配置映射路径
servletContext.log("DispatcherServlet配置映射路径匹配规则:"+suffix);
servlet.addMapping("/",suffix);
//启动顺序
servlet.setLoadOnStartup(1);
servletContext.addListener(new ContextLoaderListener(context));
FilterRegistration.Dynamic characterEncodingFilter=servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class);
servletContext.log("CharacterEncodingFilter设置编码:"+fileEncoding);
characterEncodingFilter.setInitParameter("encoding",fileEncoding);
characterEncodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");
// FilterRegistration.Dynamic openSessionInViewFilter=servletContext.addFilter("OpenSessionInViewFilter", OpenSessionInViewFilter.class);
// openSessionInViewFilter.setInitParameter("flushMode","AUTO");
// openSessionInViewFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");
}
}

@ -0,0 +1,49 @@
package org.pqh.controller;
import org.pqh.dao.BaseDao;
import org.pqh.model.AbstractModel;
import org.pqh.util.DBAction;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* Created by reborn on 2017/7/28.
*/
@Controller
public abstract class AbstractController<T extends AbstractModel> {
@Resource
private BaseDao baseDao;
@RequestMapping("/")
public String index(){
return "index";
}
public String curd(DBAction action,T formModel){
Class<T> tClass= (Class<T>) formModel.getClass();
try {
switch (action) {
case Insert:
case Update:
baseDao.saveOrUpdate(formModel);
break;
case Select:
T model = baseDao.get(tClass, formModel.primaryKey());
return model == null ? "找不到主键=" + formModel.primaryKey() + "" : model.toString();
case Delete:
baseDao.delete(tClass, formModel.primaryKey());
break;
}
return action+" success";
}catch (DataAccessException e){
}
return action+" fail";
}
}

@ -0,0 +1,22 @@
package org.pqh.controller;
import org.pqh.model.Param;
import org.pqh.util.DBAction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by reborn on 2017/8/3.
*/
@Controller
public class ParamController extends AbstractController<Param> {
@Override
@ResponseBody
@RequestMapping(value = "/param/{action}",produces = "text/html;charset=UTF-8")
public String curd(@PathVariable DBAction action,@ModelAttribute Param formModel) {
return super.curd(action, formModel);
}
}

@ -0,0 +1,35 @@
package org.pqh.dao;
import org.pqh.model.AbstractModel;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.Serializable;
/**
* Created by reborn on 2017/7/31.
*/
@Repository
@Transactional
public class BaseDao {
@Resource
private HibernateTemplate hibernateTemplate;
public <T extends AbstractModel>T get(Class<T> T, Serializable pk) throws DataAccessException{
return hibernateTemplate.get(T,pk);
}
public <T extends AbstractModel> void saveOrUpdate(T model)throws DataAccessException{
hibernateTemplate.saveOrUpdate(model);
}
public <T extends AbstractModel> void delete(Class<T> T,Serializable key) throws DataAccessException {
T model= get(T,key);
if(model!=null){
hibernateTemplate.delete(model);
}
}
}

@ -0,0 +1,11 @@
package org.pqh.model;
import java.io.Serializable;
/**
* Created by reborn on 2017/8/3.
*/
public abstract class AbstractModel {
public abstract Serializable primaryKey();
}

@ -0,0 +1,47 @@
package org.pqh.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* Created by reborn on 2017/8/3.
*/
@Entity
@Table(name="testjson")
public class Config extends AbstractModel{
private String jackson;
@Id
@Column(name = "jackson")
public String getJackson() {
return jackson;
}
public void setJackson(String jackson) {
this.jackson = jackson;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Config config = (Config) o;
return jackson.equals(config.jackson);
}
@Override
public int hashCode() {
return jackson.hashCode();
}
@Override
public Serializable primaryKey() {
return getJackson();
}
}

@ -0,0 +1,83 @@
package org.pqh.model;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.io.Serializable;
/**
* Created by reborn on 2017/7/31.
*/
@Entity
public class Param extends AbstractModel{
private String key;
private String value;
private String desc;
@Id
@Column(name = "`key`")
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@Basic
@Column(name = "value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Basic
@Column(name = "`desc`")
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Param param = (Param) o;
if (key != null ? !key.equals(param.key) : param.key != null) return false;
if (value != null ? !value.equals(param.value) : param.value != null) return false;
if (desc != null ? !desc.equals(param.desc) : param.desc != null) return false;
return true;
}
@Override
public int hashCode() {
int result = key != null ? key.hashCode() : 0;
result = 31 * result + (value != null ? value.hashCode() : 0);
result = 31 * result + (desc != null ? desc.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Param{" +
"key='" + key + '\'' +
", value='" + value + '\'' +
", desc='" + desc + '\'' +
'}';
}
@Override
public Serializable primaryKey() {
return getKey();
}
}

@ -0,0 +1,11 @@
package org.pqh.util;
/**
* Created by reborn on 2017/8/2.
*/
public enum DBAction {
Insert,
Update,
Delete,
Select
}

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60" status="debug" >
<Properties>
<Property name="pattern">%d{yyyy-MM-dd HH:mm:ss} %p %l%n%m%n</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="${pattern}"></PatternLayout>
</Console>
<File name="File" fileName="logs/info.log" bufferedIO="true" immediateFlush="true">
<PatternLayout pattern="${pattern}"></PatternLayout>
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.logging.log4j.test1" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/>
</Logger>
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File"/>
</Logger>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world</h1>
</body>
</html>

@ -0,0 +1,41 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import java.net.URI;
import java.net.URISyntaxException;
/**
* Created by reborn on 2017/8/3.
*/
public class JunitTest {
private static final Logger rootLogger= LogManager.getLogger();
private static final Logger consoleLogger= LogManager.getLogger("org.apache.logging.log4j.test1");
private static final Logger loggerFile= LogManager.getLogger("org.apache.logging.log4j.test2");
public static void main(String[] args) {
// rootLogger.info("rootLogger");
// consoleLogger.info("consoleLogger");
// loggerFile.info("loggerFile");
// LoggerContext loggerContext= (LoggerContext) LogManager.getContext(false);
//
// Collection<org.apache.logging.log4j.core.Logger> loggers=loggerContext.getLoggers();
// for(org.apache.logging.log4j.core.Logger logger:loggers){
// logger.info(logger.getName());
// }
LoggerContext loggerContext= (LoggerContext) LogManager.getContext(false);
try {
loggerContext.setConfigLocation(new URI("https://github.com/luffy9412/Bilibili/blob/master/src/main/resources/log4j.properties"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
// loggerContext.setConfigLocation();
loggerContext.reconfigure();
}
}
Loading…
Cancel
Save