spring和mybatis整合的问题

好不容易整合了spring和mybaits框架,之前一直有问题,现在把几个地方脱坑的注意一下:


mybaits.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>  
 
     
    <!-- 为JAVA Bean起类别名 -->  
    <typeAliases> 
        <typeAlias alias="User" type="com.maping.User"/> 
        <typeAlias alias="Group_base" type="com.x52im.rainbowchat.endity.Group_base"/> 
        <typeAlias alias="Group_desgion" type="com.x52im.rainbowchat.endity.Group_desgion"/> 
        <typeAlias alias="Group_Msg" type="com.x52im.rainbowchat.endity.Group_Msg"/> 
        <typeAlias alias="OffLine_Msg" type="com.x52im.rainbowchat.endity.OffLine_Msg"/> 
    </typeAliases>  
     
    
     
    <!-- 映射xml文件 -->
    <mappers>
        <mapper resource="com/maping/User.xml"/>
        <mapper resource="com/maping/group_base.xml"/>
        <mapper resource="com/maping/group_desgion.xml"/>
        <mapper resource="com/maping/t_group_msgs.xml"/>
        <mapper resource="com/maping/t_offline_msgs.xml"/>
    </mappers> 

</configuration>


这里面映射xml和实体必须这样映射,之前写法一直有问题,用了不少时间。


spring装载实体bean,直接通过映射来操作对象接口:

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"  
            default-autowire="byName" default-lazy-init="false"> 
    
    <context:property-placeholder location="classpath:c3p0.properties"/>
    
  <!--本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。 --> 
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
     <property name="driverClassName" value="${c3p0.driverClass}"/> 
     <property name="url" value="${c3p0.jdbcUrl}"/> 
     <property name="username" value="${c3p0.user}"/> 
     <property name="password" value="${c3p0.password}"/> 
  </bean> 
 
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
     <!--dataSource属性指定要用到的连接池--> 
     <property name="dataSource" ref="dataSource"/> 
     <!--configLocation属性指定mybatis的核心配置文件--> 
     <property name="configLocation" value="mybatis.xml"/> 
  </bean> 
 
  
  <!--==================================实体的bean配置======================================-->
  <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例--> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
     <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象--> 
     <property name="mapperInterface" value="com.map.intefacedata.IUserOperation" /> 
  </bean> 
  <bean id="groupMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
     <property name="mapperInterface" value="com.map.intefacedata.IGroup_Base" /> 
  </bean>
  <bean id="desgionMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
     <property name="mapperInterface" value="com.map.intefacedata.IGroup_Desgion" /> 
  </bean>
  <bean id="groupmsgMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
     <property name="mapperInterface" value="com.map.intefacedata.IGroup_Msgs" /> 
  </bean>
  <bean id="offlinemsgMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
     <property name="sqlSessionFactory" ref="sqlSessionFactory" /> 
     <property name="mapperInterface" value="com.map.intefacedata.IOffline_Msgs" /> 
  </bean>
 
</beans> 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值