zoukankan      html  css  js  c++  java
  • JavaWeb_(Mybatis框架)MyBatis整合Spring框架

    MyBatis + Spring整合开发

    a)使用Spring容器用单例模式管理Mybatis的sqlSessionFactory;
    b)使用Spring管理连接池、数据源等;
    c)将Dao/Mapper动态代理对象注入到Spring容器中,使用时直接获取;

      

    一、MyBatis整合Spring框架

      a)导入所需的包;

      

      b)创建Mybatis主配置文件sqlMapConfig.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>
        
      <!-- 别名配置 -->
      <typeAliases>
          <!-- 别名直接按他的类名去取的User(不区别大小写) -->
          <package name="com.Gary.bean"/>
      </typeAliases>
    
    </configuration>
    sqlMapConfig.xml

      c)创建Spring主配置文件applicationContext.xml (以下是spring框架xml的头,需要导入约束);

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
    </beans>
    applicationContent.xml

      d)配置C3P0连接池;

        <!-- 配置c3p0连接池 -->
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
    
        <!-- 读取配置文件 -->
        <context:property-placeholder location="db.properties" />
    
        <!-- 配置c3p0连接池 -->
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    
    
    </beans>
    applicationContent.xml

      e)读取db.properties;

        <!-- 读取配置文件 -->
        <context:property-placeholder location="db.properties" />

      jdbc.driverClass=com.mysql.jdbc.Driver
      jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis
      jdbc.user=root
      jdbc.password=123456

    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
    
        <!-- 读取配置文件 -->
        <context:property-placeholder location="db.properties" />
    
        <!-- 配置c3p0连接池 -->
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
    
    
    </beans>
    applicationContent.xml
        jdbc.driverClass=com.mysql.jdbc.Driver
        jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_mybatis
        jdbc.user=root
        jdbc.password=123456
    db.properties

      f)配置sqlSessionFactory;

        <!-- 配置mybatis sqlSessionFactory -->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 配置数据源  -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 告诉spring mybatis的核心配置文件 -->
            <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        </bean>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:util="http://www.springframework.org/schema/util" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
    
        <!-- 读取配置文件 -->
        <context:property-placeholder location="db.properties" />
    
        <!-- 配置c3p0连接池 -->
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
            <property name="user" value="${jdbc.user}"/>
            <property name="password" value="${jdbc.password}"/>
        </bean>
    
        <!-- 配置mybatis sqlSessionFactory -->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 配置数据源  -->
            <property name="dataSource" ref="dataSource"/>
            <!-- 告诉spring mybatis的核心配置文件 -->
            <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
        </bean>
    
    
    
    
    
    
    
    </beans>
    applicationContent.xml

    # Global logging configuration
    log4j.rootLogger=DEBUG, stdout
    # Console output...
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    log4j.properties
    package com.Gary.test;
    
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
            
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContent.xml");
            
            SqlSessionFactoryBean bean = ac.getBean(SqlSessionFactoryBean.class);
            
            System.out.println(bean);
            
        }
        
    }
    Test.java
  • 相关阅读:
    ajaxpro.2.dll web.config配置
    .net中Cookie的用法
    js,cookies做悬浮购物车
    Request.UrlReferrer为空的解决
    repeater里添加序号的方法
    asp.net 母版页使用详解转
    js继承的几种实现方法
    .net用母版页和内容页实现动态Title(副标题_主标题)
    linux c数据库备份第一版
    关于sql多表去重问题
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/11988160.html
Copyright © 2011-2022 走看看