zhangruohong666 2019-07-04 17:58 采纳率: 60%
浏览 265
已采纳

Mybatis动态sql问题,希望有人能帮我解答

这是我写的sql语句

<select id="selectByDynamic" parameterType="Student" resultType="Student">
 select *from sudent where
 <if test="sno!=null">
  sno=#{sno}
 </if>
 <if test="sname!=null">
   and sname=#{sname}
 </if>
 </select>  

这是日志

DEBUG [main] (JdbcTransaction.java:136) - Opening JDBC Connection
DEBUG [main] (PooledDataSource.java:387) - Created connection 1526838264.
DEBUG [main] (JdbcTransaction.java:100) - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@5b01b3f8]
DEBUG [main] (BaseJdbcLogger.java:145) - ==>  Preparing: select *from sudent where sno=? and sname=? 
DEBUG [main] (BaseJdbcLogger.java:145) - ==> Parameters: 1414855328(String), 刘梅红(String)

这是代码,执行到添加姓名的地方就不执行了,我觉得应该是mapper的动态sql语句有问题,但是我刚学习这个不知道这个语句又什么问题

 public void testDynamic() throws Exception{
             SqlSessionFactory ssf=getSqlSessionFactory();
             SqlSession ss=ssf.openSession();
             StudentMapper cm=ss.getMapper(StudentMapper.class);
             Student student=new Student();
             student.setSno("1414855328");
             student.setSname("刘梅红");
          List<Student> listStudent=   cm.selectByDynamic(student);
          listStudent.forEach(x->System.out.println());
          ss.close();
  • 写回答

4条回答 默认 最新

  • Json-Huang 2019-07-04 20:57
    关注

    select *from sudent where 表名是不是写错了,感觉应该是student

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?