日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

springmvc和mybatis整合关键配置

發(fā)布時(shí)間:2023/12/18 编程问答 37 如意码农
生活随笔 收集整理的這篇文章主要介紹了 springmvc和mybatis整合关键配置 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

springmvc+mybaits的系統(tǒng)架構(gòu):

第一步:整合dao層

mybatis和spring整合,通過(guò)spring管理mapper接口。

使用mapper的掃描器自動(dòng)掃描mapper接口在spring中進(jìn)行注冊(cè)。

第二步:整合service層

通過(guò)spring管理 service接口。

使用配置方式將service接口配置在spring配置文件中。

實(shí)現(xiàn)事務(wù)控制。

第三步:整合springmvc

由于springmvc是spring的模塊,不需要整合。

所需要的jar包:

數(shù)據(jù)庫(kù)驅(qū)動(dòng)包:mysql5.1

mybatis的jar包

mybatis和spring整合包

log4j包

dbcp數(shù)據(jù)庫(kù)連接池包

spring3.2所有jar包

jstl包

mybatis配置文件     sqlMapConfig.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> <!-- 全局setting配置,根據(jù)需要添加 --> <!-- 配置別名 -->
<typeAliases>
<!-- 批量掃描別名 -->
<package name="cn.itcast.ssm.po"/>
</typeAliases> <!-- 配置mapper
由于使用spring和mybatis的整合包進(jìn)行mapper掃描,這里不需要配置了。
必須遵循:mapper.xml和mapper.java文件同名且在一個(gè)目錄
--> <!-- <mappers> </mappers> -->
</configuration>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=

applicationContext-dao.xml





配置:

數(shù)據(jù)源

SqlSessionFactory

mapper掃描器

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加載db.properties文件中的內(nèi)容,db.properties文件中key命名要有一定的特殊規(guī)則 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置數(shù)據(jù)源 ,dbcp --> <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}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數(shù)據(jù)庫(kù)連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<!-- mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描包路徑,如果需要掃描多個(gè)包,中間使用半角逗號(hào)隔開(kāi) -->
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean> </beans>

mybatis mapper接口映射文件 ItemsMapperCustom.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="cn.itcast.ssm.mapper.ItemsMapperCustom" > <!-- 定義商品查詢的sql片段,就是商品查詢條件 -->
<sql id="query_items_where">
<!-- 使用動(dòng)態(tài)sql,通過(guò)if判斷,滿足條件進(jìn)行sql拼接 -->
<!-- 商品查詢條件通過(guò)ItemsQueryVo包裝對(duì)象 中itemsCustom屬性傳遞 -->
<if test="itemsCustom!=null">
<if test="itemsCustom.name!=null and itemsCustom.name!=''">
items.name LIKE '%${itemsCustom.name}%'
</if>
</if> </sql> <!-- 商品列表查詢 -->
<!-- parameterType傳入包裝對(duì)象(包裝了查詢條件)
resultType建議使用擴(kuò)展對(duì)象
-->
<select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo"
resultType="cn.itcast.ssm.po.ItemsCustom">
SELECT items.* FROM items
<where>
<include refid="query_items_where"></include>
</where>
</select> </mapper>

applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 商品管理的service -->
<bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"/>
</beans>

事務(wù)控制(applicationContext-transaction.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 事務(wù)管理器
對(duì)mybatis操作數(shù)據(jù)庫(kù)事務(wù)控制,spring使用jdbc的事務(wù)控制類
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數(shù)據(jù)源
dataSource在applicationContext-dao.xml中配置了
-->
<property name="dataSource" ref="dataSource"/>
</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="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/>
</aop:config> </beans>

springmvc.xml

創(chuàng)建springmvc.xml文件,配置處理器映射器、適配器、視圖解析器

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 可以掃描controller、service、...
這里讓掃描controller,指定controller的包
-->
<context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan> <!--注解映射器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->
<!--注解適配器 -->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> --> <!-- 使用 mvc:annotation-driven代替上邊注解映射器和注解適配器配置
mvc:annotation-driven默認(rèn)加載很多的參數(shù)綁定方法,
比如json轉(zhuǎn)換解析器就默認(rèn)加載了,如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
實(shí)際開(kāi)發(fā)時(shí)使用mvc:annotation-driven
-->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 視圖解析器
解析jsp解析,默認(rèn)使用jstl標(biāo)簽,classpath下的得有jstl的包
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路徑的前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路徑的后綴 -->
<property name="suffix" value=".jsp"/>
</bean> <!-- 自定義參數(shù)綁定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 轉(zhuǎn)換器 -->
<property name="converters">
<list>
<!-- 日期類型轉(zhuǎn)換 -->
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property> </bean>
</beans>

工程的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>springmvc_mybatis1208</display-name> <!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等) 如果不配置contextConfigLocation,默認(rèn)加載的是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 第一種:*.action,訪問(wèn)以.action結(jié)尾 由DispatcherServlet進(jìn)行解析 第二種:/,所以訪問(wèn)的地址都由DispatcherServlet進(jìn)行解析,對(duì)于靜態(tài)文件的解析需要配置不讓DispatcherServlet進(jìn)行解析
使用此種方式可以實(shí)現(xiàn) RESTful風(fēng)格的url 第三種:/*,這樣配置不對(duì),使用這種配置,最終要轉(zhuǎn)發(fā)到一個(gè)jsp頁(yè)面時(shí), 仍然會(huì)由DispatcherServlet解析jsp地址,不能根據(jù)jsp頁(yè)面找到handler,會(huì)報(bào)錯(cuò)。 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping> <!-- post亂碼過(guò)慮器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

總結(jié)

以上是生活随笔為你收集整理的springmvc和mybatis整合关键配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美天天性影院 | 新香蕉视频 | 欧美视频福利 | 天天狠天天干 | 欧美男女视频 | 91蜜桃在线 | 看一级黄色片 | 五月婷婷六月香 | 有机z中国电影免费观看 | 不卡中文字幕在线观看 | 粗暴video蹂躏hd | 日本a视频在线观看 | 制服.丝袜.亚洲.另类.中文 | 日本黄色特级片 | 亚洲天堂男人天堂 | 奇米狠狠去啦 | 久久久国产一区 | 青草视屏 | 色之久久综合 | 国产欧美在线播放 | 国产精品无码av无码 | 91视频免费在线观看 | 美女av免费看 | 国产成人精品一区二三区 | 色呦呦麻豆 | 久综合网 | 国产成人综合网 | 亚洲女人天堂成人av在线 | 亚洲香蕉视频 | 欧美色图另类 | 女人18毛片水真多18精品 | 午夜家庭影院 | 麻豆久久久 | 日日操日日操 | 2020自拍偷拍 | 视频一区二区三 | 色激情网| 成人涩涩软件 | 国产56页 | 少妇视频在线观看 | 国产精品永久久久久久久久久 | 国产视频999 | 日本免费黄色 | 九九热免费在线 | 男男肉耽高h彩漫 | 韩国明星乱淫(高h)小说 | 久久最新 | 亚洲精品一区中文字幕乱码 | av鲁丝一区鲁丝二区鲁丝三区 | 亚洲精品亚洲 | 欧美一区二区激情 | 色人综合 | 狠狠操五月天 | 成人免费高清 | 成人片在线看 | 五月婷婷狠狠 | 国产性av | 亚洲毛片久久 | 亚洲一区二区三区四区在线 | 国产精品乱码久久久 | 免费精品视频在线观看 | 污片免费看 | 欧美aaaaa | 欧美www.| 四虎av在线播放 | 欧美黑人xxxⅹ高潮交 | 免费的黄色av| 国产在线网站 | 喷水了…太爽了高h | 日韩欧美在线精品 | 三级特黄视频 | 亚洲国产视频一区二区三区 | 热久久在线 | 97在线看| 无码人妻久久一区二区三区不卡 | 中文字幕一区二区三区门四区五区 | 青青操国产视频 | 日韩视频在线观看一区二区 | 五月婷婷深爱 | 欧美精品免费在线观看 | 91av在线视频播放 | 91传媒视频在线观看 | 亚洲天堂手机版 | 天天拍夜夜拍 | 神马午夜精品 | 67194在线免费观看 | 日韩在线观看第一页 | 日本少妇喂奶 | 永久免费成人 | 黄网在线免费观看 | 久久国产柳州莫菁门 | 久久久久久视 | 极品少妇av | 91在线观| 亚洲色图 欧美 | 中文字幕一区二区视频 | 性色av蜜臀av| 成人欧美一区二区三区黑人免费 | 成片免费观看视频大全 |