欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis SpringMVC整合實(shí)現(xiàn)步驟詳解

 更新時(shí)間:2020年08月27日 11:34:55   作者:IT-執(zhí)念  
這篇文章主要介紹了MyBatis SpringMVC整合實(shí)現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、添加spring相關(guān)jar包

2、配置ehcache jar包。

3、添加ehcache mybatis 適配器jar包(在mybatis官網(wǎng))

4、添加spring mybatis 適配器jar包(在mybatis)官網(wǎng)

5、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ntjr.mybatisSpring</groupId>
  <artifactId>mybatisSpring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
    <mybatis_version>3.4.2</mybatis_version>
    <mysql_connector_version>5.1.38</mysql_connector_version>
    <org.springframework>4.3.7.RELEASE</org.springframework>
  </properties>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>${mybatis_version}</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>${mysql_connector_version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${org.springframework}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${org.springframework}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${org.springframework}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>${org.springframework}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance -->
    <dependency>
      <groupId>org.aopalliance</groupId>
      <artifactId>com.springsource.org.aopalliance</artifactId>
      <version>1.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/net.sourceforge.cglib/com.springsource.net.sf.cglib -->
    <dependency>
      <groupId>net.sourceforge.cglib</groupId>
      <artifactId>com.springsource.net.sf.cglib</artifactId>
      <version>2.2.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/com.springsource.org.aspectj.weaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>com.springsource.org.aspectj.weaver</artifactId>
      <version>1.6.10.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
    <dependency>
      <groupId>com.mchange</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.5.2</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis.caches</groupId>
      <artifactId>mybatis-ehcache</artifactId>
      <version>1.1.0</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-impl</artifactId>
      <version>1.2.5</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.taglibs/taglibs-standard-impl -->
    <dependency>
      <groupId>org.apache.taglibs</groupId>
      <artifactId>taglibs-standard-spec</artifactId>
      <version>1.2.5</version>
    </dependency>
  </dependencies>
</project>

6、applicationContext.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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

  <!-- Spring希望管理所有的業(yè)務(wù)邏輯組件,等。。。 -->
  <context:component-scan base-package="com.atguigu.mybatis">
    <context:exclude-filter type="annotation"
      expression="org.springframework.stereotype.Controller" />
  </context:component-scan>

  <!-- 引入數(shù)據(jù)庫的配置文件 -->
  <context:property-placeholder location="classpath:dbconfig.properties" />
  <!-- Spring用來控制業(yè)務(wù)邏輯。數(shù)據(jù)源、事務(wù)控制、aop -->
  <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
  </bean>
  <!-- spring事務(wù)管理 -->
  <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>

  <!-- 開啟基于注解的事務(wù) -->
  <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
  
  <!-- 
  整合mybatis 
    目的:1、spring管理所有組件。mapper的實(shí)現(xiàn)類。
        service==>Dao  @Autowired:自動(dòng)注入mapper;
      2、spring用來管理事務(wù),spring聲明式事務(wù)
  -->
  <!--創(chuàng)建出SqlSessionFactory對象 -->
  <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <!-- configLocation指定全局配置文件的位置 -->
    <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    <!--mapperLocations: 指定mapper文件的位置-->
    <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"></property>
  </bean>
  
  <!--配置一個(gè)可以進(jìn)行批量執(zhí)行的sqlSession -->
  <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactoryBean"></constructor-arg>
    <constructor-arg name="executorType" value="BATCH"></constructor-arg>
  </bean>
  
  <!-- 掃描所有的mapper接口的實(shí)現(xiàn),讓這些mapper能夠自動(dòng)注入;
  base-package:指定mapper接口的包名
   -->
  <mybatis-spring:scan base-package="com.atguigu.mybatis.dao"/>
  <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.atguigu.mybatis.dao"></property>
  </bean> -->
  
</beans>

7、mybatis-config.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>
  <settings>
    <setting name="mapUnderscoreToCamelCase" value="true" />
    <setting name="jdbcTypeForNull" value="NULL" />

    <!--顯式的指定每個(gè)我們需要更改的配置的值,即使他是默認(rèn)的。防止版本更新帶來的問題 -->
    <setting name="cacheEnabled" value="true" />
    <setting name="lazyLoadingEnabled" value="true" />
    <setting name="aggressiveLazyLoading" value="false" />
  </settings>

  <databaseIdProvider type="DB_VENDOR">
    <property name="MySQL" value="mysql" />
    <property name="Oracle" value="oracle" />
    <property name="SQL Server" value="sqlserver" />
  </databaseIdProvider>

</configuration>

注意:此處mybatis配置文件中沒有配置數(shù)據(jù)源,數(shù)據(jù)源由spring負(fù)責(zé)。

此外還需要ehcache.xml配置文件,Mapper.xml文件。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何使用Spring MVC的消息轉(zhuǎn)換器設(shè)置日期格式

    如何使用Spring MVC的消息轉(zhuǎn)換器設(shè)置日期格式

    這篇文章主要介紹了如何使用Spring MVC的消息轉(zhuǎn)換器設(shè)置日期格式,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • CGLIB代理的使用與原理解析

    CGLIB代理的使用與原理解析

    這篇文章主要介紹了CGLIB代理的使用與原理解析,靜態(tài)代理和JDK 代理模式都要求目標(biāo)對象是實(shí)現(xiàn)一個(gè)接口,但是有時(shí)候目標(biāo)對象只是一個(gè)單獨(dú)的對象,并沒有實(shí)現(xiàn)任何的接口,這個(gè)時(shí)候可使用目標(biāo)對象子類來實(shí)現(xiàn)代理,這就是Cglib代理,需要的朋友可以參考下
    2023-09-09
  • java反射的作用知識點(diǎn)總結(jié)

    java反射的作用知識點(diǎn)總結(jié)

    在本篇文章里小編給大家整理的是關(guān)于java反射的作用知識點(diǎn)總結(jié),需要的朋友們可以學(xué)習(xí)下。
    2020-02-02
  • 使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    使用IntelliJ IDEA調(diào)式Stream流的方法步驟

    本文主要介紹了使用IntelliJ IDEA調(diào)式Stream流的方法步驟,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • java中的hashCode方法小例子

    java中的hashCode方法小例子

    這篇文章主要介紹了java中的hashCode方法小例子,有需要的朋友可以參考一下
    2013-12-12
  • 使用springboot開發(fā)的第一個(gè)web入門程序的實(shí)現(xiàn)

    使用springboot開發(fā)的第一個(gè)web入門程序的實(shí)現(xiàn)

    這篇文章主要介紹了使用springboot開發(fā)的第一個(gè)web入門程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Java基礎(chǔ)教程之組合(composition)

    Java基礎(chǔ)教程之組合(composition)

    這篇文章主要介紹了Java基礎(chǔ)教程之組合(composition),組合是在Java中實(shí)現(xiàn)程序復(fù)用(reusibility)的基本手段之一,需要的朋友可以參考下
    2014-08-08
  • springboot對壓縮請求的處理方法

    springboot對壓縮請求的處理方法

    這篇文章主要介紹了springboot對壓縮請求的處理,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • Java類型轉(zhuǎn)換valueOf與parseInt區(qū)別探討解析

    Java類型轉(zhuǎn)換valueOf與parseInt區(qū)別探討解析

    這篇文章主要為大家介紹了Java類型轉(zhuǎn)換valueOf與parseInt區(qū)別探討解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • 分布式醫(yī)療掛號系統(tǒng)EasyExcel導(dǎo)入導(dǎo)出數(shù)據(jù)字典的使用

    分布式醫(yī)療掛號系統(tǒng)EasyExcel導(dǎo)入導(dǎo)出數(shù)據(jù)字典的使用

    這篇文章主要為大家介紹了分布式醫(yī)療掛號系統(tǒng)EasyExcel導(dǎo)入導(dǎo)出數(shù)據(jù)字典的使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04

最新評論