首页 > 编程语言 > 详细

Spring+Mybatis整合

时间:2019-11-04 20:55:13      阅读:88      评论:0      收藏:0      [点我收藏+]

进入十一月份,依旧没什么事可干,天天在工位上坐着,学点东西也记不住,浪费时间,浪费光阴。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>
View Code

 

第三步:建立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>
View Code

目录结构如此

技术分享图片

 

Spring+Mybatis整合

原文:https://www.cnblogs.com/houj/p/11794535.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!