好不容易整合了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>