码迷,mamicode.com
首页 > 数据库 > 详细

Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询

时间:2015-01-20 15:40:50      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:mybatis   传参   

用习惯了Hibernate,再换成Mybatis总会遇到一些变态问题,正如下面的错误提示,用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 错误

There is no getter for property named 'moduleCode' in 'class java.lang.String  
Dao层调用方式

/*
 *  Dao层查询 
 */
@Override
public List<CityFace> findCityFaceByCondition(String eqDate) {
	return sqlSession.selectList(CityFace.class.getName()+"_Mapper.findCityFaceByCondition",eqDate); 
}
常见的错误写法:XML映射Sql

<select id="findCityFaceByCondition" parameterType="String" resultType="CityFace">
	select * from (select unitname, to_char(rdate,'yyyy-MM') rdate,analysistype, scope from CITYSCAPEANALYSIS) 
	pivot (sum(scope) for analysistype in ('ESScope' esscope, 'EOScope' eoscope,'GCScope' gcscope, 'CScope' cscope))  
	<if test='eqDate!=""'>
		where rdate = #{eqDate}
	</if>
</select>
需要修改成: 

<select id="findCityFaceByCondition" parameterType="String" resultType="CityFace">
	select * from (select unitname, to_char(rdate,'yyyy-MM') rdate,analysistype, scope from CITYSCAPEANALYSIS) 
	pivot (sum(scope) for analysistype in ('ESScope' esscope, 'EOScope' eoscope,'GCScope' gcscope, 'CScope' cscope))  
	<if test='_parameter!=""'>
		where rdate = #{_parameter}
	</if>
</select>
不管你的参数是什么,都要改成"_parameter",但是ID就不用改就行

Java调用ID示例

@Override
public Visitlogs findVisitlogsById(int id) {
	return sqlSession.selectOne(Visitlogs.class.getName()+"_Mapper.findVisitlogsById", id);
}
xml映射

<!-- 根据id查询用户 -->
<select id="findVisitlogsById" parameterType="int" resultMap="resultVisitlogs">
	select * from visitlogs where id=#{id}
</select>
———————————————————————————分割线————————————————————————————

上面说的是String单个值,如果是多个值就可以使用map对象和Bean类对象进行封装,然后就可以使用常用的方式接收了,比如下面的案例

Java调用Map示例

@Override
public AppUser loginUser(String userName, String password) {
	Map<String, String> map = new HashMap<String, String>();
	map.put("name", userName);
	map.put("password", password);
	return sqlSession.selectOne(AppUser.class.getName()+"_Mapper.loginUser", map);
}
xml映射

<!-- 登录用户 -->
<select id="loginUser" parameterType="map" resultType="AppUser">
	select * from app_user where name=#{name} and password=#{password}
</select>
Java调用Bean类示例

@Override
public AppUser loginUser(String userName, String password) {
	AppUser user = new AppUser();
	user.setName(userName);
	user.setPassword(password);
	return sqlSession.selectOne(AppUser.class.getName()+"_Mapper.loginUser", user);
}
xml映射

<!-- 登录用户 -->
<select id="loginUser" parameterType="AppUser" resultType="AppUser">
	select * from app_user where name=#{name} and password=#{password}
</select>




Java调用ID示例

@Override
public Visitlogs findVisitlogsById(int id) {
	return sqlSession.selectOne(Visitlogs.class.getName()+"_Mapper.findVisitlogsById", id);
}
xml映射

<!-- 根据id查询用户 -->
<select id="findVisitlogsById" parameterType="int" resultMap="resultVisitlogs">
	select * from visitlogs where id=#{id}
</select>

Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询

标签:mybatis   传参   

原文地址:http://blog.csdn.net/xiaokui_wingfly/article/details/42918029

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!