基于XML + Annotation的MyBatis配置 [英] XML + Annotation based configuration for MyBatis
问题描述
是否可以在应用程序中同时具有基于XML + Annotation的MyBatis配置.
Is it possible to have both the XML + Annotation based configuration for MyBatis in an application.
之所以这样问,是因为在我的应用程序中,我使用的是基于注释的方法. 但是在一种情况下,我需要使用IN子句,可以使用
The reason that I am asking this is because, in my application I am using the Annotation based approach. But in one of the scenarios , I require to use an IN clause, which can be built using the
<foreach></foreach>
基于XML的配置.
但是,当我启动应用程序时,它似乎无法识别基于注释的映射器,并给了我一个Type interface is not known to the MapperRegistry
异常
However , when I start my application , it does not seem to recognize my Annotation based mappers and gives me a Type interface is not known to the MapperRegistry
exception
因此,我想知道是否有可能在应用程序中同时具有MyBatis的基于XML + Annotation的配置. 请提出建议.
Therefore I would like to know whether is it possible to have both the XML + Annotation based configuration for MyBatis in an application. Please suggest.
推荐答案
可能同时具有基于XML +注释的配置
Its possible to have both XML + Annotation based configuration
对于基于 xml 的配置:
从您的spring上下文文件开始.在上下文文件中添加以下行
Start with your spring context file.In your context file add following lines
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="yourDriverClassName"
p:url="yourUrl"
p:username="yourUsername"
p:password="yourPassword" />
<beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</beans:bean>
<!-- assuming you have a dao named UserDao -->
<beans:bean id="userDao" class="com.yourcomp.dao.Userdao">
<beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
</beans:bean>
您的mybatis-config.xml应该是这样的:
your mybatis-config.xml should be something like this:
<?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>
</settings>
<typeAliases>
<typeAlias alias="User" type ="com.yourcomp.domain.User" />
</typeAliases>
<mappers>
<mapper resource="com/yourcomp/domain/UserMapper.xml"/>
<mapper resource="com/yourcomp/domain/AnotherDomainObjectMapper.xml"/>
</mappers>
</configuration>
和src/com/yourcomp/domain/中的userMapper.xml可能是这样的
and your userMapper.xml in src/com/yourcomp/domain/ might be something like this
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--<mapper namespace="org.pbl.rms.RMSUserDao">-->
<mapper namespace="com.yourcomp.domain.User">
<resultMap id="userMap" type="User">
<id property="userId" column="USER_ID" javaType="int" jdbcType="NUMERIC"/>
<result property="userName" column="USER_NAME" javaType="String" jdbcType="VARCHAR"/>
<result property="userFullName" column="USER_FULL_NAME" javaType="String" jdbcType="VARCHAR"/>
<result property="password" column="PASSWORD" javaType="String" jdbcType="VARCHAR"/>
<result property="passwordExpiryDate" column="PASWRD_EXPIRY_DATE" javaType="java.util.Date" jdbcType="DATE"/>
<result property="status" column="STATUS" javaType="_integer" jdbcType="DECIMAL"/>
</resultMap>
<select id="getUserById" parameterType="map" resultMap="userMap">
select * from user where USER_ID=#{userId}
</select>
</mapper>
现在在您的DAO层中,您可能会拥有如下所示的类:
now in your DAO layer you might have class like follows:
public class UserDAO{
private SqlSessionFactory sqlSessionFactory;
public UserDAO() {
}
public UserDAO(SqlSessionFactory sqlSessionFactory ) {
this.sqlSessionFactory = sqlSessionFactory;
}
public String getUserById(Integer userId) {
SqlSession session = sqlSessionFactory.openSession();
//int success = -100;
String name=null;
try {
name = (String)session.selectOne("com.yourcomp.domain.User.getUserById",userId);
}catch(Exception e){
}finally {
session.close();
}
return name;
}
}
现在,要使用基于注释的配置,请添加以下内容:
Now For using annotation based configuration add the followings:
您可能具有如下界面:
public interface MyMapper {
@Select(value="select something from some_table")
public String getSomeValue(@Param("someParam") String someParam);
}
现在将以下内容添加到您的上下文文件中:
now add the followings to your context file:
<beans:bean id="annotatedMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<beans:property name="mapperInterface" value="com.yourcomp.MyMapper" />
<!-- remember the sqlsession factory defined earlier for xml based conf ? -->
<beans:property name="sqlSessionFactory" ref="sqlSessionFactory" />
</beans:bean>
希望这会有所帮助:)
这篇关于基于XML + Annotation的MyBatis配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!