易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
首页
/
操作系统
/
Linux
/
Spring3 整合 Mybatis3
这两天,项目需要使用spring+ibatis,于是去网上下载了,结果发现和之前我用的版本变化不小,整了两天才将功能实现,不敢怠慢,赶紧写份博客记录一下。首先是依赖的库:接着是web.xml的配置,这里,我使用的是Spring3 MVC
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">
<display-name>Log</display-name>
<welcome-file-list>
<welcome-file>welcome.html</welcome-file>
</welcome-file-list>
<!--日志配置文件的载入 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!--Spring配置文件的载入 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring字符集过滤 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter </filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Server启动时加载的应用 -->
<!-- Spring过滤器 -->
<servlet>
<servlet-name>dispatchServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatchServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
然后是spring的servlet配置servlet-config.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 自动扫描bean,把作了注??的类转换为bean -->
<context:component-scanbase-package="com.log.bean.dao, com.log.report.controller"/>
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/>
<!-- 支持JSON数据格式 -->
<beanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<propertyname="messageConverters">
<list>
<beanclass="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<!-- 声明viewResolver -->
<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix"value="/"/>
<propertyname="suffix"value=".html"/>
</bean>
</beans>
然后是Spring的Bean的配置文件applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="maxActive"value="20"/>
<propertyname="maxIdle"value="20"/>
<propertyname="maxWait"value="3000"/>
<propertyname="testWhileIdle"value="true"/>
<propertyname="timeBetweenEvictionRunsMillis"value="3600000"/>
<propertyname="validationQuery"value="select 1"/>
<propertyname="removeAbandoned"value="true"/>
<propertyname="removeAbandonedTimeout"value="1"/>
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://localhost:53306/search"/>
<propertyname="username"value="root"/>
<propertyname="password"value="8bewp2rJHfRGrl9VTQmc"/>
</bean>
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="configLocation"value="classpath:ibatis-config.xml"/>
<propertyname="dataSource"ref="dataSource"/>
<!-- mapper和resultmap配置路径 -->
<propertyname="mapperLocations">
<list>
<value>classpath:com/log/bean/mapper/*.xml</value>
</list>
</property>
</bean>
<!-- 通过扫描的模式,扫描目录在com/log/bean/mapper目录下,所有的mapper都继承
SQLMapper接口的接口, 这样一个bean就可以了 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.log.bean.mapper"/>
<propertyname="markerInterface"value="com.log.bean.mapper.SQLMapper"/>
</bean>
<beanid="sqlSession"class="org.mybatis.spring.SqlSessionTemplate">
<constructor-argindex="0"ref="sqlSessionFactory"/>
</bean>
<!--事务配置-->
<tx:annotation-driventransaction-manager="transactionManager"/>
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"autowire="byName"/>
</beans>
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图