From f905e69d243ad8a2e5bbfde85a9e44836edf605b Mon Sep 17 00:00:00 2001 From: "renqun.yuan" Date: Sat, 12 Dec 2015 09:03:10 +0800 Subject: [PATCH 1/3] mybatis old way --- .../com/javalearning/dao/BookDaoOldWay.java | 21 +++++++++++++++++++ .../javalearning/service/BookServiceTest.java | 8 +++++++ 2 files changed, 29 insertions(+) create mode 100644 mybatisExample/src/main/java/com/javalearning/dao/BookDaoOldWay.java diff --git a/mybatisExample/src/main/java/com/javalearning/dao/BookDaoOldWay.java b/mybatisExample/src/main/java/com/javalearning/dao/BookDaoOldWay.java new file mode 100644 index 0000000..1fb85a0 --- /dev/null +++ b/mybatisExample/src/main/java/com/javalearning/dao/BookDaoOldWay.java @@ -0,0 +1,21 @@ +package com.javalearning.dao; + +import com.javalearning.model.Book; +import com.javalearning.util.SessionFactoryUtil; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; + +/** + * Created by renqun.yuan on 2015/12/12. + */ +public class BookDaoOldWay { + + private SqlSessionFactory sqlSessionFactory = SessionFactoryUtil.getSqlSessionFactory().get(); + + public Book queryBookById(Integer id) { + try (SqlSession sqlSession = sqlSessionFactory.openSession()) { + Book book = sqlSession.selectOne("com.javalearning.dao.BookDao.selectBookById", id); + return book; + } + } +} diff --git a/mybatisExample/src/test/java/com/javalearning/service/BookServiceTest.java b/mybatisExample/src/test/java/com/javalearning/service/BookServiceTest.java index 2464c4c..73ce762 100644 --- a/mybatisExample/src/test/java/com/javalearning/service/BookServiceTest.java +++ b/mybatisExample/src/test/java/com/javalearning/service/BookServiceTest.java @@ -1,6 +1,7 @@ package com.javalearning.service; import com.google.common.collect.Lists; +import com.javalearning.dao.BookDaoOldWay; import com.javalearning.model.Book; import org.junit.Assert; import org.junit.Test; @@ -99,4 +100,11 @@ public void testInsertBookWithoutConfigFile() { book.setAuthor("果果"); bookService.insertBookWithoutConfigFile(book); } + + @Test + public void testOldWayDao() { + BookDaoOldWay bookDaoOldWay = new BookDaoOldWay(); + Book book = bookDaoOldWay.queryBookById(6); + System.out.println(book); + } } \ No newline at end of file From 8cf384b98fce28847f15d7b82d5db8c5465971c2 Mon Sep 17 00:00:00 2001 From: "renqun.yuan" Date: Mon, 14 Mar 2016 16:12:29 +0800 Subject: [PATCH 2/3] add spring publish and sub event example --- .../test/java/com/javalearning/ModelTest.java | 27 +++++++++++ .../test/java/com/javalearning/TestMain.java | 46 +++++++++++++++++++ pom.xml | 8 +--- springmvcexample/pom.xml | 6 +++ .../mvc/evnentnotfy/EventPublishService.java | 31 +++++++++++++ .../spring/mvc/evnentnotfy/NotifyEvent.java | 28 +++++++++++ .../mvc/evnentnotfy/NotifyEventListener.java | 26 +++++++++++ .../applicationContext.xml | 0 .../src/main/webapp/WEB-INF/web.xml | 9 ++-- .../evnentnotfy/EventPublishServiceTest.java | 23 ++++++++++ 10 files changed, 192 insertions(+), 12 deletions(-) create mode 100644 designPatterns/src/test/java/com/javalearning/ModelTest.java create mode 100644 designPatterns/src/test/java/com/javalearning/TestMain.java create mode 100644 springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java create mode 100644 springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEvent.java create mode 100644 springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEventListener.java rename springmvcexample/src/main/{webapp/WEB-INF => resources}/applicationContext.xml (100%) create mode 100644 springmvcexample/src/test/com/spring/mvc/evnentnotfy/EventPublishServiceTest.java diff --git a/designPatterns/src/test/java/com/javalearning/ModelTest.java b/designPatterns/src/test/java/com/javalearning/ModelTest.java new file mode 100644 index 0000000..e917204 --- /dev/null +++ b/designPatterns/src/test/java/com/javalearning/ModelTest.java @@ -0,0 +1,27 @@ +package com.javalearning; + +import java.util.List; + +/** + * Created by renqun.yuan on 2016/3/2. + */ +public class ModelTest { + private String key; + private List modelTestList; + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public List getModelTestList() { + return modelTestList; + } + + public void setModelTestList(List modelTestList) { + this.modelTestList = modelTestList; + } +} diff --git a/designPatterns/src/test/java/com/javalearning/TestMain.java b/designPatterns/src/test/java/com/javalearning/TestMain.java new file mode 100644 index 0000000..029cd58 --- /dev/null +++ b/designPatterns/src/test/java/com/javalearning/TestMain.java @@ -0,0 +1,46 @@ +package com.javalearning; + +import com.google.common.collect.Lists; +import org.apache.commons.collections4.CollectionUtils; + +import java.util.List; + +/** + * Created by renqun.yuan on 2016/3/2. + */ +public class TestMain { + public static void main(String[] args) { + ModelTest modelTest = new ModelTest(); + modelTest.setKey("0MOde0"); + ModelTest modelTest1 = new ModelTest(); + modelTest1.setKey("0MOdel"); + modelTest1.setModelTestList(Lists.newArrayList(modelTest)); + ModelTest modelTest2 = new ModelTest(); + modelTest2.setKey("0MOde2"); + modelTest2.setModelTestList(Lists.newArrayList(modelTest1)); + List urls = Lists.newArrayList(); + + TestMain testMain = new TestMain(); + testMain.build(urls, modelTest2); + System.out.println(urls); + + } + + private void build(List urls , ModelTest modelTest) { + if (modelTest == null) { + return; + } + + urls.add(modelTest.getKey()); + + List children = modelTest.getModelTestList(); + + if (CollectionUtils.isEmpty(children)) { + return; + } + + for (ModelTest child : children) { + build(urls, child); + } + } +} diff --git a/pom.xml b/pom.xml index 5a55c3c..1d4f742 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ UTF-8 3.8.1 - 18.0 + 19.0 1.2.0.RELEASE 4.1.1.RELEASE 4.0 @@ -99,37 +99,31 @@ org.slf4j slf4j-api ${org.slf4j.version} - runtime org.slf4j jcl-over-slf4j ${org.slf4j.version} - runtime org.slf4j jul-to-slf4j ${org.slf4j.version} - runtime org.slf4j log4j-over-slf4j ${org.slf4j.version} - runtime ch.qos.logback logback-classic ${logback.version} - runtime ch.qos.logback logback-core ${logback.version} - runtime javax.servlet diff --git a/springmvcexample/pom.xml b/springmvcexample/pom.xml index df70e94..be92ead 100644 --- a/springmvcexample/pom.xml +++ b/springmvcexample/pom.xml @@ -85,5 +85,11 @@ javax.servlet jstl + + junit + junit + 4.12 + test + diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java new file mode 100644 index 0000000..508047e --- /dev/null +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java @@ -0,0 +1,31 @@ +package com.spring.mvc.evnentnotfy; + +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; +import org.springframework.stereotype.Service; + +/** + * Created by renqun.yuan on 2016/3/14. + */ +@Service +public class EventPublishService implements ApplicationEventPublisherAware { + private ApplicationEventPublisher applicationEventPublisher; + + public void doSomething(String userName) { + // do something + applicationEventPublisher.publishEvent(new NotifyEvent(userName)); + } + + /** + * Set the ApplicationEventPublisher that this object runs in. + *

Invoked after population of normal bean properties but before an init + * callback like InitializingBean's afterPropertiesSet or a custom init-method. + * Invoked before ApplicationContextAware's setApplicationContext. + * + * @param applicationEventPublisher event publisher to be used by this object + */ + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.applicationEventPublisher = applicationEventPublisher; + } +} diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEvent.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEvent.java new file mode 100644 index 0000000..86c45d9 --- /dev/null +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEvent.java @@ -0,0 +1,28 @@ +package com.spring.mvc.evnentnotfy; + +import org.springframework.context.ApplicationEvent; + +/** + * Created by renqun.yuan on 2016/3/14. + */ +public class NotifyEvent extends ApplicationEvent { + private String userName; + + /** + * Create a new ApplicationEvent. + * + * @param source the component that published the event (never {@code null}) + */ + public NotifyEvent(String source) { + super(source); + this.userName = source; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } +} diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEventListener.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEventListener.java new file mode 100644 index 0000000..0685d74 --- /dev/null +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/NotifyEventListener.java @@ -0,0 +1,26 @@ +package com.spring.mvc.evnentnotfy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Service; + +/** + * Created by renqun.yuan on 2016/3/14. + */ +@Service +public class NotifyEventListener implements ApplicationListener { + private static final Logger logger = LoggerFactory.getLogger(NotifyEventListener.class); + + /** + * Handle an application event. + * + * @param event the event to respond to + */ + @Override + public void onApplicationEvent(NotifyEvent event) { + String userName = event.getUserName(); + // todo something , maybe send email, here just logger some info + logger.info("please say hello, {}", userName); + } +} diff --git a/springmvcexample/src/main/webapp/WEB-INF/applicationContext.xml b/springmvcexample/src/main/resources/applicationContext.xml similarity index 100% rename from springmvcexample/src/main/webapp/WEB-INF/applicationContext.xml rename to springmvcexample/src/main/resources/applicationContext.xml diff --git a/springmvcexample/src/main/webapp/WEB-INF/web.xml b/springmvcexample/src/main/webapp/WEB-INF/web.xml index 75f1207..772b050 100644 --- a/springmvcexample/src/main/webapp/WEB-INF/web.xml +++ b/springmvcexample/src/main/webapp/WEB-INF/web.xml @@ -3,10 +3,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> - - contextConfigLocation - /WEB-INF/applicationContext.xml - encodingFilter org.springframework.web.filter.CharacterEncodingFilter @@ -27,7 +23,10 @@ org.springframework.web.context.ContextLoaderListener - + + contextConfigLocation + classpath:applicationContext.xml + dispatcher org.springframework.web.servlet.DispatcherServlet diff --git a/springmvcexample/src/test/com/spring/mvc/evnentnotfy/EventPublishServiceTest.java b/springmvcexample/src/test/com/spring/mvc/evnentnotfy/EventPublishServiceTest.java new file mode 100644 index 0000000..f79b7e3 --- /dev/null +++ b/springmvcexample/src/test/com/spring/mvc/evnentnotfy/EventPublishServiceTest.java @@ -0,0 +1,23 @@ +package com.spring.mvc.evnentnotfy; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import javax.annotation.Resource; + +/** + * Created by renqun.yuan on 2016/3/14. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({"classpath:applicationContext.xml"}) +public class EventPublishServiceTest { + @Resource + private EventPublishService eventPublishService; + + @Test + public void testPublishEvent() throws Exception { + eventPublishService.doSomething("spring mvc "); + } +} \ No newline at end of file From 80b4a7c5ac806e0013d3e44c8abdde1362144a9e Mon Sep 17 00:00:00 2001 From: "renqun.yuan" Date: Tue, 19 Apr 2016 19:05:05 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BA=8B=E5=8A=A1?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6listener?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 18 ++++++++++++- springmvcexample/pom.xml | 12 +++++++++ .../mvc/evnentnotfy/EventPublishService.java | 3 +++ .../TransactionEventClassListener.java | 27 +++++++++++++++++++ .../evnentnotfy/TransactionNotifyEvent.java | 27 +++++++++++++++++++ .../src/main/resources/applicationContext.xml | 18 +++++++++++-- .../src/main/resources/jdbc.properties | 4 +++ 7 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionEventClassListener.java create mode 100644 springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionNotifyEvent.java create mode 100644 springmvcexample/src/main/resources/jdbc.properties diff --git a/pom.xml b/pom.xml index 1d4f742..5a997a3 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ 3.8.1 19.0 1.2.0.RELEASE - 4.1.1.RELEASE + 4.2.5.RELEASE 4.0 3.4 0.10.0 @@ -34,6 +34,7 @@ 2.5 1.2 1.2.4 + 5.1.34 @@ -90,11 +91,26 @@ spring-webmvc ${org.springframework.version} + + org.springframework + spring-jdbc + ${org.springframework.version} + + + org.springframework + spring-tx + ${org.springframework.version} + org.springframework spring-context-support ${org.springframework.version} + + mysql + mysql-connector-java + ${mysql-connector-java.version} + org.slf4j slf4j-api diff --git a/springmvcexample/pom.xml b/springmvcexample/pom.xml index be92ead..0ed1ef4 100644 --- a/springmvcexample/pom.xml +++ b/springmvcexample/pom.xml @@ -47,6 +47,10 @@ org.springframework spring-webmvc + + org.springframework + spring-jdbc + org.springframework spring-context-support @@ -91,5 +95,13 @@ 4.12 test + + org.springframework + spring-tx + + + mysql + mysql-connector-java + diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java index 508047e..16ca684 100644 --- a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/EventPublishService.java @@ -3,6 +3,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; /** * Created by renqun.yuan on 2016/3/14. @@ -11,9 +12,11 @@ public class EventPublishService implements ApplicationEventPublisherAware { private ApplicationEventPublisher applicationEventPublisher; + @Transactional public void doSomething(String userName) { // do something applicationEventPublisher.publishEvent(new NotifyEvent(userName)); + applicationEventPublisher.publishEvent(new TransactionNotifyEvent(userName)); } /** diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionEventClassListener.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionEventClassListener.java new file mode 100644 index 0000000..b4a223b --- /dev/null +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionEventClassListener.java @@ -0,0 +1,27 @@ +package com.spring.mvc.evnentnotfy; + +import org.springframework.stereotype.Component; +import org.springframework.transaction.event.TransactionPhase; +import org.springframework.transaction.event.TransactionalEventListener; + +/** + * Created by rq on 2016/4/19. + */ +@Component +public class TransactionEventClassListener { + + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT) + public void doSomething(TransactionNotifyEvent transactionNotifyEvent) { + System.out.println("提交之前" + transactionNotifyEvent.getUserName()); + } + + @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) + public void doSomething2(TransactionNotifyEvent transactionNotifyEvent) { + System.out.println("提交之后" + transactionNotifyEvent.getUserName()); + } + + @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, fallbackExecution = true) + public void doSomething3(TransactionNotifyEvent transactionNotifyEvent) { + System.out.println("提交完成" + transactionNotifyEvent.getUserName()); + } +} diff --git a/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionNotifyEvent.java b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionNotifyEvent.java new file mode 100644 index 0000000..88d8114 --- /dev/null +++ b/springmvcexample/src/main/java/com/spring/mvc/evnentnotfy/TransactionNotifyEvent.java @@ -0,0 +1,27 @@ +package com.spring.mvc.evnentnotfy; + +import org.springframework.context.ApplicationEvent; + +/** + * Created by rq on 2016/4/19. + */ +public class TransactionNotifyEvent extends ApplicationEvent { + private String userName; + /** + * Create a new ApplicationEvent. + * + * @param source the component that published the event (never {@code null}) + */ + public TransactionNotifyEvent(String source) { + super(source); + this.userName = source; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } +} diff --git a/springmvcexample/src/main/resources/applicationContext.xml b/springmvcexample/src/main/resources/applicationContext.xml index 4486d35..110f4cb 100644 --- a/springmvcexample/src/main/resources/applicationContext.xml +++ b/springmvcexample/src/main/resources/applicationContext.xml @@ -2,8 +2,8 @@ + xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> @@ -23,4 +23,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/springmvcexample/src/main/resources/jdbc.properties b/springmvcexample/src/main/resources/jdbc.properties new file mode 100644 index 0000000..e6939f1 --- /dev/null +++ b/springmvcexample/src/main/resources/jdbc.properties @@ -0,0 +1,4 @@ +driverClassName=com.mysql.jdbc.Driver +url=jdbc:mysql://localhost:3306/mybatis +password=1234567890 +user=root \ No newline at end of file