zoukankan      html  css  js  c++  java
  • mybatis和spring整合的关键配置

    spring配置文件 applicationContext.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    		http://www.springframework.org/schema/mvc 
    		http://www.springframework.org/schema/mvc/spring-mvc-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/aop 
    		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    		http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    	<!-- 加载配置文件 -->
    	<context:property-placeholder location="classpath:db.properties" />
    
    	<!-- 数据源,使用dbcp -->
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName" value="${jdbc.driver}" />
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    		<property name="maxActive" value="10" />
    		<property name="maxIdle" value="5" />
    	</bean>
    
    
    	<!-- sqlSessinFactory -->
    	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    		<!-- 加载mybatis的配置文件 -->
    		<property name="configLocation" value="mybatis/SqlMapConfig.xml" />
    		<!-- 数据源 -->
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    	
    	<!-- 原始dao接口 -->
    	
    	<bean id="userDao" class="cn.itcast.ssm.dao.UserDaoImpl">
    		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    	</bean>
    	
    	<!-- mapper配置 
    	MapperFactoryBean:根据mapper接口生成代理对象
    	-->
    	<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    		mapperInterface指定mapper接口
    		<property name="mapperInterface" value="cn.itcast.ssm.mapper.UserMapper"/>
    		<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    	</bean> -->
    	<!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 
    	遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
    	自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
    	-->
    	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<!-- 指定扫描的包名 
    		如果扫描多个包,每个包中间使用半角逗号分隔
    		-->
    		<property name="basePackage" value="cn.itcast.ssm.mapper"/>
    		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    		
    	</bean>
    	
    </beans>

    数据库db.properties文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/mybatis
    jdbc.username=root
    jdbc.password=mysql


    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>
    
    	<!-- 全局配置参数,需要时再设置 -->
    	<settings>
    		<!-- 打开延迟加载 的开关 -->
    		<setting name="lazyLoadingEnabled" value="true"/>
    		<!-- 将积极加载改为消极加载即按需要加载 -->
    		<setting name="aggressiveLazyLoading" value="false"/>
    		<!-- 开启二级缓存 -->
    		<setting name="cacheEnabled" value="true"/>
    	</settings>
    	
    	<!-- 别名定义 -->
    	<typeAliases>
    		
    		<!-- 针对单个别名定义
    		type:类型的路径
    		alias:别名
    		 -->
    		<!-- <typeAlias type="cn.itcast.mybatis.po.User" alias="user"/> -->
    		<!-- 批量别名定义 
    		指定包名,mybatis自动扫描包中的po类,自动定义别名,别名就是类名(首字母大写或小写都可以)
    		-->
    		<package name="cn.itcast.mybatis.po"/>
    		
    	</typeAliases>
    	
    	<!-- 加载 映射文件 -->
    	<mappers>
    		<mapper resource="sqlmap/User.xml"/>
    		
    		<!--通过resource方法一次加载一个映射文件 -->
    		<!-- <mapper resource="mapper/UserMapper.xml"/> -->
    		
    		<!-- 通过mapper接口加载单个 映射文件
    		遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录 中
    		上边规范的前提是:使用的是mapper代理方法
    		 -->
    		<!-- <mapper class="cn.itcast.mybatis.mapper.UserMapper"/> -->
    		
    		<!-- 批量加载mapper
    		指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载
    		遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录 中
    		上边规范的前提是:使用的是mapper代理方法
    		 -->
    		<package name="cn.itcast.mybatis.mapper"/>
    
    	</mappers>
    	
    </configuration>



  • 相关阅读:
    Robot Framework-资源文件的使用方法(7)
    Robot Framework-用户关键字的使用方法(6)
    robotframework 新建UI自动化测试用例实例一(2)
    robotframework--登录接口,post传递多个参数、及获取content中指定属性的值(5)
    robotframework基础知识(2)
    win7如何打开防火墙某个端口的tcp连接
    外观模式
    享元模式
    代理模式
    模板模式
  • 原文地址:https://www.cnblogs.com/reblue520/p/6239916.html
Copyright © 2011-2022 走看看