设为首页 收藏本站
查看: 1001|回复: 0

[经验分享] Maven + SpringMVC + Mybatis【绝非原创,单纯整理】【四】

[复制链接]

尚未签到

发表于 2016-11-27 08:53:59 | 显示全部楼层 |阅读模式
对于我这样的菜鸟来说,这一部分是我最痛苦的部分了,各种错误啊。而且这里涉及具体的配置,网上牛人各种方式都有,我也在配置的过程中去翻翻mybatis的源代码,尽管没看懂,但是还是比较装2的跟人家炫耀:俺也看过框架源代码,就是没看懂(当然,后面这句没说出口,他们替我说了)。
第四篇:web.xml,ApplicationContext.xml,xxx-servlet.xml(spring mvc的配置文件),mybatis-config.xml以及sql映射语句xxxxMapper.xml的配置。=================================================================
呼,好长的题目,差点一口气没上来,憋死。
先给看一下目录结构:
DSC0000.png
同事帮着分的,个人感觉还不错,不一定科学,全凭个人喜好。
一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>
<!-- 载入spring上下文  -->
<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>
<!-- 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-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>
<!-- 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.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>
<!-- 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>

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/qdclmart?useUnicode=true&amp;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"
>
<!-- 只能用于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>

只说一句,一开始没加<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>
<!-- 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.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">
<!-- 检索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>

还是一句,这里的sql id一定要和XxxMapper.xml里面的一致起来,废话。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305999-1-1.html 上篇帖子: mybatis 通用分页插件和批量操作插件 下篇帖子: Spring3+Mybatis在Maven环境下的整合
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表