时间:2021-07-01 10:21:17 帮助过:15人阅读
3,配置:web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 配置前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加载前端控制器配置文件 上下文配置位置--> <init-param> <!-- contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml(例如springmvc-servlet.xml)。 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <!-- 表示随WEB服务器启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 备注:可以拦截三种请求 第一种:拦截固定后缀的url,比如设置为 *.do、*.action,例如:/user/add.action 此方法最简单,不会导致静态资源(jpg,js,css)被拦截. 第二种:拦截所有,设置为/,例如:/user/add /user/add.action此方法可以实现REST风格的url, 很多互联网类型的应用使用这种方式.但是此方法会导致静态文件(jpg,js,css)被拦截后不能正常显示.需要特殊处理. 第三种:拦截所有,设置为/*,此设置方法错误,因为请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功. --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>View Code
4,applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd"> 3 <beans> 4 <import resource="datasource-spring.xml" /> 5 <!-- servicemvc --> 6 <import resource="springmvc.xml" /> 7 <!-- spring --> 8 <import resource="spring.xml" /> 9 </beans>View Code
5,jdbc.properties
1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://127.0.0.1:3306/yun 3 jdbc.username=root 4 jdbc.password=rootView Code
6,datasource-spring.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/tx 11 http://www.springframework.org/schema/tx/spring-tx.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd 14 http://www.springframework.org/schema/mvc 15 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 16 17 <aop:aspectj-autoproxy /> 18 19 <tx:annotation-driven /> 20 <!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd"> --> 21 <!-- <beans> --> 22 <bean id="propertyConfigure" 23 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 24 <property name="locations"> 25 <list> 26 <value>classpath*:jdbc.properties</value> 27 </list> 28 </property> 29 </bean> 30 31 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 32 init-method="init" destroy-method="close"> 33 <property name="driverClassName" value="${jdbc.driverClassName}" /> 34 <property name="url" value="${jdbc.url}" /> 35 <property name="username" value="${jdbc.username}" /> 36 <property name="password" value="${jdbc.password}" /> 37 <!-- 配置初始化大小、最小、最大 --> 38 <property name="initialSize" value="1" /> 39 <property name="minIdle" value="1" /> 40 <property name="maxActive" value="10" /> 41 42 <!-- 配置获取连接等待超时的时间 10秒 --> 43 <property name="maxWait" value="10000" /> 44 45 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 46 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 47 48 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 49 <property name="minEvictableIdleTimeMillis" value="300000" /> 50 51 <property name="testWhileIdle" value="true" /> 52 53 <!-- 这里建议配置为TRUE,防止取到的连接不可用 --> 54 <property name="testOnBorrow" value="true" /> 55 <property name="testOnReturn" value="false" /> 56 57 <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> 58 <property name="poolPreparedStatements" value="true" /> 59 <property name="maxPoolPreparedStatementPerConnectionSize" 60 value="20" /> 61 62 <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 --> 63 64 <property name="defaultAutoCommit" value="true" /> 65 66 <!-- 验证连接有效与否的SQL,不同的数据配置不同 --> 67 <property name="validationQuery" value="select 1 from dual " /> 68 <property name="filters" value="log4j" /> 69 <property name="proxyFilters"> 70 <list> 71 <!-- web 监控方式 --> 72 <!-- <ref bean="stat-filter" /> --> 73 <!-- 日志文件监控方式 --> 74 <ref bean="logFilter" /> 75 </list> 76 </property> 77 </bean> 78 79 <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter"> 80 <property name="mergeSql" value="true" /> 81 <property name="slowSqlMillis" value="10000" /> 82 <property name="logSlowSql" value="true" /> 83 </bean> 84 85 <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter"> 86 <property name="statementExecutableSqlLogEnable" value="false" /> 87 </bean> 88 89 <!-- jdbc事务管理器 --> 90 <bean id="transactionManager" 91 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 92 <property name="dataSource"> 93 <ref bean="dataSource" /> 94 </property> 95 </bean> 96 <!-- 配置事物传播特性,(REQUIRED)是否存在事务,不存在则创建一个事务 --> 97 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 98 <tx:attributes> 99 <tx:method name="save*" propagation="REQUIRED" /> 100 <tx:method name="add*" propagation="REQUIRED" /> 101 <tx:method name="create*" propagation="REQUIRED" /> 102 <tx:method name="insert*" propagation="REQUIRED" /> 103 <tx:method name="update*" propagation="REQUIRED" /> 104 <tx:method name="merge*" propagation="REQUIRED" /> 105 <tx:method name="del*" propagation="REQUIRED" /> 106 <tx:method name="remove*" propagation="REQUIRED" /> 107 <tx:method name="query*" read-only="true"/> 108 <tx:method name="use*" read-only="true"/> 109 <tx:method name="get*" read-only="true" /> 110 <tx:method name="count*" read-only="true" /> 111 <tx:method name="find*" read-only="true" /> 112 <tx:method name="list*" read-only="true" /> 113 <tx:method name="*" propagation="REQUIRED"/> 114 </tx:attributes> 115 </tx:advice> 116 117 <!-- 切面,配置参与事务的类 --> 118 <aop:config > 119 <aop:pointcut id="allMethod" expression="execution(* com.datasource.service.impl.*.*(..))" /> 120 <!-- <aop:pointcut id="mqserviceMethod" expression="execution(* com.midea.plms.*.mqservice.*.*(..))" /> --> 121 <!-- <aop:pointcut id="hessianMethod" expression="execution(* com.midea.plms.*.hessain.impl.*.*(..))" /> --> 122 123 <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" /> 124 <!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="mqserviceMethod" /> --> 125 <!-- <aop:advisor advice-ref="txAdvice" pointcut-ref="hessianMethod" /> --> 126 127 <!-- <aop:aspect order="-2147483648" ref="readWriteDataSourceTransactionProcessor"> 128 <aop:around pointcut-ref="allMethod" method="determineReadOrWriteDB"/> 129 </aop:aspect> 130 <aop:aspect order="-2147483647" ref="readWriteDataSourceTransactionProcessor"> 131 <aop:around pointcut-ref="mqserviceMethod" method="determineReadOrWriteDB"/> 132 </aop:aspect> 133 <aop:aspect order="-2147483646" ref="readWriteDataSourceTransactionProcessor"> 134 <aop:around pointcut-ref="hessianMethod" method="determineReadOrWriteDB"/> 135 </aop:aspect> --> 136 </aop:config> 137 138 <!-- spring jdbc --> 139 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 140 <property name="dataSource"> 141 <ref bean="dataSource" /> 142 </property> 143 </bean> 144 </beans>View Code
7,spring.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd"> 3 <beans> 4 <bean id="addTestOperator" class="com.datasource.operator.AddTestOperator"> 5 <property name="testService"> 6 <ref bean="testService" /> 7 </property> 8 </bean> 9 10 <bean id="testService" class="com.datasource.service.TestServiceImp"> 11 <property name="testDao"> 12 <ref bean="testDao" /> 13 </property> 14 </bean> 15 16 <bean id="testDao" class="com.datasource.dao.TestDao"> 17 <property name="jdbcTemplate"> 18 <ref bean="jdbcTemplate" /> 19 </property> 20 </bean> 21 </beans>View Code
8,springmvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd 9 http://www.springframework.org/schema/tx 10 http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 13 <!-- 配置扫描的包 --> 14 <context:component-scan base-package="com.lizj" /> 15 16 <!-- 注册HandlerMapper、HandlerAdapter两个映射类 无需配置,默认使用默认的处理 --> 17 <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 --> 18 <mvc:annotation-driven /> 19 20 <!-- 访问静态资源 --> 21 <mvc:default-servlet-handler /> 22 23 <!-- 视图解析器 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> --> 24 <bean 25 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 26 <property name="prefix" value="/view/"></property> 27 <property name="suffix" value=".jsp"></property> 28 </bean> 29 </beans>View Code
9,后台测试:
1 private JdbcTemplate jdbcTemplate; 2 3 4 public JdbcTemplate getJdbcTemplate() { 5 return jdbcTemplate; 6 } 7 8 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 9 this.jdbcTemplate = jdbcTemplate; 10 } 11 12 public int addStudent(){ 13 String sql = "INSERT INTO STUDENT(id,name,age) VALUES(?,?,?)"; 14 List<Object> list = new ArrayList<Object>(); 15 list.add(99); 16 list.add("3737"); 17 list.add("22111"); 18 return jdbcTemplate.update(sql, list.toArray()); 19 } 20 21 TestService: 22 23 void addTest(); 24 25 AddTestOperator: 26 27 Logger log = Logger.getLogger(getClass()); 28 29 private TestService testService; 30 31 public TestService getTestService() { 32 return testService; 33 } 34 35 public void setTestService(TestService testService) { 36 this.testService = testService; 37 } 38 39 public void addTest(){ 40 log.info("AddTestOperator.addTest"); 41 testService.addTest(); 42 } 43 44 TestServiceImp: 45 46 Logger log = Logger.getLogger(getClass()); 47 48 private TestDao testDao; 49 50 public TestDao getTestDao() { 51 return testDao; 52 } 53 54 public void setTestDao(TestDao testDao) { 55 this.testDao = testDao; 56 } 57 58 public void addTest() { 59 log.info(getClass()+".addTest"); 60 int i = testDao.addStudent(); 61 System.out.println(i); 62 }View Code
测试:JunitTest
@Test public void addStudent(){ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AddTestOperator AddTestOperator = (com.datasource.operator.AddTestOperator) context.getBean("addTestOperator"); AddTestOperator.addTest(); }View Code
druid数据库连接池
标签:qos sed 存在 template mysq between port express ble