mybatis传入多个参数问题

先点个坑 封闭总结三--mybatis传入多个参数问题

(2011-11-26

Mybatis

前一段时间项目使用mybatis操作时,一直使用的对象映射的方法,这样就导致有新的业务对象处理时,就必须新建一个类相对应。这样就似乎和Hibenate一样了。由于昨天我从网上查找一次输入多参数的方法,发现有两种解决方案,一种是传MAP对象,一种就定义接口。我选择了后一种方法。现在记录下来,结论基本没经过证实,只是个人观点,欢迎指正!

定义接口实现多参数访问是mybatis一种反射机制,依托spring注入。具体步骤:

1.新建接口类:

public interface AlipayMapper {

int insertOperation(@Param("operateid")String operateid, @Param("userid")String userid);

}

接口随便定义,在下一步的XML文件中有映射,在注入文件中也有扫描的配置,先在这记一下

2.创建xml配置文件

<mapper namespace="persistence.AlipayMapper">

  <insert id="insertOperation" >

    insert into test_table(OPERATE_ID, OPERATE_TIME, USER_ID)

     VALUES (#{operateid}, sysdate,  #{userid}) 

  </insert>

</mapper>

注意配置文件中红色字体部分,这个要指向第一步中定义的接口类,并且配置文件命和接口名尽量一致,不一致的情况我没试

3.定义SERVICE并在service中注入Mapper接口

@Service

public class AlipayServiceEx {

@Autowired

private AlipayMapper alipayMapper;

@PreAuthorize("hasAuthority('ROLE_USER')")

public int addMoneyOperation(String operateid, String userid)

{

return alipayMapper.insertOperation(operateid, userid);

}

}

4.在IBATIS配置文件中,加入以下东东

        <!-- enable component scanning (beware that this does not enable mapper scanning!) -->    

    <context:component-scan base-package="config.ibatis" />

    

    <!-- scan for mappers and let them be autowired -->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.persistence" />

    </bean>

5.将第二步定义的XML文件引入到SqlSessionFactoryBean的文件中

大体就这样,其余就是SPRING相关的注入等等了。

猜你喜欢

转载自blog.csdn.net/Golden_soft/article/details/81228236