基于XML + Annotation的MyBatis配置

基于XML + Annotation的MyBatis配置,第1张

基于XML + Annotation的MyBatis配置

可能同时具有基于XML + Annotation的配置

对于基于 xml 的配置:

从Spring上下文文件开始。在上下文文件中添加以下行

<beans:bean id="dataSource"      p:driverClassName="yourDriverClassName"   p:url="yourUrl"   p:username="yourUsername"   p:password="yourPassword" /><beans:bean id="sqlSessionFactory" >    <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" >    <beans:property name="sqlSessionFactory" ref="sqlSessionFactory" /></beans:bean>

您的mybatis-config.xml应该是这样的:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC "-//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可能是这样的

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//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层中,您可能会拥有如下所示的类:

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;    }}

现在,要使用基于 注释 的配置,请添加以下内容:

您可能具有如下界面:

public interface MyMapper {    @Select(value="select something from some_table")    public String getSomevalue(@Param("someParam") String someParam);}

现在将以下内容添加到您的上下文文件中:

<beans:bean id="annotatedMapper" >  <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>

希望这可以帮助 :)



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5601507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存