你的位置:首页 > 软件开发 > ASP.net > Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】

Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】

发布时间:2015-08-10 02:00:16
对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。 第四 ...

对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。 

  1. <?version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4" "http://java.sun.com/
  3.     "http://www.w3.org/2001/
  4.     xsi:schemaLocation="http://java.sun.com/
  5.            http://java.sun.com/>  
  6.              
  7.     <display-name>qdcl-mart</display-name>  
  8.       
  9.     <!-- 载入spring上下文  -->  
  10.     <context-param>  
  11.         <param-name>contextConfigLocation</param-name>  
  12.         <param-value>classpath*:/spring/applicationContext.</param-value>  
  13.     </context-param>  
  14.     <listener>  
  15.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  16.     </listener>  
  17.       
  18.     <!-- Spring 刷新Introspector防止内存泄露 -->  
  19.     <listener>  
  20.         <listener-class>  
  21.             org.springframework.web.util.IntrospectorCleanupListener  
  22.         </listener-class>  
  23.     </listener>  
  24.       
  25.     <!-- 字符编码过滤器 -->  
  26.     <filter>  
  27.         <filter-name>encodingFilter</filter-name>  
  28.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  29.         <init-param>  
  30.             <param-name>encoding</param-name>  
  31.             <param-value>UTF-8</param-value>  
  32.         </init-param>  
  33.     </filter>  
  34.   
  35.     <filter-mapping>  
  36.         <filter-name>encodingFilter</filter-name>  
  37.         <url-pattern>/</url-pattern>  
  38.     </filter-mapping>  
  39.       
  40.     <!-- 载入spring上下文  -->  
  41.     <servlet>  
  42.         <servlet-name>mart</servlet-name>  
  43.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  44.         <init-param>  
  45.             <param-name>contextConfigLocation</param-name>  
  46.             <param-value>classpath*:/spring-get='_blank'>mvc/mart-servlet.</param-value>  
  47.         </init-param>  
  48.         <load-on-startup>1</load-on-startup>  
  49.     </servlet>  
  50.     <servlet-mapping>  
  51.         <servlet-name>mart</servlet-name>  
  52.         <url-pattern>/</url-pattern>  
  53.     </servlet-mapping>  
  54.       
  55.     <!-- 欢迎页面 -->  
  56.     <welcome-file-list>  
  57.         <welcome-file>index.jsp</welcome-file>  
  58.     </welcome-file-list>  
  59. </web-app>  
  1. <?version="1.0" encoding="UTF-8"?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.w3.org/2001/
  4.     "http://www.springframework.org/schema/aop"  
  5.     "http://www.springframework.org/schema/tx"   
  6.     "http://www.springframework.org/schema/jdbc"  
  7.     "http://www.springframework.org/schema/context"  
  8.     xsi:schemaLocation="            
  9.       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd            
  10.       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            
  11.       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd            
  12.       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            
  13.       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  14.      >  
  15.         
  16.     <!-- 属性文件读入 -->  
  17.     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  18.         <property name="locations">  
  19.             <list>  
  20.                 <value>classpath:spring/jdbc.properties</value>  
  21.             </list>  
  22.         </property>  
  23.     </bean>  
  24.         
  25.     <!-- 数据库连接池 -->  
  26.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  27.         <property name="driverClassName" value="${jdbc.driver}" />  
  28.         <property name="url" value="${jdbc.url}" />  
  29.         <property name="username" value="${jdbc.username}" />  
  30.         <property name="password" value="${jdbc.password}" />  
  31.     </bean>  
  32.       
  33.     <!-- Mybatis's sqlSessionFactory config -->  
  34.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  35.         <property name="dataSource" ref="dataSource"></property>  
  36.         <property name="configLocation" value="classpath:mybatis-config./>          
  37.     </bean>  
  38.       
  39.     <bean name="transactionManager"  
  40.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  41.         <property name="dataSource" ref="dataSource"></property>  
  42.     </bean>  
  43.       
  44.     <!-- 启用事务 -->  
  45.     <tx:advice id="txAdvice" transaction-manager="transactionManager">  
  46.         <tx:attributes>  
  47.             <tx:method name="save*" propagation="REQUIRED" />  
  48.             <tx:method name="delete*" propagation="REQUIRED" />  
  49.             <tx:method name="update*" propagation="REQUIRED" />  
  50.         </tx:attributes>  
  51.     </tx:advice>  
  52.   
  53.     <aop:config>  
  54.         <aop:pointcut id="serviceOperation"  expression="execution(* com.qdcl.mart.business.service.*.*(..))" />  
  55.         <aop:advisor advice-ref="txAdvice"   pointcut-ref="serviceOperation" />  
  56.     </aop:config>  
  57.       
  58.     <!-- scan  mappers and let them be autowired -->  
  59.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">         
  60.         <property name="basePackage" value="com.qdcl.mart.business.persistence" />  
  61.     </bean>  
  62.     <!-- 激活annotation功能 -->  
  63.     <context:annotation-config />  
  64.     <!-- 激活annotation功能 -->  
  65.     <context:spring-configured/>  
  66.     <!-- 扫描指定package下所有带有如@controller,@services,@resource,并把所注释的注册为Spring Beans -->  
  67.     <context:component-scan base-package="com.qdcl.mart.business.service" />  
  68. </beans>   
  1. <?version="1.0" encoding="UTF-8" ?>  
  2. <beans "http://www.springframework.org/schema/beans"  
  3.     "http://www.w3.org/2001/
  4.     "http://www.springframework.org/schema/p"  
  5.     "http://www.springframework.org/schema/mvc"  
  6.     "http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  9.                         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"  
  10.                         >  
  11.     <!-- 只能用于springMVC,用于配置springMVC的注解驱动 -->  
  12.     <mvc:annotation-driven />  
  13.     <!-- Spring mvc视图解析器  -->                       
  14.     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  15.         <property name="prefix" value="/WEB-INF/view/" />  
  16.         <property name="suffix" value=".jsp" />  
  17.     </bean>     
  18.     <context:component-scan base-package="com.qdcl.mart.business.web" />  
  19.   
  20. </beans>  
  1. <?version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  3. <configuration>  
  4.     <!-- 基础设置 -->  
  5.     <settings>  
  6.         <!-- changes from the defaults -->  
  7.         <setting name="lazyLoadingEnabled" value="false" />  
  8.     </settings>  
  9.     <!-- 别名定义 -->  
  10.     <typeAliases>  
  11.         <typeAlias alias="production" type="com.qdcl.mart.business.domain.Production" />  
  12.     </typeAliases>  
  13.     <!-- SQL映射文件 -->  
  14.     <mappers>  
  15.         <mapper resource="mapper/ProductionMapper./>  
  16.     </mappers>  
  17. </configuration>  
也是一句,就是最后这个mappers的resource配置,当时手贱,写成/mapper/ProductionMapper.5、XxxMapper.
  1. <?version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4.   
  5. <mapper namespace="com.qdcl.mart.business.persistence.ProductionMapper">  
  6.   
  7.     <!-- 检索 -->  
  8.     <select id="selectProductionByName" parameterType="java.lang.String" resultType="production">  
  9.         <!-- 检索sql文 -->  
  10.         SELECT productionid,productionname,price,detail FROM production  
  11.         where productionname = #{productionname}  
  12.     </select>  
  13.       
  14.     <!-- 插入 -->  
  15.     <insert id="insertProduction" parameterType="production">  
  16.         insert into production  values (#{productionid},#{productionname},#{price},#{detail});  
  17.     </insert>  
  18.     <!-- 更新 -->  
  19.     <update id="updateProduction" parameterType="production">   
  20.         update production set productionname = #{productionname},  
  21.                               price = #{price},  
  22.                               detail = #{detail}      
  23.     </update>  
  24.     <!-- 删除 -->  
  25.     <delete id="deleteProduction" parameterType="java.lang.String">  
  26.         delete from production where productionid = #{value}  
  27.     </delete>  
  28. </mapper>  

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录