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
  • 相关阅读:
    codeforces-1328F-Make k Equal
    codeforces-1327C-Game with Chips
    codeforces-1328E-Tree Queries
    深度学习(九):变分自编码器
    深度学习(八):概率生成模型
    深度学习(六):吉布斯采样
    深度学习(五):M-H采样
    深度学习(四):马尔科夫链蒙特卡洛采样(MCMC)
    深度学习(二):图模型的学习
    深度学习(一):概率图模型引入
  • 原文地址:https://www.cnblogs.com/1138720556Gary/p/11988160.html
Copyright © 2011-2022 走看看