【笔记】Spring的查询功能

Spring 的查询(导入相关包)

public class Main {
    public void mysqlAction(){
        DriverManagerDataSource dmd = new DriverManagerDataSource();
        dmd.setDriverClassName("com.mysql.jdbc.Driver");
        dmd.setUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8");
        dmd.setUsername("root");
        dmd.setPassword("123456");
        JdbcTemplate jt = new JdbcTemplate(dmd);
        String sqlAll = "select * from user";
        List lt = jt.query(sqlAll, new mymap());
        for(person p:lt){
            if(p.getName().equals("张三")){
                System.out.println("234567234567");
                System.out.println(p);
            }
        }       
    }

写一个集成 RowMapper的类

class mymap implements RowMapper{

        public person mapRow(ResultSet rs, int arg1) throws SQLException {
            // TODO Auto-generated method stub
            String name = rs.getString(1);
            String age = rs.getString(2);
            person p = new person();
            p.setName(name);
            p.setAge(age);
            return p;
        }
        
    }

person类

public class person {
    private String name;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "person [age=" + age + ", name=" + name + "]";
    }
    
}

遇到的问题:数据库查询中文的时候一直无法查询;
解决办法:在地址链接的地方添加?useUnicode=true&characterEncoding=utf8

你可能感兴趣的:(【笔记】Spring的查询功能)