使用mapper接口的方式整合mybatis
1:创建UserMapper.java的接口
2:在资源文件中创建UserMapper.xml对应上面接口的映射文件,(文件名要和接口名相同)
3:在配置文件SqlMapConfig.xml中引用UserMapper.xml
4:配置applicationContext的文件
5:web.xml中加载applicationContext文件
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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/application*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/mvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>applicationContext-dao.xml的配置:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <context:property-placeholder location="classpath:db.properties" /> <!-- 閰嶇疆鏁版嵁婧�--> <!--<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">--> <!--<property name="driverClass" value="${db.driver}" />--> <!--<property name="jdbcUrl" value="${db.url}" />--> <!--<property name="user" value="${db.username}" />--> <!--<property name="password" value="${db.password}" />--> <!--<property name="maxPoolSize" value="10" />--> <!--<property name="minPoolSize" value="5" />--> <!--</bean>--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis" /> <property name="username" value="root" /> <property name="password" value="root" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:config/SqlMapConfig.xml"/> <property name="dataSource" ref="dataSource"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.feitian.web.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
applicationContext-service.xml的配置
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 閰嶇疆service鐨刡ean锛屼娇鐢ㄦ壂鎻忓櫒 --> <context:component-scan base-package="com.taotao.web"></context:component-scan> <!-- 閰嶇疆浜嬪姟绠$悊鍣�--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 鏁版嵁婧�--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 閰嶇疆閫氱煡 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 閰嶇疆浜嬪姟鐨勪紶鎾壒鎬�--> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="find*" read-only="true" /> <tx:method name="query*" read-only="true" /> <tx:method name="select*" read-only="true" /> <tx:method name="get*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 閰嶇疆aop --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.feitian.web.service.*.*(..))" /> </aop:config> </beans>SqlMapConfig.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.feitian.web.po.User" alias="User"/> </typeAliases> <mappers> <mapper resource="config/sqlMap/UserMapper.xml"/> </mappers> </configuration>UserMapper.xml配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.feitian.web.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="java.lang.String"> SELECT username FROM tb_user WHERE id = #{id} </select> </mapper>UserMapper.java接口:
package com.feitian.web.mapper; public interface UserMapper { String findUserById( int id); }mvc_servlet.xml配置;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven /> <context:component-scan base-package="com.feitian.web.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>以上配置可以正确运行
在配置过程中出现了下面的错误:
Mapped Statements collection does not contain value for com.feitianweb.mappe
经排查是userMapper.xml配置文件没有找到,除此之外还有几个注意点也会引起上面的错误:
1、mapper.xml中没有加入namespace
2、mapper.xml中的方法和接口mapper的方法不对应
3、mapper.xml没有加入到SqlMapConfig.xml中(即总的配置文件),例外:配置了mapper文件的包路径的除外
(或者:由于包的层级太多,跟SqlMapConfig.xml配置的内容不一致,导致无法识别)
4、mapper.xml文件名和所写的mapper名称不相同。