进入十一月份,依旧没什么事可干,天天在工位上坐着,学点东西也记不住,浪费时间,浪费光阴。Spring框架学到了后面,整合Mybatis,方式有很多。今天花了点时间整合起来,总结一下
按说应该mybatis-spring-1.2.3.jar这个包里 的类应该提供完全取代mybatis配置文件的功能,还没有探究过,我还是保留了mybatis配置文件。
第一步:导入所有所需的mybati的jar,导入spring的jar。最重要的导入mybatis-spring-1.2.3.jar
第二步:新建spring配置文件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:aop="http://www.springframework.org/schema/aop" 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-3.0.xsd 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"> <!-- <context:component-scan base-package="com.houjun.utils"></context:component-scan> --> <context:component-scan base-package="com.houjun.pojo" /><!--用以扫描注解--> <context:property-placeholder location="jdbc.properties" /><!--导入属性文件--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:sqlMapConfig.xml"></property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.houjun.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
第三步:建立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"/><!-- 牵一发动全身【专指延迟属性】 --> </settings> <typeAliases> <package name="com.houjun.pojo" /> </typeAliases> </configuration>
目录结构如此