springmvc与mybatis集成配置实例详解
简单之美,springmvc,mybatis就是一个很好的简单集成方案,能够满足一般的项目需求。闲暇时间把项目配置文件共享出来,供大家参看:
1.首先我们来看下依赖的pom:
<!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--mybatis包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!--mybatisspring插件--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!--mysql连接--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <!--数据源--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--json--> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency>
spring选用的是4.1.4的版本,根据系统需要我们可以选择自己适合的版本。
2.相关的配置文件:
a)spring.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!--引入配置属性文件--> <context:property-placeholderlocation="classpath:config.properties"/> <!--自动扫描含有@Service将其注入为bean--> <context:component-scanbase-package="com.demo.report.web.service"/>
b)spring-mvc.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!--自动扫描controller包下的所有类,如果@Controller注入为bean--> <context:component-scanbase-package="com.demo.report.web.controller"/> <!--避免IE执行AJAX时,返回JSON出现下载文件--> <beanid="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <propertyname="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!--启动SpringMVC的注解功能,完成请求和注解POJO的映射--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <propertyname="messageConverters"> <list> <!--json转换器--> <refbean="mappingJacksonHttpMessageConverter"/> </list> </property> </bean> <!--对模型视图名称的解析,即在模型视图名称添加前后缀--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <propertyname="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <propertyname="prefix"value=""/> <propertyname="suffix"value=""/> </bean> <!--配置多文件上传 <beanid="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <propertyname="defaultEncoding"> <value>UTF-8</value> </property> <propertyname="maxUploadSize"> <value>32505856</value> </property> <propertyname="maxInMemorySize"> <value>4096</value> </property> </bean>--> </beans>
c)spring-mybatis.xml
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd "> <!--配置数据源使用的是Druid数据源--> <beanname="dataSource"class="com.alibaba.druid.pool.DruidDataSource" init-method="init"destroy-method="close"> <propertyname="url"value="${jdbc.url}"/> <propertyname="username"value="${jdbc.username}"/> <propertyname="password"value="${jdbc.password}"/> <!--初始化连接大小--> <propertyname="initialSize"value="0"/> <!--连接池最大使用连接数量--> <propertyname="maxActive"value="20"/> <!--连接池最小空闲--> <propertyname="minIdle"value="0"/> <!--获取连接最大等待时间--> <propertyname="maxWait"value="60000"/> <propertyname="poolPreparedStatements"value="true"/> <propertyname="maxPoolPreparedStatementPerConnectionSize" value="33"/> <!--用来检测有效sql--> <propertyname="validationQuery"value="${validationQuery}"/> <propertyname="testOnBorrow"value="false"/> <propertyname="testOnReturn"value="false"/> <propertyname="testWhileIdle"value="true"/> <!--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒--> <propertyname="timeBetweenEvictionRunsMillis"value="60000"/> <!--配置一个连接在池中最小生存的时间,单位是毫秒--> <propertyname="minEvictableIdleTimeMillis"value="25200000"/> <!--打开removeAbandoned功能--> <propertyname="removeAbandoned"value="true"/> <!--1800秒,也就是30分钟--> <propertyname="removeAbandonedTimeout"value="1800"/> <!--关闭abanded连接时输出错误日志--> <propertyname="logAbandoned"value="true"/> <!--监控数据库--> <propertyname="filters"value="mergeStat"/> </bean> <!--myBatis文件--> <beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <!--自动扫描entity目录,省掉Configuration.xml里的手工配置--> <propertyname="mapperLocations"value="classpath:com/demo/report/web/mapper/*.xml"/> </bean> <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"> <propertyname="basePackage"value="com.feidai.report.web.mapper"/> <propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/> </bean> <!--配置事务管理器--> <beanid="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <propertyname="dataSource"ref="dataSource"/> </bean>
d)web.xml
<display-name>springmvc_mybatis_demo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--防止spring内存溢出监听器--> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <servlet> <description>springmvcservlet</description> <servlet-name>rest</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:spring-mvc.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>DruidStatView</servlet-name> <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping> <!--配置session超时时间,单位分钟--> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
使用了druid的数据源,在web中的详细配置可以参看代码。
以上所述是小编给大家介绍的springmvc与mybatis集成配置实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对毛票票网站的支持!