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

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

生活随笔

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

javascript

Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合

發(fā)布時(shí)間:2023/12/3 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
【0】README 0)本文旨在 review Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合過(guò)程; 1)項(xiàng)目整合所涉及的源代碼,please visit ?https://github.com/pacosonTang/MyBatis/tree/master/spring4mvc_mybatis3 2)由于晚輩我還不怎么熟悉maven,所以沒(méi)有用maven依賴(lài)各種jar 依賴(lài)包來(lái)構(gòu)建項(xiàng)目(編譯項(xiàng)目),抱歉;(不過(guò)有興趣的朋友,吧上述的源碼down 下來(lái),里面有相應(yīng)的jar 文件,足矣)
【1】Spring4.2.6+MyBatis3.4.0 整合 1)準(zhǔn)備工作 step1)建立數(shù)據(jù)庫(kù)和表字段

step2)安裝 mybatis generator:download from?https://github.com/mybatis/generator, (具體的安裝 detail 不再累述,自行g(shù)oogle)

step3)安裝完后,在eclipse中執(zhí)行如下操作(當(dāng)然,你要先建立好web proj);(t3)

step4)修改generatorConfig.xml 中的相應(yīng)配置信息,右鍵該文件,點(diǎn)擊 generate MyBatis/iBatis artifacts,生成相應(yīng)的 ORM 映射文件;

對(duì)以上生成文件的分析(Analysis): A1)TStudentMapper 是個(gè)接口:它其實(shí)是作為 dao 層,數(shù)據(jù)訪(fǎng)問(wèn)層,你可以將其 放置在 dao package中,它沒(méi)有實(shí)現(xiàn)類(lèi),它其中的方法具體實(shí)現(xiàn) 由?TStudentMapper.xml 中的XML 配置元素來(lái)實(shí)現(xiàn);(這里很容易理解,Apache Digester庫(kù) 通過(guò)反射可以將 XML元素 轉(zhuǎn)換為java類(lèi)對(duì)象),這種實(shí)現(xiàn)就是與底層數(shù)據(jù)庫(kù)進(jìn)行交互(厲害); A2)TStudent :就是一個(gè) model 或 pojo,你可以將其 移入到 model 或 pojo package下;
2)建立 M(Model) 層級(jí)架構(gòu):建立模型層中的邏輯處理層 service層: 創(chuàng)建相應(yīng)接口StudentService.java 和 實(shí)現(xiàn)類(lèi)StudentServiceImpl.java,在實(shí)現(xiàn)類(lèi)中調(diào)用 dao 層的數(shù)據(jù)庫(kù)訪(fǎng)問(wèn)方法; package com.mybatis.service;import com.mybatis.model.Student;public interface StudentService {void insertStudent(int id, String username);public Student getStudentById(int Id); } package com.mybatis.serviceImpl;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.mybatis.dao.StudentMapper; import com.mybatis.model.Student; import com.mybatis.service.StudentService;@Service public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentMapper studentMapper;@Overridepublic void insertStudent(int id, String username) {Student stu = new Student(id, username);}@Overridepublic Student getStudentById(int id) {return studentMapper.selectByPrimaryKey(id);} } 3)建立 spring 和 mybatis 整合的配置xml 文件(spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.mybatis" /><!-- 引入 jdbc 配置文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <bean id="dataSource"class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /><!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}" /> <!-- 連接池最大數(shù)量 --> <property name="maxTotal" value="${maxTotal}" /> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}" /> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}" /> <!-- 獲取連接最大等待時(shí)間 --> <property name="maxWaitMillis" value="${maxWait}" /> </bean><!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自動(dòng)掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/mybatis/orm/*.xml" /> </bean><!-- DAO接口所在包名,Spring會(huì)自動(dòng)查找其下的類(lèi) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.mybatis.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean><!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean> </beans> 3.1)log4j.properties(log4j 日志屬性文件) #定義LOG輸出級(jí)別 log4j.rootLogger=INFO,Console,File #定義日志輸出目的地為控制臺(tái) log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #可以靈活地指定日志輸出格式,下面一行是指定具體的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n#文件大小到達(dá)指定尺寸的時(shí)候產(chǎn)生一個(gè)新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定輸出目錄 log4j.appender.File.File = logs/ssm.log #定義文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 輸出所以日志,如果換成DEBUG表示輸出DEBUG以上級(jí)別日志 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n 3.2)jdbc.properties(jdbc屬性文件) driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/t_mybatis username=root password=root #定義初始連接數(shù) initialSize=0 #定義最大連接數(shù) maxTotal=20 #定義最大空閑 maxIdle=20 #定義最小空閑 minIdle=1 #定義最長(zhǎng)等待時(shí)間 maxWait=60000
4)建立測(cè)試用例
package com.mybatis.test;import javax.annotation.Resource;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.mybatis.model.Student; import com.mybatis.service.StudentService;@RunWith(SpringJUnit4ClassRunner.class) //表示繼承了SpringJUnit4ClassRunner類(lèi) @ContextConfiguration(locations = {"classpath:spring-mybatis.xml"}) public class TestMyBatis { @Resource private StudentService studentService; @Test public void test1() { Student stu = studentService.getStudentById(1); System.out.println(" the name is " + stu.getName()); } } 5)測(cè)試用例結(jié)果(Bingo!)

【2】Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合 1)intro:因?yàn)?springmvc 需要使用視圖,而前臺(tái)視圖需要通過(guò)控制器做路由映射與后臺(tái)數(shù)據(jù)庫(kù)進(jìn)行交互,所以我們要添加 V-View視圖層 和 C-Controller控制器層;
step1)V-View視圖層: 建立jsp 文件 渲染 服務(wù)器響應(yīng)的結(jié)果;(t6) <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>測(cè)試</title></head><body>${student.name}</body> </html>

step2)C-Controller 控制器層:建立Controller java 類(lèi),對(duì)前臺(tái)請(qǐng)求路徑進(jìn)行路由映射; package com.mybatis.controller;import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod;import com.mybatis.model.Student; import com.mybatis.service.StudentService;@Controller @RequestMapping(value="/stu") public class StudentController {@Resourceprivate StudentService studentService; @RequestMapping(value="/showUser", method=RequestMethod.GET) public String toIndex(HttpServletRequest request, Model model){ int userId = Integer.parseInt(request.getParameter("id")); Student student = this.studentService.getStudentById(userId); model.addAttribute("student", student); return "showUser"; } } 2)建立 springmvc 的配置xml 文件(spring-mvc.xml):因?yàn)閟pringmvc 是基于spring的,所以無(wú)需建立?springmvc?與 mybatis整合的配置文件; <?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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動(dòng)掃描該包,使SpringMVC認(rèn)為包下用了@controller注解的類(lèi)是控制器 --> <context:component-scan base-package="com.mybatis.controller" /> <!--避免IE執(zhí)行AJAX時(shí),返回JSON出現(xiàn)下載文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 啟動(dòng)SpringMVC的注解功能,完成請(qǐng)求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉(zhuǎn)換器 --> </list> </property> </bean> <!-- 定義跳轉(zhuǎn)的文件的前后綴 ,視圖模式配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 這里的配置我的理解是自動(dòng)給后面action的方法return的字符串加上前綴和后綴,變成一個(gè) 可用的url地址 --> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置文件上傳,如果沒(méi)有使用文件上傳可以不用配置,當(dāng)然如果不配,那么配置文件中也不必引入上傳組件包 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認(rèn)編碼 --> <property name="defaultEncoding" value="utf-8" /> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000" /> <!-- 內(nèi)存中的最大值 --> <property name="maxInMemorySize" value="40960" /> </bean> </beans> 3)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_3_0.xsd"version="3.0"><display-name>MyBatis SpringMVC Integration</display-name><!-- Spring和mybatis的配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring-mybatis.xml</param-value></context-param><!-- 編碼過(guò)濾器 --><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><async-supported>true</async-supported><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監(jiān)聽(tīng)器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 防止Spring內(nèi)存溢出監(jiān)聽(tīng)器 --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- Spring MVC servlet --><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-mvc.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported></servlet><servlet-mapping><servlet-name>SpringMVC</servlet-name><!-- 此處可以可以配置成*.do,對(duì)應(yīng)struts的后綴習(xí)慣 --><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>/index.jsp</welcome-file></welcome-file-list></web-app>
4)測(cè)試用例和結(jié)果

總結(jié)

以上是生活随笔為你收集整理的Spring4.2.6+SpringMVC4.2.6+MyBatis3.4.0 整合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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