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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC和mybatis的框架

發(fā)布時(shí)間:2025/3/14 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC和mybatis的框架 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.首先以一個(gè)項(xiàng)目做例子,該項(xiàng)目用到的框架即為SpringMVC+mybatis,項(xiàng)目環(huán)境為MyEclipse+sqlserver+tomcat6,項(xiàng)目的地址(項(xiàng)目+數(shù)據(jù)庫備份文件)大家可以上我的百度云下載http://pan.baidu.com/s/1o8qDqxs

2.還原項(xiàng)目怎么還原這里不具體說了,打開項(xiàng)目,找到ApplicationContext.xml,先改變一下數(shù)據(jù)庫的連接,把用戶名和密碼設(shè)成自己的數(shù)據(jù)庫的用戶名和密碼

3.我們現(xiàn)在整體看一下這個(gè)項(xiàng)目的結(jié)構(gòu),首先是整合SpringMVC,Appicationcontext-mvc.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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

?


<!-- 啟用spring mvc 注解-->
<mvc:annotation-driven/>
<!-- 自動(dòng)掃描的包名 ,使Spring支持自動(dòng)檢測(cè)組件,如注解的Controller-->
<context:component-scan base-package="com.flf.controller" />
<!--配置攔截器, 多個(gè)攔截器,順序執(zhí)行 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 匹配的是url路徑 -->
<mvc:mapping path="/**/*.html*"/>
<bean class="com.flf.interceptor.LoginHandlerInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**/*.html*"/>
<bean class="com.flf.interceptor.RightsHandlerInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

<!-- 視圖解析器:定義跳轉(zhuǎn)的文件的前后綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/><!--可為空,方便實(shí)現(xiàn)自已的依據(jù)擴(kuò)展名來選擇視圖解釋類的邏輯 -->
</bean>

<bean id="exceptionResolver" class="com.flf.resolver.MyExceptionResolver"></bean>

</beans>

4.整合mybitas,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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<import resource="ApplicationContext-service.xml"/>
<!-- 配置數(shù)據(jù)源 ,與數(shù)據(jù)庫的連接-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="username" value="sa"/>
<property name="password" value="123456"/>
<property name="url" value="jdbc:sqlserver://localhost:1433; DatabaseName=chongqing"/>
</bean>

<!-- mybatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/config.xml"/>
</bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
</bean>

<!-- 掃描 basePackage下所有的接口,根據(jù)對(duì)應(yīng)的mapper.xml為其生成代理類-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.flf.mapper" />
</bean>

<!-- 數(shù)據(jù)源指向于DataSource -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 增刪該查的操作指向于transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--
aop: 作用表達(dá)于com.flf.service下的所有service,指向于txAdvice的方法
-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.flf.service..*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice" />
</aop:config>

</beans>

?

5web.xml整合SpringMVC和mybitas

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!--
<display-name>wl</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>wl</param-value>
</context-param>
-->
<!-- 初始化過程首先讀的節(jié)點(diǎn) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!-- 配置字符集 -->
<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>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.flf.listener.WebAppContextListener</listener-class>
</listener>
<!-- 初始化 DispatcherServlet時(shí),該框架在 web應(yīng)用程序classpath目錄中尋找spring/ApplicationContext-mvc.xml的文件,
并在那里定義相關(guān)的Beans,重寫在全局中定義的任何Beans -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<!-- 所有以.html的請(qǐng)求,都會(huì)被DispatcherServlet處理 -->
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

?

6.當(dāng)我們新增一個(gè)對(duì)象的時(shí)候,先要在Applicationcontext.service中注入該對(duì)象的service,然后再在mybatis文件夾中增加該對(duì)象對(duì)應(yīng)的xml文件

本人微信號(hào)xjt199561,有什么技術(shù)上的交流歡迎添加

?

轉(zhuǎn)載于:https://www.cnblogs.com/xujiating/p/6287116.html

總結(jié)

以上是生活随笔為你收集整理的SpringMVC和mybatis的框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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