Sql语句模糊查询有两种写法,一种是在jdbcTemplate的查询方法参数里拼接字符串%,一种是在Sql语句里拼接%字符串。

public class IsNameDaoImpl implements IsNameDao {
    JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<User> isname(String input1) {
        if (input1==null||input1==""){
            return null;
        }
        try { 
          /* 
           
            第一种写法 在参数里拼接
            String sql="select * from user where name like ?";
            List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), input1+"%");
            
            */
            
            //第二种写法,在sql语句里写
            /*这样写是不对的
            
              String sql="select * from user where name like  ?%";
            
            */
            
            //这种写法是对的
            String sql="select * from user where name like  ?\"%\" ";

            List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), input1);
            return users;
        } catch (DataAccessException e) {
            System.out.println("没查到");
           return null;
        }
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
猜你喜欢
  • 2021-10-07
  • 2021-10-16
  • 2021-06-19
  • 2022-12-23
  • 2021-09-27
  • 2022-12-23
相关资源
相似解决方案