最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法

 更新時(shí)間:2016年06月01日 11:54:44   作者:czj4451  
和MyBatis類似,Spring或者Spring MVC框架在Web應(yīng)用程序的運(yùn)作中同樣主要負(fù)責(zé)處理數(shù)據(jù)庫(kù)事務(wù),這里我們就來看一下Java環(huán)境中MyBatis與Spring或Spring MVC框架的集成方法

與Spring3集成
Spring作為基礎(chǔ)框架,可以集成后端框架,如Hibernate,MyBatis等。

前面是介紹單獨(dú)使用MyBatis的,大致邏輯是:
sqlSessionFactory <- configuration file (包括數(shù)據(jù)庫(kù)連接配置)
IXxxMapper <- sqlSession <- sqlSessionFactory
                     <- mapper interface <- mapper xml
得到IxxMapper后,就可以調(diào)用其方法進(jìn)行數(shù)據(jù)交互了。

和Spring集成時(shí),上面的這些對(duì)象需要作為bean來管理:
dataSource bean <- 數(shù)據(jù)庫(kù)連接配置
sqlSessionFactory bean <- dataSource
                                     <- configuration file
userMapper bean <- sqlSessionFactory
                          <- mapper interface
1. 在pom.xml中加入依賴:

<properties> 
 <mybatis.spring.version>1.2.1</mybatis.spring.version> 
 <dbcp.version>1.4</dbcp.version> 
 <spring.version>3.1.2.RELEASE</spring.version> 
</properties> 
 
<dependencies> 
 <dependency><!-- mybatis的幾個(gè)對(duì)象包裝成spring的bean --> 
  <groupId>org.mybatis</groupId> 
  <artifactId>mybatis-spring</artifactId> 
  <version>${mybatis.spring.version}</version> 
 </dependency> 
 <dependency><!-- spring上下文用spring-jdbc連接數(shù)據(jù)庫(kù) --> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-jdbc</artifactId> 
  <version>${spring.version}</version> 
 </dependency> 
 <dependency><!-- dataSource是BasicDataSource的實(shí)例 --> 
  <groupId>commons-dbcp</groupId> 
  <artifactId>commons-dbcp</artifactId> 
  <version>${dbcp.version}</version> 
 </dependency> 
 <dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-test</artifactId> 
  <version>${spring.version}</version> 
 </dependency> 
</dependencies> 

2. 在類路徑下創(chuàng)建beans-da.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" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><!-- 數(shù)據(jù)庫(kù)連接bean --> 
  <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
  <property name="url" value="jdbc:mysql://localhost:3306/hbatis?characterEncoding=utf8" /> 
  <property name="username" value="root" /> 
  <property name="password" value="123456" /> 
 </bean> 
  
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- sqlSessionFactory bean --> 
  <property name="dataSource" ref="dataSource" /><!-- 數(shù)據(jù)源 --> 
  <property name="configLocation" value="classpath:Configuration.xml" /><!-- 配置文件 --> 
 </bean> 
  
 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"><!-- user映射bean--> 
  <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
  <property name="mapperInterface" value="com.john.hbatis.mapper.IUserMapper" /><!-- 映射接口 --> 
 </bean> 
</beans> 

3. 測(cè)試類:

@ContextConfiguration(locations = { "classpath:beans-da.xml" }) 
public class SpringIntegrationTest extends AbstractTestNGSpringContextTests { 
  private static final Logger log = LoggerFactory.getLogger(SpringIntegrationTest.class); 
   
  @Resource 
  IUserMapper mapper; 
   
  @Test 
  public void queryTest() { 
    User user = mapper.getUserById(1); 
    log.info("Name: {}, address: {}", user.getName(), user.getAddress()); 
  } 
} 

與SpringMVC集成
這里我們建立在與Spring3集成基礎(chǔ)上來講:
1. 往pom.xml添加SpringMVC和Freemarker依賴:

<properties> 
 <freemarker.version>2.3.19</freemarker.version> 
 <servlet.version>2.5</servlet.version> 
</properties> 
 
<dependency>  
 <groupId>org.freemarker</groupId>  
 <artifactId>freemarker</artifactId>  
 <version>${freemarker.version}</version>  
</dependency> 
<dependency> 
 <groupId>javax.servlet</groupId>  
 <artifactId>servlet-api</artifactId>  
 <version>${servlet.version}</version> 
 <scope>provided</scope> 
</dependency> 

2. 在web.xml中加入Spring的監(jiān)聽器和SpringMVC的servlet:

<listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class><!-- 監(jiān)聽容器事件,初始化和關(guān)閉Web應(yīng)用上下文并調(diào)用ContextCleanupListener清理資源 --> 
</listener> 
<listener> 
 <listener-class>org.springframework.web.context.ContextCleanupListener</listener-class><!-- Web應(yīng)用關(guān)閉時(shí),清理ServletContext中spring相關(guān)的可銷毀資源 --> 
</listener> 
 
<servlet> 
 <servlet-name>hbatis</servlet-name> 
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 <!--<init-param> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/hbatis-servlet.xml</param-value> 
 </init-param>--><!-- 未配置時(shí),SpringMVC會(huì)到WEB-INF目錄下找${project.name}-servlet.xml --> 
 <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
 <servlet-name>hbatis</servlet-name> 
 <url-pattern>*.htm</url-pattern> 
</servlet-mapping> 

3. 在WEB-INF下新建:

Spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 
 
 <context:property-placeholder location="classpath:/database.properties" /><!-- 數(shù)據(jù)庫(kù)配置文件 --> 
 
 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
  p:driverClassName="${driverClassName}" 
  p:url="${url}" 
  p:username="${user_name}" 
  p:password="${password}" /><!-- 數(shù)據(jù)源配置 --> 
  
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- sqlSessionFactory對(duì)象 --> 
  <property name="dataSource" ref="dataSource" /><!-- 數(shù)據(jù)源 --> 
  <property name="configLocation" value="classpath:Configuration.xml" /><!-- myBatis配置文件 --> 
  <!--<property name="mapperLocations" value="classpath*:com/john/hbatis/model/*.xml" />--><!-- 可以在Configuration.xml或此處配置映射文件,但其中不能有相同id的parameterMap, resultMap或sql等 --> 
 </bean> 
  
 <bean id="mapperConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 掃描指定包以獲取映射器 --> 
  <property name="basePackage" value="com.john.hbatis.mapper" /> 
 </bean> 
</beans> 

類路徑下的database.properties:

driverClassName=com.mysql.jdbc.Driver 
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8 
user_name=root 
password=123456 


注:因?yàn)镸apperScannerConfigurer可能會(huì)導(dǎo)致username取的是系統(tǒng)用戶的賬號(hào),而造成數(shù)據(jù)庫(kù)連接失敗,所以改成其它值:user_name。

SpringMVC配置文件hbatis-servlet.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: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.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
 
 <mvc:annotation-driven /><!-- 注冊(cè)RequestMappingHandlerMapping, RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver以提供對(duì)@RequestMapping,@ExceptionHandler等注解的支持 --> 
 
 <context:component-scan base-package="com.john.hbatis.controller" /><!-- 掃描控制器包下有特定注解的類,并實(shí)例化和依賴注入 --> 
 
 <!-- FreeMarker視圖處理器 --> 
 <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
  <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>  
  <property name="contentType" value="text/html;charset=utf-8"/>  
  <property name="prefix" value="" />  
  <property name="cache" value="false"/>  
  <property name="viewNames">  
    <array>  
      <value>*.ftl</value>  
    </array>  
  </property>  
  <!--<property name="suffix" value=".ftl"/>-->  
  <property name="order" value="0"/><!-- 優(yōu)先級(jí),數(shù)值越小優(yōu)先級(jí)越高 -->  
 </bean> 
 
 <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
  <property name="templateLoaderPaths">  
    <list>  
      <value>/WEB-INF/ftl/</value><!-- 模板加載路徑 -->  
    </list>  
  </property>  
 </bean> 
</beans> 

4. MVC:
控制層:UserController.java

@Controller 
@RequestMapping("/article") 
public class UserController { 
 
  @Autowired 
  IUserMapper mapper; 
 
  @RequestMapping("/list") 
  public String showAll(ModelMap modelMap) { 
    List<Article> articles = mapper.getArticlesByUserId(1); 
    modelMap.addAttribute("articles", articles); 
    return "main.ftl"; 
  } 
} 

視圖層:main.ftl:

<#list articles as article> 
  <div>${article.id}. ${article.title}: ${article.content}</div> 
</#list> 

5. 啟動(dòng)工程,瀏覽器輸入:http://localhost:8080/hbatis/article/list.htm查看結(jié)果。

相關(guān)文章

  • Java方法及數(shù)組相關(guān)原理解析

    Java方法及數(shù)組相關(guān)原理解析

    這篇文章主要介紹了Java方法及數(shù)組相關(guān)原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析

    springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析

    這篇文章主要介紹了springboot中自定義異常以及定制異常界面實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • java  異常詳解及應(yīng)用實(shí)例

    java 異常詳解及應(yīng)用實(shí)例

    這篇文章主要介紹了java 異常詳解及應(yīng)用實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Java?awt-對(duì)話框簡(jiǎn)單實(shí)現(xiàn)方式

    Java?awt-對(duì)話框簡(jiǎn)單實(shí)現(xiàn)方式

    這篇文章主要介紹了Java?awt-對(duì)話框簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java位掩碼控制權(quán)限與(&)或(|)非(~)、>的介紹

    Java位掩碼控制權(quán)限與(&)或(|)非(~)、>的介紹

    今天小編就為大家分享一篇關(guān)于Java位掩碼控制權(quán)限與(&)或(|)非(~)、>的介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • SpringMVC+Spring+Mybatis實(shí)現(xiàn)支付寶支付功能的示例代碼

    SpringMVC+Spring+Mybatis實(shí)現(xiàn)支付寶支付功能的示例代碼

    這篇文章主要介紹了SpringMVC+Spring+Mybatis實(shí)現(xiàn)支付寶支付功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Java中如何檢查數(shù)組是否包含某整數(shù)

    Java中如何檢查數(shù)組是否包含某整數(shù)

    這篇文章主要介紹了在?Java?中檢查數(shù)組是否包含某整數(shù),在本文中,我們使用了幾個(gè)內(nèi)置的方法,如anyMatch()、contains()、binarySearch()等,我們將在給定的數(shù)組中找到一個(gè)值,結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Spring?Cloud?Loadbalancer服務(wù)均衡負(fù)載器詳解

    Spring?Cloud?Loadbalancer服務(wù)均衡負(fù)載器詳解

    這篇文章主要介紹了Spring?Cloud?Loadbalancer服務(wù)均衡負(fù)載器,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • java并發(fā)編程包JUC線程同步CyclicBarrier語(yǔ)法示例

    java并發(fā)編程包JUC線程同步CyclicBarrier語(yǔ)法示例

    這篇文章主要為大家介紹了java并發(fā)編程工具包JUC線程同步CyclicBarrier語(yǔ)法使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • Java使用Deque實(shí)現(xiàn)堆棧的方法

    Java使用Deque實(shí)現(xiàn)堆棧的方法

    這篇文章主要介紹了Java使用Deque實(shí)現(xiàn)堆棧的方法,實(shí)例分析了java簡(jiǎn)單實(shí)現(xiàn)堆棧的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07

最新評(píng)論

井冈山市| 孟州市| 额尔古纳市| 乌兰察布市| 广灵县| 花垣县| 陇川县| 措美县| 南丹县| 石渠县| 正安县| 佛冈县| 邵阳县| 连云港市| 兴义市| 曲麻莱县| 三原县| 岗巴县| 昭觉县| 宁都县| 满洲里市| 彰化县| 龙山县| 长治市| 壤塘县| 崇左市| 长海县| 兴和县| 宜宾市| 新巴尔虎右旗| 从化市| 吕梁市| 江安县| 页游| 合川市| 蚌埠市| 维西| 宜黄县| 伽师县| 勃利县| 江陵县|