commit c26418aaf8c3826009ba8dca22b017649f06f3aa Author: luffy9412 <1029559041@qq.com> Date: Mon Aug 7 18:33:08 2017 +0800 初始化 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..91d8903 --- /dev/null +++ b/pom.xml @@ -0,0 +1,120 @@ + + + 4.0.0 + + org.pqh + webCrawler + pom + 1.0 + + 简单的网络爬虫客户端 + + + UTF-8 + UTF-8 + 1.8 + 6.0.6 + + + + + + org.springframework + spring-web + LATEST + + + + org.springframework + spring-webmvc + LATEST + + + + org.springframework + spring-orm + LATEST + + + + + + + + + + + + + + + + org.apache.commons + commons-dbcp2 + LATEST + + + org.hibernate + hibernate-core + LATEST + + + + mysql + mysql-connector-java + ${mysql.version} + + + + com.fasterxml.jackson.core + jackson-databind + LATEST + + + + org.aspectj + aspectjweaver + LATEST + + + + org.apache.logging.log4j + log4j-api + LATEST + + + + org.apache.logging.log4j + log4j-core + LATEST + + + + org.fusesource.jansi + jansi + LATEST + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.1 + + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + + + diff --git a/src/main/java/org/pqh/aop/InsertAdvice.java b/src/main/java/org/pqh/aop/InsertAdvice.java new file mode 100644 index 0000000..4ec3ac2 --- /dev/null +++ b/src/main/java/org/pqh/aop/InsertAdvice.java @@ -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)); + } +} diff --git a/src/main/java/org/pqh/aop/InsertAdvisor.java b/src/main/java/org/pqh/aop/InsertAdvisor.java new file mode 100644 index 0000000..9c0a8b7 --- /dev/null +++ b/src/main/java/org/pqh/aop/InsertAdvisor.java @@ -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); + } + +} diff --git a/src/main/java/org/pqh/aop/LogAspect.java b/src/main/java/org/pqh/aop/LogAspect.java new file mode 100644 index 0000000..0b4647d --- /dev/null +++ b/src/main/java/org/pqh/aop/LogAspect.java @@ -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); + } + } + +} diff --git a/src/main/java/org/pqh/aop/MatcherRule.java b/src/main/java/org/pqh/aop/MatcherRule.java new file mode 100644 index 0000000..ecce2be --- /dev/null +++ b/src/main/java/org/pqh/aop/MatcherRule.java @@ -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(){} +} diff --git a/src/main/java/org/pqh/config/HibernateConfig.java b/src/main/java/org/pqh/config/HibernateConfig.java new file mode 100644 index 0000000..e20ba06 --- /dev/null +++ b/src/main/java/org/pqh/config/HibernateConfig.java @@ -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; + } + + +} diff --git a/src/main/java/org/pqh/config/SpringConfig.java b/src/main/java/org/pqh/config/SpringConfig.java new file mode 100644 index 0000000..4127739 --- /dev/null +++ b/src/main/java/org/pqh/config/SpringConfig.java @@ -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; + } + + +} diff --git a/src/main/java/org/pqh/config/WebConfig.java b/src/main/java/org/pqh/config/WebConfig.java new file mode 100644 index 0000000..13ffed1 --- /dev/null +++ b/src/main/java/org/pqh/config/WebConfig.java @@ -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,"/*"); + + + + } + +} + + diff --git a/src/main/java/org/pqh/controller/AbstractController.java b/src/main/java/org/pqh/controller/AbstractController.java new file mode 100644 index 0000000..edb0f04 --- /dev/null +++ b/src/main/java/org/pqh/controller/AbstractController.java @@ -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 { + + @Resource + private BaseDao baseDao; + + @RequestMapping("/") + public String index(){ + return "index"; + } + + + public String curd(DBAction action,T formModel){ + Class tClass= (Class) 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"; + } + +} diff --git a/src/main/java/org/pqh/controller/ParamController.java b/src/main/java/org/pqh/controller/ParamController.java new file mode 100644 index 0000000..c8f3b0c --- /dev/null +++ b/src/main/java/org/pqh/controller/ParamController.java @@ -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 { + @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); + } +} diff --git a/src/main/java/org/pqh/dao/BaseDao.java b/src/main/java/org/pqh/dao/BaseDao.java new file mode 100644 index 0000000..1a3d860 --- /dev/null +++ b/src/main/java/org/pqh/dao/BaseDao.java @@ -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 get(Class T, Serializable pk) throws DataAccessException{ + return hibernateTemplate.get(T,pk); + } + + public void saveOrUpdate(T model)throws DataAccessException{ + hibernateTemplate.saveOrUpdate(model); + } + + public void delete(Class T,Serializable key) throws DataAccessException { + T model= get(T,key); + if(model!=null){ + hibernateTemplate.delete(model); + } + } +} diff --git a/src/main/java/org/pqh/model/AbstractModel.java b/src/main/java/org/pqh/model/AbstractModel.java new file mode 100644 index 0000000..2df4b98 --- /dev/null +++ b/src/main/java/org/pqh/model/AbstractModel.java @@ -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(); +} diff --git a/src/main/java/org/pqh/model/Config.java b/src/main/java/org/pqh/model/Config.java new file mode 100644 index 0000000..69eea3d --- /dev/null +++ b/src/main/java/org/pqh/model/Config.java @@ -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(); + } +} diff --git a/src/main/java/org/pqh/model/Param.java b/src/main/java/org/pqh/model/Param.java new file mode 100644 index 0000000..52ed8a7 --- /dev/null +++ b/src/main/java/org/pqh/model/Param.java @@ -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(); + } +} diff --git a/src/main/java/org/pqh/util/DBAction.java b/src/main/java/org/pqh/util/DBAction.java new file mode 100644 index 0000000..1fd431c --- /dev/null +++ b/src/main/java/org/pqh/util/DBAction.java @@ -0,0 +1,11 @@ +package org.pqh.util; + +/** + * Created by reborn on 2017/8/2. + */ +public enum DBAction { + Insert, + Update, + Delete, + Select +} diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..96f54c7 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,26 @@ + + + + %d{yyyy-MM-dd HH:mm:ss} %p %l%n%m%n + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp new file mode 100644 index 0000000..7ec7e4e --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -0,0 +1,10 @@ + + + + + Title + + +

hello world

+ + \ No newline at end of file diff --git a/src/test/java/JunitTest.java b/src/test/java/JunitTest.java new file mode 100644 index 0000000..717ddb9 --- /dev/null +++ b/src/test/java/JunitTest.java @@ -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 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(); + } + +}