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

来源:互联网 发布:windows脚本编程 编辑:程序博客网 时间:2024/05/21 04:40
 

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


查看原文:http://www.ibloger.net/article/285.html


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

[java] view plain copy
 print?
  1. There is no getter for property named 'moduleCode' in 'class java.lang.String    
Dao层调用方式

[java] view plain copy
 print?
  1. /* 
  2.  *  Dao层查询  
  3.  */  
  4. @Override  
  5. public List<CityFace> findCityFaceByCondition(String eqDate) {  
  6.     return sqlSession.selectList(CityFace.class.getName()+"_Mapper.findCityFaceByCondition",eqDate);   
  7. }  
常见的错误写法:XML映射Sql

[html] view plain copy
 print?
  1. <select id="findCityFaceByCondition" parameterType="String" resultType="CityFace">  
  2.     select * from (select unitname, to_char(rdate,'yyyy-MM') rdate,analysistype, scope from CITYSCAPEANALYSIS)   
  3.     pivot (sum(scope) for analysistype in ('ESScope' esscope, 'EOScope' eoscope,'GCScope' gcscope, 'CScope' cscope))    
  4.     <if test='eqDate!=""'>  
  5.         where rdate = #{eqDate}  
  6.     </if>  
  7. </select>  
需要修改成: 

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

Java调用ID示例

[java] view plain copy
 print?
  1. @Override  
  2. public Visitlogs findVisitlogsById(int id) {  
  3.     return sqlSession.selectOne(Visitlogs.class.getName()+"_Mapper.findVisitlogsById", id);  
  4. }  
xml映射

[html] view plain copy
 print?
  1. <!-- 根据id查询用户 -->  
  2. <select id="findVisitlogsById" parameterType="int" resultMap="resultVisitlogs">  
  3.     select * from visitlogs where id=#{id}  
  4. </select>  
———————————————————————————分割线————————————————————————————

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

Java调用Map示例

[java] view plain copy
 print?
  1. @Override  
  2. public AppUser loginUser(String userName, String password) {  
  3.     Map<String, String> map = new HashMap<String, String>();  
  4.     map.put("name", userName);  
  5.     map.put("password", password);  
  6.     return sqlSession.selectOne(AppUser.class.getName()+"_Mapper.loginUser", map);  
  7. }  
xml映射

[html] view plain copy
 print?
  1. <!-- 登录用户 -->  
  2. <select id="loginUser" parameterType="map" resultType="AppUser">  
  3.     select * from app_user where name=#{name} and password=#{password}  
  4. </select>  
Java调用Bean类示例

[java] view plain copy
 print?
  1. @Override  
  2. public AppUser loginUser(String userName, String password) {  
  3.     AppUser user = new AppUser();  
  4.     user.setName(userName);  
  5.     user.setPassword(password);  
  6.     return sqlSession.selectOne(AppUser.class.getName()+"_Mapper.loginUser", user);  
  7. }  
xml映射

[html] view plain copy
 print?
  1. <!-- 登录用户 -->  
  2. <select id="loginUser" parameterType="AppUser" resultType="AppUser">  
  3.     select * from app_user where name=#{name} and password=#{password}  
  4. </select>