标签:
对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。
第四篇:web.xml,ApplicationContext.xml,xxx-servlet.xml(spring mvc的配置文件),mybatis-config.xml以及sql映射语句xxxxMapper.xml的配置。=================================================================
呼,好长的题目,差点一口气没上来,憋死。
先给看一下目录结构:
[img]
http://dl.iteye.com/upload/attachment/453071/2ed93902-e777-309e-bbe5-58225bcac04b.png
[/img]
同事帮着分的,个人感觉还不错,不一定科学,全凭个人喜好。
一web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
- http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <display-name>qdcl-mart</display-name>
-
-
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/spring/applicationContext.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
-
- <listener>
- <listener-class>
- org.springframework.web.util.IntrospectorCleanupListener
- </listener-class>
- </listener>
-
-
- <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>
- </filter>
-
- <filter-mapping>
- <filter-name>encodingFilter</filter-name>
- <url-pattern>/</url-pattern>
- </filter-mapping>
-
-
- <servlet>
- <servlet-name>mart</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/spring-mvc/mart-servlet.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>mart</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
关于classpath*:/其实无所谓,至少在我的目录结构里无所谓,用classpath:/或者不写都可以找到的。既然我用这个跑起来了,其他的我就不写了。
2:applicationContext.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
- >
-
-
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath:spring/jdbc.properties</value>
- </list>
- </property>
- </bean>
-
-
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${jdbc.driver}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- </bean>
-
- <bean name="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="delete*" propagation="REQUIRED" />
- <tx:method name="update*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
-
- <aop:config>
- <aop:pointcut id="serviceOperation" expression="execution(* com.qdcl.mart.business.service.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
- </aop:config>
-
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.qdcl.mart.business.persistence" />
- </bean>
-
- <context:annotation-config />
-
- <context:spring-configured/>
-
- <context:component-scan base-package="com.qdcl.mart.business.service" />
- </beans>
jdbc.properties
- jdbc.url=jdbc:mysql://localhost:3306/qdclmart?useUnicode=true&characterEncoding=UTF-8
- jdbc.driver=com.mysql.jdbc.Driver
- jdbc.username=root
- jdbc.password=root
里面的dataSource就不说了,简单说一下
①、sqlSessionFactory:用来进行数据库操作的类,而且好玩的是虽然你配了org.mybatis.spring.SqlSessionFactoryBean,但是测试的时候你用getBean("sqlSessionFactory")得到的确是mybatis的这个类: org.apache.ibatis.session.SqlSessionFactory,原因是org.mybatis.spring.SqlSessionFactoryBean.getObject返回的类型给改变了。具体为何我也不知道。大家可以测试一下,强转就抛错误了。
②、
- <!-- scan mappers and let them be autowired -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.qdcl.mart.business.persistence" />
- </bean>
这段配置是我比较喜欢滴,因为他自动扫描了所有的XxxxMapper.java,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。详细的大家可以到google mybatis项目网址看一下那个文档,中文滴。
③、
- <!-- 激活annotation功能 -->
- <context:annotation-config />
- <!-- 激活annotation功能 -->
- <context:spring-configured/>
- <!-- 扫描指定package下所有带有如@controller,@services,@resource,并把所注释的注册为Spring Beans -->
- <context:component-scan base-package="com.qdcl.mart.business.service" />
以上3句不解释了啊,就是注解式管理bean,有的大神不推荐,我个人还是喜欢,因为对大量的配置文件有些抵触,错了找起来比较麻烦。懒嘛。 context:component-scan尽管他是递归方式扫描,我还是建议大家base-package具体一些,你懂得。
3:mart-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:mvc="http://www.springframework.org/schema/mvc"
- xmlns:context="http://www.springframework.org/schema/context"
- 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"
- >
-
- <mvc:annotation-driven />
-
- <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/view/" />
- <property name="suffix" value=".jsp" />
- </bean>
- <context:component-scan base-package="com.qdcl.mart.business.web" />
-
- </beans>
只说一句,一开始没加<context:component-scan base-package="com.qdcl.mart.business.web" />这一句,结局就是我在Controller里面写RequestMapping(xxxx)的时候是找不到的,我弄了一上午,并且发誓再找不到原因就跳楼的,还好吃饭前找出原因。要不要么是死人,要么是女人了。呵呵。
4、mybatis-config.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>
-
- <settings>
-
- <setting name="lazyLoadingEnabled" value="false" />
- </settings>
-
- <typeAliases>
- <typeAlias alias="production" type="com.qdcl.mart.business.domain.Production" />
- </typeAliases>
-
- <mappers>
- <mapper resource="mapper/ProductionMapper.xml" />
- </mappers>
- </configuration>
也是一句,就是最后这个mappers的resource配置,当时手贱,写成/mapper/ProductionMapper.xml,那叫一个悲剧,一直没找到,这就是不仔细看文档的下场。
5、XxxMapper.xml(ProductionMapper.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.qdcl.mart.business.persistence.ProductionMapper">
-
-
- <select id="selectProductionByName" parameterType="java.lang.String" resultType="production">
-
- SELECT productionid,productionname,price,detail FROM production
- where productionname = #{productionname}
- </select>
-
-
- <insert id="insertProduction" parameterType="production">
- insert into production values (#{productionid},#{productionname},#{price},#{detail});
- </insert>
-
- <update id="updateProduction" parameterType="production">
- update production set productionname = #{productionname},
- price = #{price},
- detail = #{detail}
- </update>
-
- <delete id="deleteProduction" parameterType="java.lang.String">
- delete from production where productionid = #{value}
- </delete>
- </mapper>
还是一句,这里的sql id一定要和XxxMapper.xml里面的一致起来,废话。
整理的比较粗略,将代码分享给大家,【源码地址获取】
Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】
标签:
原文地址:http://www.cnblogs.com/zhenjava/p/4716815.html