你的位置:
首页
>
软件开发 >
ASP.net > Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】
Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】
发布时间:2015-08-10 02:00:16
对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。 第四 ...
对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。
- <?version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" "http://java.sun.com/
- "http://www.w3.org/2001/
- xsi:schemaLocation="http://java.sun.com/
- http://java.sun.com/>
-
- <display-name>qdcl-mart</display-name>
-
- <!-- 载入spring上下文 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:/spring/applicationContext.</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
-
- <!-- Spring 刷新Introspector防止内存泄露 -->
- <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>
-
- <!-- 载入spring上下文 -->
- <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-get='_blank'>mvc/mart-servlet.</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>
- <?version="1.0" encoding="UTF-8"?>
- <beans "http://www.springframework.org/schema/beans"
- "http://www.w3.org/2001/
- "http://www.springframework.org/schema/aop"
- "http://www.springframework.org/schema/tx"
- "http://www.springframework.org/schema/jdbc"
- "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>
-
- <!-- Mybatis's sqlSessionFactory config -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"></property>
- <property name="configLocation" value="classpath:mybatis-config./>
- </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>
-
- <!-- scan mappers and let them be autowired -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.qdcl.mart.business.persistence" />
- </bean>
- <!-- 激活annotation功能 -->
- <context:annotation-config />
- <!-- 激活annotation功能 -->
- <context:spring-configured/>
- <!-- 扫描指定package下所有带有如@controller,@services,@resource,并把所注释的注册为Spring Beans -->
- <context:component-scan base-package="com.qdcl.mart.business.service" />
- </beans>
- <?version="1.0" encoding="UTF-8" ?>
- <beans "http://www.springframework.org/schema/beans"
- "http://www.w3.org/2001/
- "http://www.springframework.org/schema/p"
- "http://www.springframework.org/schema/mvc"
- "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"
- >
- <!-- 只能用于springMVC,用于配置springMVC的注解驱动 -->
- <mvc:annotation-driven />
- <!-- Spring mvc视图解析器 -->
- <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>
- <?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>
- <!-- changes from the defaults -->
- <setting name="lazyLoadingEnabled" value="false" />
- </settings>
- <!-- 别名定义 -->
- <typeAliases>
- <typeAlias alias="production" type="com.qdcl.mart.business.domain.Production" />
- </typeAliases>
- <!-- SQL映射文件 -->
- <mappers>
- <mapper resource="mapper/ProductionMapper./>
- </mappers>
- </configuration>
也是一句,就是最后这个mappers的resource配置,当时手贱,写成/mapper/ProductionMapper.5、XxxMapper.
- <?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">
- <!-- 检索sql文 -->
- 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>
原标题:Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。