星空网 > 软件开发 > ASP.net

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

对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。 
第四篇:web.================================================================= 
呼,好长的题目,差点一口气没上来,憋死。 
先给看一下目录结构: 
[img] 
http://dl.iteye.com/upload/attachment/453071/2ed93902-e777-309e-bbe5-58225bcac04b.png 
[/img] 
同事帮着分的,个人感觉还不错,不一定科学,全凭个人喜好。 
一web.

  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-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>  


关于classpath*:/其实无所谓,至少在我的目录结构里无所谓,用classpath:/或者不写都可以找到的。既然我用这个跑起来了,其他的我就不写了。 
2:applicationContext.

  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>   


jdbc.properties 

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

  1. jdbc.url=jdbc:mysql://localhost:3306/qdclmart?useUnicode=true&amp;characterEncoding=UTF-8  
  2. jdbc.driver=com.mysql.jdbc.Driver  
  3. jdbc.username=root  
  4. jdbc.password=root  


里面的dataSource就不说了,简单说一下 
①、sqlSessionFactory:用来进行数据库操作的类,而且好玩的是虽然你配了org.mybatis.spring.SqlSessionFactoryBean,但是测试的时候你用getBean("sqlSessionFactory")得到的确是mybatis的这个类: org.apache.ibatis.session.SqlSessionFactory,原因是org.mybatis.spring.SqlSessionFactoryBean.getObject返回的类型给改变了。具体为何我也不知道。大家可以测试一下,强转就抛错误了。 
②、

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

  1. <!-- scan  mappers and let them be autowired -->  
  2.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">         
  3.         <property name="basePackage" value="com.qdcl.mart.business.persistence" />  
  4.     </bean>  


这段配置是我比较喜欢滴,因为他自动扫描了所有的XxxxMapper.java,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。详细的大家可以到google mybatis项目网址看一下那个文档,中文滴。 
③、

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

  1. <!-- 激活annotation功能 -->  
  2.     <context:annotation-config />  
  3.     <!-- 激活annotation功能 -->  
  4.     <context:spring-configured/>  
  5.     <!-- 扫描指定package下所有带有如@controller,@services,@resource,并把所注释的注册为Spring Beans -->  
  6.     <context:component-scan base-package="com.qdcl.mart.business.service" />  


以上3句不解释了啊,就是注解式管理bean,有的大神不推荐,我个人还是喜欢,因为对大量的配置文件有些抵触,错了找起来比较麻烦。懒嘛。 context:component-scan尽管他是递归方式扫描,我还是建议大家base-package具体一些,你懂得。 
3:mart-servlet.

  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>  


只说一句,一开始没加<context:component-scan base-package="com.qdcl.mart.business.web" />这一句,结局就是我在Controller里面写RequestMapping(xxxx)的时候是找不到的,我弄了一上午,并且发誓再找不到原因就跳楼的,还好吃饭前找出原因。要不要么是死人,要么是女人了。呵呵。 
4、mybatis-config.

  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>  


还是一句,这里的sql id一定要和XxxMapper.

 整理的比较粗略,将代码分享给大家,【源码地址获取】




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

关键词:Spring

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

绝了!Temu击败亚马逊Shein冲到榜首!还要收购跨境通旗舰大站?:https://www.ikjzd.com/articles/1598521719183052802
亚马逊卖家店铺没流量怎么办?这几个方法可以试试!:https://www.ikjzd.com/articles/1598578596401774594
出口尼日利亚要注意这些问题!:https://www.ikjzd.com/articles/1598578605139890177
Amazon、速卖通、lazada、如何实现安全测评补单,有哪些技巧?:https://www.ikjzd.com/articles/1598583636178178050
年售5亿美金!解决男性“难言之隐”的护理品牌Hims在北美杀疯了:https://www.ikjzd.com/articles/1598586922562162689
“数字化混展”再下一城,阿里国际站组团800家中小外贸企业参展德国慕尼黑ISPO展:https://www.ikjzd.com/articles/1598596488780664833
深圳大运中心周边酒店住宿推荐:https://www.vstour.cn/a/336169.html
八台山有哪些景点要收门票 八台山风景名胜区门票:https://www.vstour.cn/a/336170.html
相关文章
我的浏览记录
跨境电商服务平台 | 深圳旅行社 | 东南亚物流