国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - spring、mybatis 配置方式詳解(常用兩種方式)

spring、mybatis 配置方式詳解(常用兩種方式)

2021-03-03 14:16QH_JAVA Java教程

這篇文章給大家總結(jié)了常用的兩種spring、mybatis 配置方式,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧

在之前的文章中總結(jié)了三種方式,但是有兩種是注解sql的,這種方式比較混亂所以大家不怎么使用,下面總結(jié)一下常用的兩種總結(jié)方式:

一、 動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫dao的實(shí)現(xiàn)類

這種方式比較簡單,不用實(shí)現(xiàn)dao層,只需要定義接口就可以了,這里只是為了記錄配置文件所以程序?qū)懙暮芎唵危?/p>

1、整體結(jié)構(gòu)圖:

spring、mybatis 配置方式詳解(常用兩種方式)

2、三個(gè)配置文件以及一個(gè)映射文件

(1)、程序入口以及前端控制器配置 web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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>website1</display-name>
 <!-- 設(shè)置監(jiān)聽,在web容器啟動(dòng)時(shí)自動(dòng)裝配applicationcontext的配置信息-->
 <listener>
  <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 <!-- 設(shè)置spring容器加載配置文件路徑 -->
 <context-param>
  <param-name>contextconfiglocation</param-name>
  <param-value>
  classpath:config/springmvc-servlet.xml,
  classpath:config/applicationcontext.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>
  <init-param>
   <param-name>forceencoding</param-name>
   <param-value>true</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>encodingfilter</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
 <!-- 前端控制器 -->
 <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:config/springmvc-servlet.xml</param-value>
  </init-param>
  <!-- 這個(gè)配置文件在容器啟動(dòng)的時(shí)候 就加載 -->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!-- 攔截請(qǐng)求 -->
  <url-pattern>*.do</url-pattern>
 </servlet-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>

  (2)、掃描控制層、自動(dòng)注入以及視圖解析器的配置 springmvc-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemalocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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-3.1.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
 <!-- 注解驅(qū)動(dòng) -->
 <mvc:annotation-driven />
 <!-- <context:annotation-config /> -->
 <!-- context:component-scan 具有annotation-config 的功能 -->
 <!-- 掃描 控制層 -->
 <context:component-scan base-package="com.website.controller"></context:component-scan>
 <!-- 視圖解析器 -->
 <bean id="viewresolver" class="org.springframework.web.servlet.view.internalresourceviewresolver">
  <property name="prefix" value="/web-inf/view/">
  </property>
  <property name="suffix" value=".jsp"></property>
 </bean>
</beans>

(3)、數(shù)據(jù)源、service 自動(dòng)掃描注入、spring代管mybatissqlsessionfactory 、dao層接口動(dòng)態(tài)代理以及事務(wù)的配置applicationcontext.xml

這里會(huì)有多中配置文件

1)、單數(shù)據(jù)源,動(dòng)態(tài)代理實(shí)現(xiàn)dao層接口時(shí)不設(shè)置sqlsessionfactorybeanname、或sqlsessiontemplatebeanname 兩個(gè)屬性的值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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:tx="http://www.springframework.org/schema/tx" 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.2.xsd
  http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 <!-- 加載配置jdbc文件 -->
 <context:property-placeholder location="classpath:db.properties" />
 <!-- 數(shù)據(jù)源 -->
 <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
  <property name="driverclassname">
   <value>${jdbc.driverclassname}</value>
  </property>
  <property name="url">
   <value>${jdbc.url}</value>
  </property>
  <property name="username">
   <value>${jdbc.username}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
 </bean>
 <!-- 開啟注解配置 即autowried -->
 <!-- <context:annotation-config/> -->
 <!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類注冊(cè)到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時(shí) spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實(shí)體類映射文件路徑,這里只有一個(gè)就寫死了,多個(gè)可以使用mybatis/*.xml來替代 -->
  <property name="mapperlocations" value="classpath:mybatis/usermapper.xml" />
 </bean>
 <!-- <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg index="0"> <ref bean="sqlsessionfactory"/>
  </constructor-arg> </bean> -->
 <!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫dao的實(shí)現(xiàn) -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
  <!-- <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> -->
  <!--直接指定了sqlsessiontemplate名稱,這個(gè)和上面的其實(shí)是一樣的 -->
  <!-- <property name="sqlsessiontemplatebeanname" value="sqlsession" /> -->
 </bean>
 <!--事務(wù)管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務(wù) -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

2)、單數(shù)據(jù)源配置 sqlsessionfactorybeanname 這個(gè)屬性值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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:tx="http://www.springframework.org/schema/tx" 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.2.xsd
  http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 <!-- 加載配置jdbc文件 -->
 <context:property-placeholder location="classpath:db.properties" />
 <!-- 數(shù)據(jù)源 -->
 <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
  <property name="driverclassname">
   <value>${jdbc.driverclassname}</value>
  </property>
  <property name="url">
   <value>${jdbc.url}</value>
  </property>
  <property name="username">
   <value>${jdbc.username}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
 </bean>
 <!-- 開啟注解配置 即autowried -->
 <!-- <context:annotation-config/> -->
 <!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類注冊(cè)到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時(shí) spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實(shí)體類映射文件路徑,這里只有一個(gè)就寫死了,多個(gè)可以使用mybatis/*.xml來替代 -->
  <property name="mapperlocations" value="classpath:mybatis/usermapper.xml" />
 </bean>
 <!-- <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate"> <constructor-arg index="0"> <ref bean="sqlsessionfactory"/>
  </constructor-arg> </bean> -->
 <!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫dao的實(shí)現(xiàn) -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
  <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" />
  <!--直接制定了sqlsessiontemplate名稱,這個(gè)和上面的其實(shí)是一樣的 -->
  <!-- <property name="sqlsessiontemplatebeanname" value="sqlsession" /> -->
 </bean>
 <!--事務(wù)管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務(wù) -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

3)、單數(shù)據(jù)源配置sqlsessiontemplatebeanname 這個(gè)屬性值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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:tx="http://www.springframework.org/schema/tx" 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.2.xsd
  http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 <!-- 加載配置jdbc文件 -->
 <context:property-placeholder location="classpath:db.properties" />
 <!-- 數(shù)據(jù)源 -->
 <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
  <property name="driverclassname">
   <value>${jdbc.driverclassname}</value>
  </property>
  <property name="url">
   <value>${jdbc.url}</value>
  </property>
  <property name="username">
   <value>${jdbc.username}</value>
  </property>
  <property name="password">
   <value>${jdbc.password}</value>
  </property>
 </bean>
 <!-- 開啟注解配置 即autowried -->
 <!-- <context:annotation-config/> -->
 <!--其實(shí)component-scan 就有了annotation-config的功能即把需要的類注冊(cè)到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時(shí) spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實(shí)體類映射文件路徑,這里只有一個(gè)就寫死了,多個(gè)可以使用mybatis/*.xml來替代 -->
  <property name="mapperlocations" value="classpath:mybatis/usermapper.xml" />
 </bean>
 <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate">
  <constructor-arg index="0">
   <ref bean="sqlsessionfactory" />
  </constructor-arg>
 </bean>
 <!--動(dòng)態(tài)代理實(shí)現(xiàn) 不用寫dao的實(shí)現(xiàn) -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實(shí)現(xiàn) -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個(gè)數(shù)據(jù)源的話可以不用指定,但是如果有多個(gè)數(shù)據(jù)源的話必須要指定 -->
  <!-- <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> -->
  <!--直接制定了sqlsessiontemplate名稱,這個(gè)和上面的其實(shí)是一樣的 -->
  <property name="sqlsessiontemplatebeanname" value="sqlsession" />
 </bean>
 <!--事務(wù)管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務(wù) -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

4)、多數(shù)據(jù)源

注意如果是多數(shù)據(jù)源則一定要使用sqlsessionfactorybeanname 或sqlsessiontemplatebeanname 來指定具體的數(shù)據(jù)源,不知道在上面的配置中有沒有注意到,如果使用sqlsessiontemplatebeanname 的話要

?
1
2
3
4
5
<bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate">
  <constructor-arg index="0">
   <ref bean="sqlsessionfactory" />
  </constructor-arg>
 </bean>

來創(chuàng)建具體的實(shí)例并賦值給sqlsessiontemplatebeanname 這個(gè)屬性。

(4)、mybatis sql映射文件 usermapper.xml:

?
1
2
3
4
5
6
7
8
9
10
<?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">
<!-- namespace的值就是dao接口的完整路勁,就這個(gè)demo而言namespace 就是userdao.java的完整路勁 -->
<mapper namespace="com.website.dao.userdao">
 <!-- 這里的id就是接口中方法的名稱 -->
 <insert id="saveuser" parametertype="java.util.map">
  insert into user(id,name) values(#{id},#{name})
 </insert>
</mapper>

ok  到這里配置文件到搞定了下面來看看控制層,業(yè)務(wù)邏輯層以及dao層的代碼。

3、controller層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.website.controller;
import java.util.hashmap;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import com.website.service.userservice;
@controller
@requestmapping(value = "/user")
public class usercontroller {
 // 注入userservice 對(duì)象
 @autowired
 private userservice userservice;
 @requestmapping(value = "/save.do", method = requestmethod.get)
 public string saveuser(httpservletrequest request,
  httpservletresponse response) {
 string id = request.getparameter("id");
 string name = request.getparameter("name");
 map<string, string> map = new hashmap<string, string>();
 map.put("id", id);
 map.put("name", name);
 userservice.saveuser(map);
 return "index";
 }
}

4、service層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.website.service;
import java.util.map;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import com.website.dao.userdao;
@service("userservice")
@transactional
public class userservice {
 // 注入dao接口實(shí)現(xiàn)類實(shí)例
 // @resource、@autowired兩種注入方式都可以
 @autowired
 private userdao userdao;
 public void saveuser(map<string, string> map) {
 int end = userdao.saveuser(map);
 system.out.println("end:" + end);
 }
}

5、dao 層 接口

?
1
2
3
4
5
6
package com.website.dao;
import java.util.map;
//com.website.dao.userdao
public interface userdao {
 int saveuser(map<string, string> map);
}

dao 接口的完整路勁就是這個(gè)dao 接口對(duì)應(yīng)的那個(gè)映射文件的namespace 而方法名就是 id的值

ok到這里這種配置方式都完了,也有了一個(gè)完整的小demo,下面我們簡單總結(jié)一下:

這種配置方式相比之前的配置方式(下面也會(huì)寫出來)特別之處就是他使用了dao層接口的動(dòng)態(tài)代理方式實(shí)現(xiàn)了,之前我們會(huì)在dao層自己手動(dòng)實(shí)現(xiàn)dao層然后自動(dòng)注入sqlsessiontemplate 實(shí)例來調(diào)用具體的方法 比如 insert("","")  selectone("","") 等方法 其中第一個(gè)參數(shù)就是映射文件的地址: namespace+id  而第二個(gè)參數(shù)就是傳遞的條件這樣mybatis 就會(huì)按照我們傳遞的這兩個(gè)參數(shù)找到具體的映射文件進(jìn)行解析查詢。而這里使用動(dòng)態(tài)代理就省去了我們實(shí)現(xiàn)dao接口的這一步驟,而是由spring提我們實(shí)現(xiàn)了,那有個(gè)問題,查詢條件參數(shù)我們傳遞了,但映射文件的具體路徑即:namespce+id  沒有傳遞怎么辦,那就是你的映射文件的namespace 必須是接口的類全名稱而id 必須是接口中的方法名稱,這樣動(dòng)態(tài)代理就能找到路勁了也有了參數(shù)了。 這樣一來是不是覺得就一樣了啊哈哈哈!

二、手動(dòng)實(shí)現(xiàn)dao層接口

下面先來看看手動(dòng)實(shí)現(xiàn)dao層的配置以及代碼:

1、正題結(jié)構(gòu)圖

spring、mybatis 配置方式詳解(常用兩種方式)

2、三個(gè)配置文件以及映射文件

(1)、程序入口,前端控制器配置 web.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?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>website2</display-name>
 <!-- 加載spring容器配置 -->
 <listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 <!-- 設(shè)置spring容器加載配置文件路徑 -->
 <context-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>
  classpath:config/springmvc-servlet.xml,
  classpath:config/applicationcontext.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>
 <init-param>
  <param-name>forceencoding</param-name>
  <param-value>true</param-value>
 </init-param>
 </filter>
 <filter-mapping>
 <filter-name>encodingfilter</filter-name>
 <url-pattern>*.do</url-pattern>
 </filter-mapping>
 <!-- 前端控制器 -->
 <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:config/springmvc-servlet.xml</param-value>
 </init-param>
 <!-- 這個(gè)配置文件在容器啟動(dòng)的時(shí)候 就加載 -->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <!-- 攔截請(qǐng)求 -->
 <url-pattern>*.do</url-pattern>
 </servlet-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>

(2)、掃描控制層、自動(dòng)注入以及視圖解析器的配置 springmvc-servlet.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
 xmlns:cache="http://www.springframework.org/schema/cache"
 xsi:schemalocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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-3.1.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
 <!-- 注解驅(qū)動(dòng) -->
 <mvc:annotation-driven />
 <!-- <context:annotation-config /> -->
 <!-- 掃描 -->
 <context:component-scan base-package="com.website.controller"></context:component-scan>
 <!-- 視圖解析器 -->
 <bean id="viewresolver"
 class="org.springframework.web.servlet.view.internalresourceviewresolver">
 <property name="prefix" value="/web-inf/view/">
 </property>
 <property name="suffix" value=".jsp"></property>
 </bean>
</beans>

(3)、數(shù)據(jù)源、service 自動(dòng)掃描注入、spring代管mybatissqlsessionfactory 以及事務(wù)的配置applicationcontext.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?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:tx="http://www.springframework.org/schema/tx"
 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.2.xsd
  http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
 <!-- 加載配置jdbc文件 -->
 <context:property-placeholder location="classpath:db.properties" />
 <!-- 數(shù)據(jù)源 -->
 <bean id="datasource"
 class="org.springframework.jdbc.datasource.drivermanagerdatasource">
 <property name="driverclassname">
  <value>${jdbc.driverclassname}</value>
 </property>
 <property name="url">
  <value>${jdbc.url}</value>
 </property>
 <property name="username">
  <value>${jdbc.username}</value>
 </property>
 <property name="password">
  <value>${jdbc.password}</value>
 </property>
 </bean>
 <!-- 開啟注解配置 即autowried -->
 <!--component-scan擁有 annotation-config的功能即注入需要的類到spring容器中 -->
 <!--<context:annotation-config/> -->
 <!--使用自動(dòng)注入的時(shí)候要 添加他來掃描bean之后才能在使用的時(shí)候 -->
 <context:component-scan base-package="com.website.service ,com.website.dao" />
 <!-- 在使用mybatis時(shí) spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <!-- 而像這種使用接口實(shí)現(xiàn)的方式 是使用sqlsessiontemplate來進(jìn)行操作的,他提供了一些方法 -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
 <property name="datasource" ref="datasource" />
 <!-- mybatis配置文件路徑 -->
 <property name="configlocation" value="" />
 <!-- 實(shí)體類映射文件路徑,在開發(fā)中映射文件肯定是多個(gè)所以使用mybatis/*.xml來替代 -->
 <property name="mapperlocations" value="classpath:mybatis/usermapping.xml" />
 </bean>
 <!--其實(shí)這里類的實(shí)例就是mybatis中sqlsession -->
 <bean id="sqlsession" class="org.mybatis.spring.sqlsessiontemplate">
 <constructor-arg index="0">
  <ref bean="sqlsessionfactory" />
 </constructor-arg>
 </bean>
 <bean id="transactionmanager"
 class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
 <property name="datasource" ref="datasource" />
 </bean>
 <!--使用全注釋事務(wù) -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

(4)、mybatis 映射文件

?
1
2
3
4
5
6
7
8
9
10
11
<?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">
 <!--這個(gè)namespace + 下面的id 就是一個(gè)完整的路徑,在dao層我們寫了完整的路徑之后mybatis就是映射這個(gè)文件中的相關(guān)sql語句 -->
<mapper namespace="com.website.usermapper">
<!-- parametertype就是你接受的參數(shù)的類型, -->
<!-- 添加用戶信息 -->
<insert id="insertuser" parametertype="java.util.map">
 insert into user(id,name,password) values(#{id},#{name},#{password})
</insert>
</mapper>

你可能看到了這里的映射文件的namespace +id  是自定義的而不是dao 層接口的全類名+id

3、控制層controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.website.controller;
import java.util.hashmap;
import java.util.map;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import com.website.service.userservice;
/**
 * @author whd data 2016年6月5日
 */
@controller
@requestmapping(value = "/user")
public class usercontroller {
 @autowired
 private userservice userservice;
 @requestmapping(value = "/save.do")
 public string saveuser(httpservletrequest request,
  httpservletresponse response) {
 string id = request.getparameter("id");
 string name = request.getparameter("name");
 string password = request.getparameter("password");
 map<string, string> map = new hashmap<string, string>();
 map.put("id", id);
 map.put("name", name);
 map.put("password", password);
 userservice.saveuser(map);
 return "index";
 }
}

4、業(yè)務(wù)邏輯層 service

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.website.service;
import java.util.map;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import org.springframework.transaction.annotation.transactional;
import com.website.dao.userdao;
/**
 * @author whd data 2016年6月5日
 */
@service("userservice")
@transactional
public class userservice {
 @autowired
 private userdao userdao;
 public void saveuser(map<string, string> map) {
 userdao.saveuser(map);
 }
}

5、dao層

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.website.dao;
import java.util.map;
import org.mybatis.spring.sqlsessiontemplate;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.repository;
/**
 * @author whd data 2016年6月5日
 */
@repository("userdao")
public class userdao {
 @autowired
 private sqlsessiontemplate sqlsession;
 public void saveuser(map<string, string> map) {
 int end = sqlsession.insert("com.website.usermapper.insertuser", map);
 system.out.println("end" + end);
 }
}

我們看倒dao層的 sqlsessiontemplate  這個(gè)其實(shí)是mybatis中的sqlsession 對(duì)象,我們看到在applicationcontext.xml 中配置了,所以在我們使用時(shí)spring會(huì)幫我們自動(dòng)注入,我們直接使用就可以了不用去自己創(chuàng)建,這也就是所謂的控制反轉(zhuǎn)。ok 到此兩種文件的配置方式就結(jié)束了。

總結(jié)

以上所述是小編給大家介紹的spring、mybatis 配置方式詳解(常用兩種方式),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!

原文鏈接:http://blog.csdn.net/QH_JAVA/article/details/51601139

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久国产精品视频 | 91羞羞网站 | 日韩欧美精品 | 国产黄a三级三级看三级 | 久久久精品在线 | 91麻豆精品国产91久久久资源速度 | 国产天堂在线 | 欧美午夜一区二区三区免费大片 | 国产免费自拍 | 日韩中文字幕在线视频 | 欧美在线不卡视频 | 欧美黑人性暴力猛交喷水黑人巨大 | 欧美日韩中文在线观看 | 国产欧美视频一区二区三区 | 美女久久久久 | 精品视频在线播放 | 国产亚洲精品久久久 | 国产女人爽到高潮免费视频 | 成人午夜精品一区二区三区 | 亚洲一区二区三区在线播放 | 成人精品 | 日韩免费在线观看视频 | 在线色网站| a久久 | 免费的污网站 | 亚洲aⅴ天堂av在线电影软件 | 一级免费毛片 | 日韩成人在线播放 | 亚洲国产精品久久久 | 精品国产一区二区三区在线观看 | 亚洲一区免费在线观看 | 日韩成人影院 | 国产精品永久免费视频 | 红杏首页| 成人综合网站 | 欧美日韩国产精品一区二区 | 综合色在线 | 午夜精品一区 | 亚洲国内精品 | 婷婷综合在线 | 91精品国产91久久久久久 |