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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)

發布時間:2025/7/25 c/c++ 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

**這個整合。僅僅是最主要的整合,而且是xml配置文件的方式之中的一個,即當中的mybatis是採用非mapper接口的方式。(第二遍採用mapper接口方式。第三遍採用注解的方式;第四篇採用注解基于maven的方式)。記錄在這里,以免下次忘記時留作備用。
===================================================================================================**
一。總體結構


二,所需jar包:
實質上并不須要所有導入,這里為了方便就所有導入啦。


(1)spring4所有jar包
(2)mybatis所有jar包
(3)mybatis-spring.jar整合須要這個包,如今spring官方已經不提供整合的jar,所以須要另外下載mybatis官方提供的整合jar。
(4)這里使用數據源dbcp.jar,依賴pool.jar,因此也須要導進來。

以及數據庫連接驅動jar也須要。

三,整合spring。springmvc和mybatis配置

<?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_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>zh2demo</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><!-- 上下文參數 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:com/mapc/conf/application-dao.xml,classpath:com/mapc/conf/application-service.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:com/mapc/conf/log4j.properties</param-value></context-param><!-- 配置log4j日志監聽器,開啟日志記錄,容器須要 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- 配置context上下文監聽器,創建root容器,和web容器集成在一起 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置springmvc控制器,會創建web上下文容器。而且會設置root上下文容器為此容器的父容器 --><servlet><servlet-name>controller</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:com/mapc/conf/application-controller.xml</param-value></init-param><load-on-startup>1</load-on-startup><!-- web容器啟動時就創建該context容器 --></servlet><servlet-mapping><servlet-name>controller</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 利用spring提供的編碼控制過濾器 --><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> </web-app>

root父容器所需的配置文件:
1。application-dao.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:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!-- 引入資源文件 --><context:property-placeholder file-encoding="utf8" location="classpath:com/mapc/conf/jdbc.properties"/><!-- 配置dbcp數據源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}"/> <span style="color:#ff0000;"><!-- 創建SqlSessionFactory。同一時候指定數據源和mybatis配置文件。

特別注意:須要引入mybatis-spring.jar-->

<bean id="sqlSessionFactory" class="</span><span style="color:#000099;">org.mybatis.spring.SqlSessionFactoryBean</span><span style="color:#ff0000;">"> </span><span style="color:#000099;"><property name="configLocation" value="classpath:com/mapc/conf/mybatisconf/Configuration.xml"/> </span><span style="color:#ff0000;"> </span><span style="color: rgb(255, 0, 0); white-space: pre;"> </span><span style="color:#ff0000;"><!--這里提供了集成持久層框架mybatis的集成點--> <property name="dataSource" ref="dataSource"></property> </bean> </span><!-- 配置Session模板類 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 配置事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 配置增強通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置共享事務 --> <aop:config expose-proxy="true"> <!-- 配置切點 --> <aop:pointcut expression="execution(* com.mapc.service..*.*(..))" id="txPointcut"/> <!-- 配置切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- 裝配StudentDao --> <bean id="studentDao" class="com.mapc.dao.impl.StudentDaoImpl" p:sqlSession-ref="sqlSessionTemplate"/> </beans>

屬性文件:

jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF8 jdbc.username=root jdbc.password=root

2,application-service.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><!-- 裝配StudentService --><bean id="studentService" class="com.mapc.service.impl.StudentServiceImpl"p:studentDao-ref="studentDao"/></beans>

3,其它,這里視詳細需求。
web層子容器的配置文件:

<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 處理器映射器:這樣的方式,從url映射到詳細的控制器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 視圖解析器,配置ContentNegotiatingViewResolver更通用 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/> <!-- 靜態資源的訪問控制 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 處理器 --> <bean id="/queryStudentList.do" class="com.mapc.controller.StudentController" p:studentService-ref="studentService"/> </beans>

mybatis配置文件Configuration.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><mappers><mapper resource="com/mapc/conf/mybatisconf/Student.xml"/></mappers></configuration>

mybatis Mapper的配置文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="Student"><resultMap type="com.mapc.bean.Student" id="studentResultMapper"><id column="ID" jdbcType="VARCHAR" property="id"/><result column="NAME" jdbcType="VARCHAR" property="name"/><result column="AGE" jdbcType="INTEGER" property="age"/></resultMap><select id="queryStudentList" resultMap="studentResultMapper">select ID,NAME,AGE from STUDENT </select> </mapper>

四,java源代碼
———-bean

package com.mapc.bean; /*** POJO* @author DC**/ public class Student {/*** 學號*/private String id;/*** 姓名*/private String name;/*** 年齡*/private int age;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";}}

———-dao以及dao.impl

package com.mapc.dao;import java.util.List;import com.mapc.bean.Student;/*** 持久層接口* @author DC**/ public interface StudentDao {/*** 查詢所有學生*/List<Student> queryStudentList(); } package com.mapc.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate;import com.mapc.bean.Student; import com.mapc.dao.StudentDao; /*** 持久層詳細實現類之中的一個* @author DC**/ public class StudentDaoImpl implements StudentDao {private SqlSessionTemplate sqlSessionTemplate;public void setSqlSession(SqlSessionTemplate sqlSessionTemplate) {this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic List<Student> queryStudentList() {return sqlSessionTemplate.selectList("Student.queryStudentList");}}

————service和service.impl

package com.mapc.service;import java.util.List;import com.mapc.bean.Student; import com.mapc.dao.StudentDao;/*** 學生相關的業務接口* @author DC**/ public interface StudentService {/*** 查詢學生信息的業務接口方法*/public List<Student> queryStudentList();} package com.mapc.service.impl;import java.util.List;import com.mapc.bean.Student; import com.mapc.dao.StudentDao; import com.mapc.service.StudentService; /*** 學生業務的詳細實現類* @author DC**/ public class StudentServiceImpl implements StudentService {/*** 注入StudentDao*/private StudentDao studentDao;public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic List<Student> queryStudentList() {return studentDao.queryStudentList();}}

——–controller頁面處理器

package com.mapc.controller;import java.util.List;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller;import com.mapc.bean.Student; import com.mapc.service.StudentService; /*** 學生控制類* @author DC**/ public class StudentController implements Controller {/*** 注入學生業務類*/private StudentService studentService;public void setStudentService(StudentService studentService) {this.studentService = studentService;}@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {ModelAndView mv=new ModelAndView();List<Student> list=studentService.queryStudentList();mv.addObject("studentList",list);System.out.println("----------------:"+list.toString());mv.setViewName("showStudentList");return mv;}}

五,數據表

六。執行測試(利用別的測試手段也得)

菜鳥首次整合,盡管非常easy,在第一次整合的時候還是出現各種難以預料的錯誤的。

為了更加清晰這里面的情況,整合了第二次,還是實踐可以驗證整理。

多實踐,多思考,謹記!

轉載于:https://www.cnblogs.com/zsychanpin/p/7278572.html

總結

以上是生活随笔為你收集整理的spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

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