From cc37f4fd113f2ff7c3e196fe74a19a7ef5d6f903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=98=E5=95=9F=E5=8D=8E?= <1029559041@qq.com> Date: Sun, 26 Aug 2018 03:32:38 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=8C=E5=8D=95=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/db/config/HibernateConfig.java | 8 +- .../java/web/config/AbstractWebConfig.java | 4 + web/src/main/java/web/config/AppConfig.java | 2 - .../main/java/web/config/SpringConfig.java | 17 +- .../java/web/controller/BaseController.java | 13 +- .../java/web/controller/WordController.java | 33 +++ web/src/main/java/web/filter/Filter.java | 29 +++ .../main/java/web/service/BaseService.java | 19 +- .../main/java/web/service/BiliService.java | 17 ++ web/src/main/webapp/WEB-INF/jsp/head.jsp | 20 +- web/src/main/webapp/WEB-INF/jsp/index.jsp | 6 +- web/src/main/webapp/WEB-INF/jsp/word/word.jsp | 188 ++++++++++++++++++ web/src/test/java/spring/SpringMVCTest.java | 5 +- 13 files changed, 326 insertions(+), 35 deletions(-) create mode 100644 web/src/main/java/web/filter/Filter.java create mode 100644 web/src/main/webapp/WEB-INF/jsp/word/word.jsp diff --git a/db/src/main/java/db/config/HibernateConfig.java b/db/src/main/java/db/config/HibernateConfig.java index eed6871..bdd9093 100644 --- a/db/src/main/java/db/config/HibernateConfig.java +++ b/db/src/main/java/db/config/HibernateConfig.java @@ -117,7 +117,8 @@ public class HibernateConfig { * @return */ private HibernateTemplate initDB(DataSourceModel dataSourceModel) { - SessionFactory sessionFactory = initSessionFactory(dataSource(dataSourceModel), (LocalSessionFactoryBean bean) -> { + DruidDataSource dataSource=dataSource(dataSourceModel); + SessionFactory sessionFactory = initSessionFactory(dataSource, (LocalSessionFactoryBean bean) -> { Class c; try { //按照规则拼接实体类注解标记全类名 @@ -139,6 +140,8 @@ public class HibernateConfig { if (sessionFactory != null) { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(sessionFactory); + hibernateTransactionManager.setDataSource(dataSource); + hibernateTransactionManager.afterPropertiesSet(); //添加事务管理器 beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbId(),DBBeanNameManager.transactionManager),hibernateTransactionManager); return new HibernateTemplate(sessionFactory); @@ -183,9 +186,8 @@ public class HibernateConfig { for (DataSourceModel dataSourceModel : dataSourceModels) { HibernateTemplate hibernateTemplate = initDB(dataSourceModel); if (hibernateTemplate != null) { - //添加事务管理器 - beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbId(),DBBeanNameManager.hibernateTemplate),hibernateTemplate); + beanFactory.registerSingleton(DBBeanNameManager.getName(dataSourceModel.getDbId(),DBBeanNameManager.hibernateTemplate),hibernateTemplate); Class c; try { diff --git a/web/src/main/java/web/config/AbstractWebConfig.java b/web/src/main/java/web/config/AbstractWebConfig.java index 2fa13c8..7ec2185 100644 --- a/web/src/main/java/web/config/AbstractWebConfig.java +++ b/web/src/main/java/web/config/AbstractWebConfig.java @@ -3,6 +3,7 @@ package web.config; import com.alibaba.druid.support.http.StatViewServlet; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; +import web.filter.Filter; import javax.servlet.*; import java.util.EnumSet; @@ -38,6 +39,9 @@ public class AbstractWebConfig extends AbstractAnnotationConfigDispatcherServlet FilterRegistration.Dynamic characterEncodingFilter=super.registerServletFilter(servletContext,new CharacterEncodingFilter(encoding)); characterEncodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*"); + FilterRegistration.Dynamic myfilter=super.registerServletFilter(servletContext,new Filter()); + myfilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*"); + ServletRegistration.Dynamic servlet=servletContext.addServlet("DruidStatView", StatViewServlet.class); servlet.addMapping("/druid/*"); diff --git a/web/src/main/java/web/config/AppConfig.java b/web/src/main/java/web/config/AppConfig.java index f9c2574..25b6d59 100644 --- a/web/src/main/java/web/config/AppConfig.java +++ b/web/src/main/java/web/config/AppConfig.java @@ -5,11 +5,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; -import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableAspectJAutoProxy(proxyTargetClass = true) -@EnableTransactionManagement @PropertySource("classpath:config.properties") @Import(HibernateConfig.class) public class AppConfig { diff --git a/web/src/main/java/web/config/SpringConfig.java b/web/src/main/java/web/config/SpringConfig.java index 8f7953d..511be2d 100644 --- a/web/src/main/java/web/config/SpringConfig.java +++ b/web/src/main/java/web/config/SpringConfig.java @@ -1,14 +1,16 @@ package web.config; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; -import db.config.HibernateConfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.http.MediaType; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor; -import org.springframework.web.servlet.config.annotation.*; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.Arrays; import java.util.List; @@ -18,6 +20,7 @@ import java.util.List; */ @Configuration @EnableWebMvc +@EnableTransactionManagement @PropertySource({"classpath:config.properties"}) @ComponentScan({"web.controller","web.service","web.util","web.aop","core.thrift"}) public class SpringConfig implements WebMvcConfigurer { @@ -34,14 +37,6 @@ public class SpringConfig implements WebMvcConfigurer { "classpath:/META-INF/resources/webjars/"); } - @Override - public void addInterceptors(InterceptorRegistry registry) { - HibernateConfig.get().forEach((key,hibernateTemplate)->{ - OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); - interceptor.setSessionFactory(hibernateTemplate.getSessionFactory()); - registry.addWebRequestInterceptor(interceptor); - }); - } @Override public void configureMessageConverters(List> converters) { diff --git a/web/src/main/java/web/controller/BaseController.java b/web/src/main/java/web/controller/BaseController.java index a0b82b4..d322594 100644 --- a/web/src/main/java/web/controller/BaseController.java +++ b/web/src/main/java/web/controller/BaseController.java @@ -51,12 +51,14 @@ public abstract class BaseController result=new JsonResult(); result.setAction(DBAction.C); try { service.save(model); + result.setData(model); result.setType(Type.success); } catch (DataAccessException e) { e.printStackTrace(); @@ -65,6 +67,7 @@ public abstract class BaseController result=new JsonResult(); @@ -94,12 +97,14 @@ public abstract class BaseController result=new JsonResult(); result.setAction(DBAction.U); try { service.update(model); + result.setData(model); result.setType(Type.success); } catch (DataAccessException e) { e.printStackTrace(); @@ -108,12 +113,14 @@ public abstract class BaseController result=new JsonResult(); result.setAction(DBAction.D); try { service.delete(model); + result.setData(model); result.setType(Type.success); } catch (DataAccessException e) { e.printStackTrace(); diff --git a/web/src/main/java/web/controller/WordController.java b/web/src/main/java/web/controller/WordController.java index 3b28127..9a9f9cd 100644 --- a/web/src/main/java/web/controller/WordController.java +++ b/web/src/main/java/web/controller/WordController.java @@ -1,11 +1,44 @@ package web.controller; import db.model.bilibili.WordModel; +import org.apache.commons.lang3.StringUtils; +import org.hibernate.criterion.DetachedCriteria; +import org.hibernate.criterion.Projections; +import org.hibernate.criterion.Restrictions; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.List; @Controller @RequestMapping("/word") public class WordController extends BiliController { + @Autowired + @Qualifier("Tweb.service.BiliService") + private HibernateTransactionManager hibernateTransactionManager; + + @RequestMapping + public String list(@ModelAttribute WordModel queryCommand, @RequestParam(defaultValue = "0") int firstResult, @RequestParam(defaultValue = "0") int maxResults, Model model){ + model.addAttribute("word",super.get(queryCommand, firstResult, maxResults)); + List courseNames=service.find(DetachedCriteria.forClass(WordModel.class).setProjection(Projections.groupProperty("courseName"))); + model.addAttribute("courseNames",courseNames); + return "word/word"; + } + + @Override + protected DetachedCriteria getDetachedCriteria(WordModel queryCommand) { + if(StringUtils.isNotEmpty(queryCommand.getCourseName())){ + return super.getDetachedCriteria(queryCommand).add(Restrictions.eq("courseName",queryCommand.getCourseName())); + }else{ + return super.getDetachedCriteria(queryCommand); + } + + } } diff --git a/web/src/main/java/web/filter/Filter.java b/web/src/main/java/web/filter/Filter.java new file mode 100644 index 0000000..922fd67 --- /dev/null +++ b/web/src/main/java/web/filter/Filter.java @@ -0,0 +1,29 @@ +package web.filter; + +import db.annotation.Aliyun; +import db.config.HibernateConfig; +import org.hibernate.FlushMode; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter; + + +public class Filter extends OpenSessionInViewFilter { + @Override + protected SessionFactory lookupSessionFactory() { + return HibernateConfig.get(Aliyun.class).getSessionFactory(); + } + + @Override + protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException { + try { + Session session = sessionFactory.openSession(); + session.setHibernateFlushMode(FlushMode.AUTO); + return session; + } catch (HibernateException var3) { + throw new DataAccessResourceFailureException("Could not open Hibernate Session", var3); + } + } +} diff --git a/web/src/main/java/web/service/BaseService.java b/web/src/main/java/web/service/BaseService.java index 88334a6..84910e0 100644 --- a/web/src/main/java/web/service/BaseService.java +++ b/web/src/main/java/web/service/BaseService.java @@ -31,25 +31,28 @@ public abstract class BaseService{ return hibernateTemplate; } - public final Serializable save(T command) throws DataAccessException { + public Serializable save(T command) throws DataAccessException { return getHibernateTemplate().save(command); } - public final void update(T command) throws DataAccessException{ + public void update(T command) throws DataAccessException{ getHibernateTemplate().update(command); } - public final T get(T command) throws DataAccessException{ - T model=(T)getHibernateTemplate().get(command.getClass(),command.primaryKey()); - if(model!=null){ + public final T get(T command) throws DataAccessException { + T model = (T) getHibernateTemplate().get(command.getClass(), command.primaryKey()); + if (model != null) { return model.cast(); - }else{ + } else { return null; } } - public final void delete(T command) throws DataAccessException{ - getHibernateTemplate().delete(get(command)); + public void delete(T command) throws DataAccessException{ + T model=get(command); + if(model!=null){ + getHibernateTemplate().delete(model); + } } public final List find(DetachedCriteria criteria, int firstResult,int maxResults) throws DataAccessException{ diff --git a/web/src/main/java/web/service/BiliService.java b/web/src/main/java/web/service/BiliService.java index 72f2889..6df9c25 100644 --- a/web/src/main/java/web/service/BiliService.java +++ b/web/src/main/java/web/service/BiliService.java @@ -2,10 +2,27 @@ package web.service; import db.annotation.Aliyun; import db.annotation.Model; +import db.model.AbstractModel; +import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.Serializable; + @Service @Model(Aliyun.class) + public class BiliService extends BaseService{ + @Override + @Transactional(value = "Tweb.service.BiliService") + public Serializable save(T command) throws DataAccessException { + return super.save(command); + } + @Override + @Transactional(value = "Tweb.service.BiliService") + public void delete(T command) throws DataAccessException { + super.delete(command); + } } diff --git a/web/src/main/webapp/WEB-INF/jsp/head.jsp b/web/src/main/webapp/WEB-INF/jsp/head.jsp index 296a8ed..da32258 100644 --- a/web/src/main/webapp/WEB-INF/jsp/head.jsp +++ b/web/src/main/webapp/WEB-INF/jsp/head.jsp @@ -30,6 +30,24 @@ - + diff --git a/web/src/main/webapp/WEB-INF/jsp/index.jsp b/web/src/main/webapp/WEB-INF/jsp/index.jsp index 3132ea9..c269a55 100644 --- a/web/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/web/src/main/webapp/WEB-INF/jsp/index.jsp @@ -66,14 +66,10 @@ Close  -
+
-
- - -
diff --git a/web/src/main/webapp/WEB-INF/jsp/word/word.jsp b/web/src/main/webapp/WEB-INF/jsp/word/word.jsp new file mode 100644 index 0000000..9c0ac89 --- /dev/null +++ b/web/src/main/webapp/WEB-INF/jsp/word/word.jsp @@ -0,0 +1,188 @@ +<%-- + Created by IntelliJ IDEA. + User: 潘啟华 + Date: 2018/8/25 + Time: 13:03 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + Title + + + + + + + + +
+ +
+ +
style="display: none"> + +
+ + + + + + +
+ + + +
+ +
+
+
+ +
+ +
+ + diff --git a/web/src/test/java/spring/SpringMVCTest.java b/web/src/test/java/spring/SpringMVCTest.java index 987a29d..e768192 100644 --- a/web/src/test/java/spring/SpringMVCTest.java +++ b/web/src/test/java/spring/SpringMVCTest.java @@ -53,6 +53,7 @@ public class SpringMVCTest { @Test @Transactional + @Rollback(false) public void save() throws Exception { ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.post("/word/C").param("courseName","123").param("kana","456").param("chinese","789")); resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); @@ -62,7 +63,7 @@ public class SpringMVCTest { @Transactional @Rollback(false) public void update() throws Exception{ - ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.post("/word/U").param("id","1").param("courseName","123").param("kana","666").param("chinese","666")); + ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.post("/word/U").param("id","2").param("courseName","123").param("kana","666").param("chinese","666")); resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); } @@ -71,7 +72,7 @@ public class SpringMVCTest { @Transactional @Rollback(false) public void delete() throws Exception{ - ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.post("/word/D").param("id","1")); + ResultActions resultActions=mockMvc.perform(MockMvcRequestBuilders.post("/word/D").param("id","2")); resultActions.andDo(MockMvcResultHandlers.print()).andReturn(); }