makejavas / EasyCode

基于IntelliJ IDEA开发的代码生成插件,支持自定义任意模板(Java,html,js,xml)。只要是与数据库相关的代码都可以通过自定义模板来生成。支持数据库类型与java类型映射关系配置。支持同时生成生成多张表的代码。每张表有独立的配置信息。完全的个性化定义,规则由你设置。
MIT License
1.03k stars 327 forks source link

实体类为条件进行查询异常 #94

Open ZuoxianZhifeng opened 2 years ago

ZuoxianZhifeng commented 2 years ago

用插件的Default模板,在以实体类为条件进行查询时,出现参数异常

插件版本:1.2.6

Mybatis版本:3.5.9

代码如下

实体类:

public class User implements Serializable {
    private static final long serialVersionUID = 240653121897900253L;

    private Integer uid;

    private String username;

    private String password;

    private String level;

    ......
}

dao:

/**
     * 查询指定行数据
     *
     * @param user 查询条件
     * @param pageable         分页对象
     * @return 对象列表
     */
    List<User> queryAllByLimit(User user, @Param("pageable") Pageable pageable);

xml:

<!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="UserMap">
        select
          uid, username, password, level
        from user
        <where>
            <if test="uid != null">
                and uid = #{uid}
            </if>
            <if test="username != null and username != ''">
                and username = #{username}
            </if>
            <if test="password != null and password != ''">
                and password = #{password}
            </if>
            <if test="level != null and level != ''">
                and level = #{level}
            </if>
        </where>
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

测试代码:

User user = new User();
user.setUsername("www");
userService.queryByPage(user, new PageRequest(1,100));

报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'uid' not found. Available parameters are [arg0, pageable, param1, param2]

怀疑是dao中同时传入了user和pageable导致无法识别

如下修改解决问题

  1. 给dao.java的实体类加Param注解List<User> queryAllByLimit(@Param("user") User user, @Param("pageable") Pageable pageable);

  2. 在dao.xml中修改,使用【引用.属性名】的方式访问,如:

    <where>
    <if test="user.uid != null">
        and uid = #{user.uid}
    </if>
    
       ......
    </where>

项目使用的是ssm框架,不知道是不是我调用出现了问题?

eqianyu commented 1 year ago

我也遇到了