mybatis和spring整合

第一步:导入包
aopalliance-1.0.jar
aspectjweaver-1.8.13.jar
commons-dbcp-1.4.jar
commons-logging-1.2.jar
commons-pool-1.5.4.jar
hamcrest-core-1.3.jar
junit-4.12.jar
log4j-1.2.16.jar
mybatis-3.4.1.jar
mybatis-spring-1.3.2.jar
mysql-connector-java-5.1.6.jar
spring-aop-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-tx-4.2.5.RELEASE.jar

第二步:配置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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       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.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">
       
        <!--引入属性文件,推荐用法-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>classpath:zscbbs.properties</value>
        </property>
    </bean>
	<!--引入xml文件-->
    <!--<import resource="mybatis-spring.xml"/>-->
    <!--引入属性文件-->
    <!--<context:property-placeholder location="zscbbs.properties"/>-->


    <!--数据源,所有项目都可以用-->
    <!--通过JNDI从服务器容器中获取dataSource资源,在服务器环境中配置JNDI数据源,在Spring配置文件引用-->
    <!--<bean id="dataSourceTwo"  class="org.springframework.jndi.JndiObjectFactoryBean" >-->
         <!--<property name="jndiName" >-->
            <!--&lt;!&ndash;java:comp/env:固定写法,/jdbcMysql:服务器context.xml配置文件里面数据源配置名称&ndash;&gt;-->
            <!--<value>java:comp/env/jdbcMysql</value>-->
         <!--</property>-->
    <!--</bean>-->
    
    <!--dbcp数据源,只对当前项目有用-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    
	<!--配置SqlSessionFactoryBean-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--引入数据源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--引入mybatis-config.xml文件,可以只把里面内容放过来,也可以引入文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!--引入mapper.xml文件,可以多写,也可以写通配符-->
        <property name="mapperLocations">
            <list>
                <value>classpath:cn/kgc/mappers/detailMapper.xml</value>
            </list>
        </property>
    </bean>
    
    <!--配置SqlSessionTemplate,采用下面写法可以省略-->
    <!--<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
    </bean>
	<!--创建DAO层的实现类的bean,并注入SqlSessionTemplate属性,采用下面写法可以省略-->
    <bean id="detailImpl" class="cn.kgc.dao.impl.DetailDaoImpl">
        <property name="sqlSess" ref="sqlSessionTemplate"></property>
    </bean>-->
    
    <!--采用数据映射器MapperFactoryBean完成对数据库的操作,就可以不用上面二步,mapperInterface只能是接口类型,可以不用写接口实现,但多个接口就需要配置多个,比较麻烦,可以省略,所以推荐下面的写法-->
    <!--<bean id="detailDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="cn.kgc.dao.DetailDao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"></property>
    </bean>-->

	<!--自动扫描指定包下的mapper接口,并将它们直接注册为 数据映射器MapperFactoryBean,就不用每个接口都单独配置,id默认为首字母小写,可以不用写接口实现-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.kgc.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

	<!--指定扫描注解定义的包,因为DAO层是自动实现,所以不用注解不用新建bean了,只需对service和pojo层aop层注解,实现loc新建bean的处理-->
    <context:component-scan base-package="com.big3.service,com.big3.pojo,com.big3.aop"/>

    <!--注册Aspectj代理对象,AOP切面增强用,注解方式-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 </beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值