首页 > 代码库 > springmvc+mybatis集成配置
springmvc+mybatis集成配置
简单之美,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>
spring 选用的是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" />
b)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 <bean27 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 <bean38 class="org.springframework.web.servlet.view.InternalResourceViewResolver">39 <property name="viewClass"40 value="http://www.mamicode.com/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>
c)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.xsd10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop-4.1.xsd12 ">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="http://www.mamicode.com/${jdbc.url}" />18 <property name="username" value="http://www.mamicode.com/${jdbc.username}" />19 <property name="password" value="http://www.mamicode.com/${jdbc.password}" />20 21 <!-- 初始化连接大小 -->22 <property name="initialSize" value="http://www.mamicode.com/0" />23 <!-- 连接池最大使用连接数量 -->24 <property name="maxActive" value="http://www.mamicode.com/20" />25 26 <!-- 连接池最小空闲 -->27 <property name="minIdle" value="http://www.mamicode.com/0" />28 <!-- 获取连接最大等待时间 -->29 <property name="maxWait" value="http://www.mamicode.com/60000" />30 <property name="poolPreparedStatements" value="http://www.mamicode.com/true" />31 <property name="maxPoolPreparedStatementPerConnectionSize"32 value="http://www.mamicode.com/33" />33 <!-- 用来检测有效sql -->34 <property name="validationQuery" value="http://www.mamicode.com/${validationQuery}" />35 <property name="testOnBorrow" value="http://www.mamicode.com/false" />36 <property name="testOnReturn" value="http://www.mamicode.com/false" />37 <property name="testWhileIdle" value="http://www.mamicode.com/true" />38 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->39 <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/60000" />40 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->41 <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/25200000" />42 <!-- 打开removeAbandoned功能 -->43 <property name="removeAbandoned" value="http://www.mamicode.com/true" />44 <!-- 1800秒,也就是30分钟 -->45 <property name="removeAbandonedTimeout" value="http://www.mamicode.com/1800" />46 <!-- 关闭abanded连接时输出错误日志 -->47 <property name="logAbandoned" value="http://www.mamicode.com/true" />48 <!-- 监控数据库 -->49 <property name="filters" value="http://www.mamicode.com/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="http://www.mamicode.com/classpath:com/demo/report/web/mapper/*.xml" />57 </bean>58 59 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">60 <property name="basePackage" value="http://www.mamicode.com/com.feidai.report.web.mapper" />61 <property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory" />62 </bean>63 64 <!-- 配置事务管理器 -->65 <bean id="transactionManager"66 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">67 <property name="dataSource" ref="dataSource" />68 </bean>
d)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.xml42 </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>
使用了druid的数据源,在web中的详细配置可以参看代码。
springmvc+mybatis集成配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。