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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc整合mybatis之准备阶段与文件配置

發布時間:2024/8/23 c/c++ 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc整合mybatis之准备阶段与文件配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章出處:課程資料
web.xml等配置文件的解釋:打開博客

為了更好的學習 springmvc和mybatis整合開發的方法,需要將springmvc和mybatis進行整合。

整合目標:控制層采用springmvc、持久層使用mybatis實現。

步驟詳解:

創建數據庫表

需要的jar包

  • spring(包括springmvc)
  • mybatis
  • mybatis-spring整合包
  • 數據庫驅動
  • 第三方連接池。
  • 整合思路

    Dao層:

    1、SqlMapConfig.xml,空文件即可,但是需要文件頭。
    2、applicationContext-dao.xml

    數據庫連接池

    b) SqlSessionFactory對象,需要spring和mybatis整合包下的。
    c) 配置mapper文件掃描器。

    Service層:

    1、applicationContext-service.xml包掃描器,掃描@service注解的類。
    2、applicationContext-trans.xml配置事務。

    Controller層:

    1、Springmvc.xml
    a) 包掃描器,掃描@Controller注解的類。
    b) 配置注解驅動
    c) 配置視圖解析器

    Web.xml文件:

    1、配置spring
    2、配置前端控制器。

    創建工程

    創建動態web工程springmvc-web(選2.5可以自動生成web.xml)

    加入jar包

    復制jar包到/WEB-INF/lib中
    工程自動加載jar包

    加入配置文件

    創建資源文件夾config
    在其下創建mybatis和spring文件夾,用來存放配置文件,如下圖:

    sqlMapConfig.xml

    使用逆向工程來生成Mapper相關代碼,不需要配置別名。
    在config/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></configuration>

    applicationContext-dao.xml

    配置數據源、配置SqlSessionFactory、mapper掃描器。

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 加載配置文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 數據庫連接池 --><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="10" /><property name="maxIdle" value="5" /></bean><!-- 配置SqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 數據庫連接池 --><property name="dataSource" ref="dataSource" /><!-- 加載mybatis的全局配置文件 --><property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /></bean><!-- 配置Mapper掃描 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置Mapper掃描包 --><property name="basePackage" value="cn.itcast.ssm.mapper" /></bean></beans>

    db.properties

    配置數據庫相關信息

    jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8 jdbc.username=root jdbc.password=root

    applicationContext-service.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置service掃描 --><context:component-scan base-package="cn.itcast.ssm.service" /></beans>

    springmvc.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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置controller掃描包 --><context:component-scan base-package="cn.itcast.ssm.controller" /><!-- 注解驅動 --><mvc:annotation-driven /><!-- Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp" --><!-- 配置視圖解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!-- 配置邏輯視圖的前綴 --><property name="prefix" value="/WEB-INF/jsp/" /><!-- 配置邏輯視圖的后綴 --><property name="suffix" value=".jsp" /></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"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-web</display-name><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><!-- 配置spring --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext*.xml</param-value></context-param><!-- 使用監聽器加載Spring配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置SrpingMVC的前端控制器 --><servlet><servlet-name>springmvc-web</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param></servlet><!-- 1. /* 攔截所有 jsp js png .css 真的全攔截 建議不使用2. *.action *.do 攔截以do action 結尾的請求 肯定能使用 ERP 3. / 攔截所有 (不包括jsp) (包含.js .png.css) 強烈建議使用 前臺 面向消費者 www.jd.com/search /對靜態資源放行--><servlet-mapping><servlet-name>springmvc-web</servlet-name><!-- 配置所有以action結尾的請求進入SpringMVC --><url-pattern>*.action</url-pattern></servlet-mapping></web-app>

    加入jsp頁面

    itemList.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查詢商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post"> 查詢條件: <table width="100%" border=1> <tr> <td><input type="submit" value="查詢"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr><td>商品名稱</td><td>商品價格</td><td>生產日期</td><td>商品描述</td><td>操作</td> </tr> <c:forEach items="${itemList }" var="item"> <tr><td>${item.name }</td><td>${item.price }</td><td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td><td>${item.detail }</td><td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td></tr> </c:forEach></table> </form> </body></html>

    itemEdit.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>修改商品信息</title></head> <body> <!-- 上傳圖片是需要指定屬性 enctype="multipart/form-data" --><!-- <form id="itemForm" action="" method="post" enctype="multipart/form-data"> --><form id="itemForm" action="${pageContext.request.contextPath }/updateitem.action" method="post"><input type="hidden" name="items.id" value="${item.id }" /> 修改商品信息:<table width="100%" border=1><tr><td>商品名稱</td><td><input type="text" name="items.name" value="${item.name }" /></td></tr><tr><td>商品價格</td><td><input type="text" name="items.price" value="${item.price }" /></td></tr><tr><td>商品生產日期</td><td><input type="text" name="items.createtime"value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" /></td></tr><%-- <tr><td>商品圖片</td><td><c:if test="${item.pic !=null}"><img src="/pic/${item.pic}" width=100 height=100/><br/></c:if><input type="file" name="pictureFile"/> </td></tr>--%><tr><td>商品簡介</td><td><textarea rows="3" cols="30" name="items.detail">${item.detail }</textarea></td></tr><tr><td colspan="2" align="center"><input type="submit" value="提交" /></td></tr></table></form> </body></html>

    總結

    以上是生活随笔為你收集整理的springmvc整合mybatis之准备阶段与文件配置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。