用习惯了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>
未经允许请勿转载:程序喵 » 【SSM】Mybatis进行id类型、String类型、map类型、ben类型参数传入Sql查询