问题描述
有什么方法可以使用Ibatis/MyBatis动态选择/更新/删除吗?
Is there any way to select/update/delete dynamically using Ibatis/MyBatis?
当我说动态"时,这意味着我根本不想创建任何POJO/DataMapper.
When I say "dynamically" it means I don't want to create any POJO/DataMapper at all.
任何URL示例都将受到欢迎.
Any URL example would be welcomed.
推荐答案
是的,只需将resultType
属性设置为map
,表数据将被放入列名称为值的HashMap中.如果查询返回的行多于1条,则映射的行将被放入列表中.如果要选择单个列,则可以仅获取该值(如String,int等)或作为列表.
Yes, just set the resultType
attribute to map
and the table data will be placed into a HashMap of column names to values. If the query returns more than 1 row, the mapped rows will be put into a List. If you want to select a single column, you can get just that value (as String, int, etc) or as a list.
<select id="test1" resultType="map">select * from user</select>
<select id="test2" resultType="map" parameterType="int">
select * from user where id=#{value}</select>
<select id="test3" resultType="string">select name from user</select>
...
// returns a list of maps
List test = sqlSession.selectList("test1");
// returns a single map
Object map = sqlSession.selectOne("test2", 0);
// returns a list of strings
List names = sqlSession.selectList("test3");
这适用于MyBatis 3;我认为您可以在iBatis 2中做类似的事情.
This applies to MyBatis 3; I think you can do something similar in iBatis 2.
这篇关于Ibatis/MyBatis动态选择,而无需创建任何Pojo/Mapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!