激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

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

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

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

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

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

一、 動態代理實現 不用寫dao的實現類

這種方式比較簡單,不用實現dao層,只需要定義接口就可以了,這里只是為了記錄配置文件所以程序寫的很簡單:

1、整體結構圖:

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

2、三個配置文件以及一個映射文件

(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>
 <!-- 設置監聽,在web容器啟動時自動裝配applicationcontext的配置信息-->
 <listener>
  <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
 </listener>
 <!-- 設置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>
  <!-- 這個配置文件在容器啟動的時候 就加載 -->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <!-- 攔截請求 -->
  <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)、掃描控制層、自動注入以及視圖解析器的配置 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">
 <!-- 注解驅動 -->
 <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)、數據源、service 自動掃描注入、spring代管mybatissqlsessionfactory 、dao層接口動態代理以及事務的配置applicationcontext.xml

這里會有多中配置文件

1)、單數據源,動態代理實現dao層接口時不設置sqlsessionfactorybeanname、或sqlsessiontemplatebeanname 兩個屬性的值

?
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" />
 <!-- 數據源 -->
 <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/> -->
 <!--其實component-scan 就有了annotation-config的功能即把需要的類注冊到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時 spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用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> -->
 <!--動態代理實現 不用寫dao的實現 -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實現 -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定 -->
  <!-- <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> -->
  <!--直接指定了sqlsessiontemplate名稱,這個和上面的其實是一樣的 -->
  <!-- <property name="sqlsessiontemplatebeanname" value="sqlsession" /> -->
 </bean>
 <!--事務管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務 -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

2)、單數據源配置 sqlsessionfactorybeanname 這個屬性值

?
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" />
 <!-- 數據源 -->
 <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/> -->
 <!--其實component-scan 就有了annotation-config的功能即把需要的類注冊到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時 spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用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> -->
 <!--動態代理實現 不用寫dao的實現 -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實現 -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定 -->
  <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" />
  <!--直接制定了sqlsessiontemplate名稱,這個和上面的其實是一樣的 -->
  <!-- <property name="sqlsessiontemplatebeanname" value="sqlsession" /> -->
 </bean>
 <!--事務管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務 -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

3)、單數據源配置sqlsessiontemplatebeanname 這個屬性值

?
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" />
 <!-- 數據源 -->
 <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/> -->
 <!--其實component-scan 就有了annotation-config的功能即把需要的類注冊到了spring容器中 -->
 <context:component-scan base-package="com.website.service" />
 <!-- 在使用mybatis時 spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
  <property name="datasource" ref="datasource" />
  <!-- mybatis配置文件路徑 -->
  <property name="configlocation" value="" />
  <!-- 實體類映射文件路徑,這里只有一個就寫死了,多個可以使用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>
 <!--動態代理實現 不用寫dao的實現 -->
 <bean id="mapperscannerconfigurer" class="org.mybatis.spring.mapper.mapperscannerconfigurer">
  <!-- 這里的basepackage 指定了dao層接口路勁,這里的dao接口不用自己實現 -->
  <property name="basepackage" value="com.website.dao" />
  <!-- 如果只有一個數據源的話可以不用指定,但是如果有多個數據源的話必須要指定 -->
  <!-- <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" /> -->
  <!--直接制定了sqlsessiontemplate名稱,這個和上面的其實是一樣的 -->
  <property name="sqlsessiontemplatebeanname" value="sqlsession" />
 </bean>
 <!--事務管理器 -->
 <bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">
  <property name="datasource" ref="datasource" />
 </bean>
 <!-- 使用全注釋事務 -->
 <tx:annotation-driven transaction-manager="transactionmanager" />
</beans>

4)、多數據源

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

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

來創建具體的實例并賦值給sqlsessiontemplatebeanname 這個屬性。

(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接口的完整路勁,就這個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  到這里配置文件到搞定了下面來看看控制層,業務邏輯層以及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 對象
 @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接口實現類實例
 // @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 接口的完整路勁就是這個dao 接口對應的那個映射文件的namespace 而方法名就是 id的值

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

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

二、手動實現dao層接口

下面先來看看手動實現dao層的配置以及代碼:

1、正題結構圖

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

2、三個配置文件以及映射文件

(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>
 <!-- 設置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>
 <!-- 這個配置文件在容器啟動的時候 就加載 -->
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>springmvc</servlet-name>
 <!-- 攔截請求 -->
 <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)、掃描控制層、自動注入以及視圖解析器的配置 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">
 <!-- 注解驅動 -->
 <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)、數據源、service 自動掃描注入、spring代管mybatissqlsessionfactory 以及事務的配置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" />
 <!-- 數據源 -->
 <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/> -->
 <!--使用自動注入的時候要 添加他來掃描bean之后才能在使用的時候 -->
 <context:component-scan base-package="com.website.service ,com.website.dao" />
 <!-- 在使用mybatis時 spring使用sqlsessionfactorybean 來管理mybatis的sqlsessionfactory -->
 <!-- 而像這種使用接口實現的方式 是使用sqlsessiontemplate來進行操作的,他提供了一些方法 -->
 <bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
 <property name="datasource" ref="datasource" />
 <!-- mybatis配置文件路徑 -->
 <property name="configlocation" value="" />
 <!-- 實體類映射文件路徑,在開發中映射文件肯定是多個所以使用mybatis/*.xml來替代 -->
 <property name="mapperlocations" value="classpath:mybatis/usermapping.xml" />
 </bean>
 <!--其實這里類的實例就是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>
 <!--使用全注釋事務 -->
 <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">
 <!--這個namespace + 下面的id 就是一個完整的路徑,在dao層我們寫了完整的路徑之后mybatis就是映射這個文件中的相關sql語句 -->
<mapper namespace="com.website.usermapper">
<!-- parametertype就是你接受的參數的類型, -->
<!-- 添加用戶信息 -->
<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、業務邏輯層 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  這個其實是mybatis中的sqlsession 對象,我們看到在applicationcontext.xml 中配置了,所以在我們使用時spring會幫我們自動注入,我們直接使用就可以了不用去自己創建,這也就是所謂的控制反轉。ok 到此兩種文件的配置方式就結束了。

總結

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 中文字幕网站在线 | 欧美一级高清免费 | 亚洲欧美在线看 | 国产视频在线观看免费 | 日本aⅴ在线 | 97se亚洲综合在线韩国专区福利 | 久久中出| 草草久久久 | 亚洲人成网站在e线播放 | 国产成人高潮免费观看精品 | 欧美成人二区 | 成人午夜一区二区 | 色猫av| 一本色道久久99精品综合蜜臀 | 国产www免费| 少妇一级淫片免费放播放 | 91久久国产露脸精品国产护士 | 男女羞羞视频在线观看免费 | 中文字幕激情 | 久久色伦理资源站 | 欧美18一12sex性处hd | 亚洲成人第一区 | 久久亚洲精品国产 | 国产一区二区三区视频观看 | 国产一级免费在线视频 | 亚州综合图片 | 欧美日韩亚洲成人 | 黄色网址在线播放 | asian裸体佳人pics | 毛片视频大全 | 日本视频在线免费观看 | 国产91porn| 色婷婷综合久久久中文一区二区 | 久久毛片免费观看 | 激情视频在线播放 | 三人弄娇妻高潮3p视频 | 91色综合综合热五月激情 | 欧美a∨一区二区三区久久黄 | 欧美特黄一级视频 | 中文字幕电影免费播放 | 毛片视频播放 |