簡單之美,springmvc,mybatis就是一個很好的簡單集成方案,能夠滿足一般的項目需求。閒暇時間把項目配置文件共享出來,供大家參看:
1.首先我們來看下依賴的pom:
1 <!-- spring --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 <version>${spring.version}</version> 6 </dependency> 7 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-beans</artifactId> 11 <version>${spring.version}</version> 12 </dependency> 13 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-context</artifactId> 17 <version>${spring.version}</version> 18 </dependency> 19 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-tx</artifactId> 23 <version>${spring.version}</version> 24 </dependency> 25 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-web</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-webmvc</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-jdbc</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-test</artifactId> 47 <version>${spring.version}</version> 48 <scope>test</scope> 49 </dependency> 50 51 <!-- mybatis 包 --> 52 <dependency> 53 <groupId>org.mybatis</groupId> 54 <artifactId>mybatis</artifactId> 55 <version>3.2.8</version> 56 </dependency> 57 58 <!--mybatis spring 插件 --> 59 <dependency> 60 <groupId>org.mybatis</groupId> 61 <artifactId>mybatis-spring</artifactId> 62 <version>1.2.2</version> 63 </dependency> 64 65 <!-- mysql連接 --> 66 <dependency> 67 <groupId>mysql</groupId> 68 <artifactId>mysql-connector-java</artifactId> 69 <version>5.1.34</version> 70 </dependency> 71 72 <!-- 數據源 --> 73 <dependency> 74 <groupId>com.alibaba</groupId> 75 <artifactId>druid</artifactId> 76 <version>1.0.12</version> 77 </dependency> 78 79 <dependency> 80 <groupId>org.aspectj</groupId> 81 <artifactId>aspectjweaver</artifactId> 82 <version>1.8.4</version> 83 </dependency> 84 85 <!-- log4j --> 86 <dependency> 87 <groupId>log4j</groupId> 88 <artifactId>log4j</artifactId> 89 <version>1.2.17</version> 90 </dependency> 91 92 <!-- servlet --> 93 <dependency> 94 <groupId>javax.servlet</groupId> 95 <artifactId>servlet-api</artifactId> 96 <version>3.0-alpha-1</version> 97 </dependency> 98 99 <dependency> 100 <groupId>javax.servlet</groupId> 101 <artifactId>jstl</artifactId> 102 <version>1.2</version> 103 </dependency> 104 105 <!-- json --> 106 <dependency> 107 <groupId>org.codehaus.jackson</groupId> 108 <artifactId>jackson-mapper-asl</artifactId> 109 <version>1.9.13</version> 110 </dependency> 111 112 <dependency> 113 <groupId>com.alibaba</groupId> 114 <artifactId>fastjson</artifactId> 115 <version>1.2.3</version> 116 </dependency> 117 118 <dependency> 119 <groupId>com.fasterxml.jackson.core</groupId> 120 <artifactId>jackson-annotations</artifactId> 121 <version>${jackson.version}</version> 122 </dependency> 123 124 <dependency> 125 <groupId>com.fasterxml.jackson.core</groupId> 126 <artifactId>jackson-core</artifactId> 127 <version>${jackson.version}</version> 128 </dependency> 129 130 <dependency> 131 <groupId>com.fasterxml.jackson.core</groupId> 132 <artifactId>jackson-databind</artifactId> 133 <version>${jackson.version}</version> 134 </dependency> 135 <!-- 文件上傳 --> 136 <dependency> 137 <groupId>commons-io</groupId> 138 <artifactId>commons-io</artifactId> 139 <version>2.4</version> 140 </dependency> 141 142 <dependency> 143 <groupId>commons-fileupload</groupId> 144 <artifactId>commons-fileupload</artifactId> 145 <version>1.2.2</version> 146 </dependency> View Codespring 選用的是4.1.4的版本,根據系統需要我們可以選擇自己適合的版本。
2.相關的配置文件:
a)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:context="http://www.springframework.org/schema/context" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 6 http://www.springframework.org/schema/context 7 http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 8 9 <!--引入配置屬性文件 --> 10 <context:property-placeholder location="classpath:config.properties" /> 11 12 <!--自動掃描含有@Service將其注入為bean --> 13 <context:component-scan base-package="com.demo.report.web.service" /> View Codeb)spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.1.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> 11 12 <!-- 自動掃描controller包下的所有類,如果@Controller注入為bean --> 13 <context:component-scan base-package="com.demo.report.web.controller" /> 14 15 <!-- 避免IE執行AJAX時,返回JSON出現下載文件 --> 16 <bean id="mappingJacksonHttpMessageConverter" 17 class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 18 <property name="supportedMediaTypes"> 19 <list> 20 <value>text/html;charset=UTF-8</value> 21 </list> 22 </property> 23 </bean> 24 25 <!-- 啟動Spring MVC的注解功能,完成請求和注解POJO的映射 --> 26 <bean 27 class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 28 <property name="messageConverters"> 29 <list> 30 <!-- json轉換器 --> 31 <ref bean="mappingJacksonHttpMessageConverter" /> 32 </list> 33 </property> 34 </bean> 35 36 <!-- 對模型視圖名稱的解析,即在模型視圖名稱添加前後綴 --> 37 <bean 38 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 39 <property name="viewClass" 40 value="org.springframework.web.servlet.view.JstlView" /> 41 <property name="prefix" value="" /> 42 <property name="suffix" value="" /> 43 </bean> 44 45 <!-- 配置多文件上傳 46 <bean id="multipartResolver" 47 class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 48 <property name="defaultEncoding"> 49 <value>UTF-8</value> 50 </property> 51 <property name="maxUploadSize"> 52 <value>32505856</value> 53 </property> 54 <property name="maxInMemorySize"> 55 <value>4096</value> 56 </property> 57 </bean>--> 58 59 </beans> View Codec)spring-mybatis.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:tx="http://www.springframework.org/schema/tx" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 8 http://www.springframework.org/schema/tx 9 http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 12 "> 13 14 <!-- 配置數據源 使用的是Druid數據源 --> 15 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 16 init-method="init" destroy-method="close"> 17 <property name="url" value="${jdbc.url}" /> 18 <property name="username" value="${jdbc.username}" /> 19 <property name="password" value="${jdbc.password}" /> 20 21 <!-- 初始化連接大小 --> 22 <property name="initialSize" value="0" /> 23 <!-- 連接池最大使用連接數量 --> 24 <property name="maxActive" value="20" /> 25 26 <!-- 連接池最小空閒 --> 27 <property name="minIdle" value="0" /> 28 <!-- 獲取連接最大等待時間 --> 29 <property name="maxWait" value="60000" /> 30 <property name="poolPreparedStatements" value="true" /> 31 <property name="maxPoolPreparedStatementPerConnectionSize" 32 value="33" /> 33 <!-- 用來檢測有效sql --> 34 <property name="validationQuery" value="${validationQuery}" /> 35 <property name="testOnBorrow" value="false" /> 36 <property name="testOnReturn" value="false" /> 37 <property name="testWhileIdle" value="true" /> 38 <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒 --> 39 <property name="timeBetweenEvictionRunsMillis" value="60000" /> 40 <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 --> 41 <property name="minEvictableIdleTimeMillis" value="25200000" /> 42 <!-- 打開removeAbandoned功能 --> 43 <property name="removeAbandoned" value="true" /> 44 <!-- 1800秒,也就是30分鐘 --> 45 <property name="removeAbandonedTimeout" value="1800" /> 46 <!-- 關閉abanded連接時輸出錯誤日志 --> 47 <property name="logAbandoned" value="true" /> 48 <!-- 監控數據庫 --> 49 <property name="filters" value="mergeStat" /> 50 </bean> 51 52 <!-- myBatis文件 --> 53 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 54 <property name="dataSource" ref="dataSource" /> 55 <!-- 自動掃描entity目錄, 省掉Configuration.xml裡的手工配置 --> 56 <property name="mapperLocations" value="classpath:com/demo/report/web/mapper/*.xml" /> 57 </bean> 58 59 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 60 <property name="basePackage" value="com.feidai.report.web.mapper" /> 61 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 62 </bean> 63 64 <!-- 配置事務管理器 --> 65 <bean id="transactionManager" 66 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 67 <property name="dataSource" ref="dataSource" /> 68 </bean> View Coded)web.xml
1 <display-name>springmvc_mybatis_demo</display-name> 2 3 <context-param> 4 <param-name>contextConfigLocation</param-name> 5 <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value> 6 </context-param> 7 8 <filter> 9 <filter-name>encodingFilter</filter-name> 10 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 11 <init-param> 12 <param-name>encoding</param-name> 13 <param-value>utf-8</param-value> 14 </init-param> 15 <init-param> 16 <param-name>forceEncoding</param-name> 17 <param-value>true</param-value> 18 </init-param> 19 </filter> 20 <filter-mapping> 21 <filter-name>encodingFilter</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 <listener> 26 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 27 </listener> 28 29 <!-- 防止spring內存溢出監聽器 --> 30 <listener> 31 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 32 </listener> 33 34 <servlet> 35 <description>spring mvc servlet</description> 36 <servlet-name>rest</servlet-name> 37 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 38 <init-param> 39 <param-name>contextConfigLocation</param-name> 40 <param-value> 41 classpath:spring-mvc.xml 42 </param-value> 43 </init-param> 44 <load-on-startup>1</load-on-startup> 45 </servlet> 46 <servlet-mapping> 47 <servlet-name>rest</servlet-name> 48 <url-pattern>/</url-pattern> 49 </servlet-mapping> 50 51 <servlet> 52 <servlet-name>DruidStatView</servlet-name> 53 <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> 54 </servlet> 55 <servlet-mapping> 56 <servlet-name>DruidStatView</servlet-name> 57 <url-pattern>/druid/*</url-pattern> 58 </servlet-mapping> 59 60 <!-- 配置session超時時間,單位分鐘 --> 61 <session-config> 62 <session-timeout>30</session-timeout> 63 </session-config> 64 65 <welcome-file-list> 66 <welcome-file>index.jsp</welcome-file> 67 </welcome-file-list> View Code使用了druid的數據源,在web中的詳細配置可以參看代碼。