spring jdbc 记录

 

 

@Repository("com.example.demo.dao.impl.SmpUserDaoImpl")
public class SmpUserDaoImpl implements SmpUserDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public SmpUserPo getByName(String name) {
        SmpUserPo userPo = new SmpUserPo();

        try {
            String sql = "select `user_id`, `user_name`, `login_password`, `pay_password`, `create_date`, `update_date` from smp_user where user_name = ? ";
            System.out.println(sql);
            userPo = jdbcTemplate.queryForObject(sql,
                    new Object[]{name},
                    new BeanPropertyRowMapper<SmpUserPo>(SmpUserPo.class));
        }catch (EmptyResultDataAccessException e){
            System.out.println("用户no data");
            return  null;
        }catch (Exception e){
            System.out.println("exxc");
            e.printStackTrace();
            return null;
        }

        return userPo;
    }

    @Override
    public void getAll() {
        String sql = "select `user_id`, `user_name`, `login_password`, `pay_password`, `create_date`, `update_date` from smp_user where '1' = ? ";
        System.out.println(sql);
        SmpUserPo userPo = new SmpUserPo();
        jdbcTemplate.query(sql,new Object[]{"1"},new ResultSetExtractor(){
            @Override
            public Object extractData(ResultSet rs) throws SQLException,DataAccessException {
                while(rs.next()) {
                    userPo.setUserId(rs.getLong("user_id"));
                    userPo.setUserName(rs.getString("user_name"));
                    userPo.setLoginPassword(rs.getString("login_password"));
                    userPo.setPayPassword(rs.getString("pay_password"));
                    userPo.setCreateDate(rs.getLong("create_date"));
                    userPo.setUpdateDate(rs.getLong("update_date"));

                    System.out.println(userPo.getUserName());
                }
                return null;
            }
        });

    }

}

 

posted on 2018-04-16 16:32  阮減显  阅读(99)  评论(0编辑  收藏  举报

导航