java 之Spring jdbcTemlate

Spring JDBC    1.update():执行DML语句。增、删、改语句   

                         2.queryForMap():             3.queryForList():        4.query(): 最常用                5.queryForObject

Spring JDBC

    1.Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
    * 步骤: 1. 导入jar包    2. 创建JdbcTemplate对象。依赖于数据源DataSource

        2. 创建JdbcTemplate对象。      依赖于数据源   DataSource   就是数据库链接池
            * JdbcTemplate template = new JdbcTemplate(ds);

        3. 调用JdbcTemplate的方法来完成CRUD的操作


1.update():执行DML语句。增、删、改语句

        JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource()); 工具类的获取池子方法
        String sql = "update wyc set name = 'xqy' where id = ?";
        int count = template.update(sql, 1);   参数一一对应 
        System.out.println(count); 1           不用关心释放资源
    @Test
    public void change (){
        String sql = "update wyc set salary =? where id = 7";
        int count = template.update(sql, 10000);
        System.out.println(count); //1}
    @Test
    public void remove(){
        String sql = "delete from wyc where id =?";
        int coun = template.update(sql, 9);
        System.out.println(coun);}

2.queryForMap():

查询结果将结果集封装为map集合,将列名作为key,将值作为value 将这条记录封装为一个map集合
注意:这个方法查询的结果集长度只能是1

@Test        将查询字段放入 map 集合中   只能是一个
public void select_1(){
String sql= "select * from wyc where id = ?";
Map<String, Object> maplist = template.queryForMap(sql, 2);
System.out.println(maplist);}
{id=2, name=xqy, age=17, local=congqing, salary=4000.0, join_time=2019-09-13 02:09:25.0}

3.queryForList():

           查询结果将结果集封装为list集合  list集合的泛型 为 map 集合   就是列表嵌套集合
           注意:1.将每一条记录封装为一个Map集合,再将Map集合装载到List集合中   2.注意为null的数据  需要使用包装类型   

    @Test       将查询每一个字段放入map集合中  再放入list集合中
    public void select_list(){ 
        String sql ="select * from wyc";
        List<Map<String, Object>> mapList = template.queryForList(sql);
        for (Map<String, Object> stringObjectMap : mapList) {
            System.out.println(stringObjectMap);}}
{id=6, name=aaa, age=25, local=bbb, salary=65040.0, join_time=2019-09-13 17:27:12.0}
{id=7, name=wyc, age=25, local=bbb, salary=10000.0, join_time=2019-09-13 17:35:57.0}
{id=8, name=bbb, age=100, local=Beijing, salary=9999.0, join_time=null}

4.query(): 最常用

  1.查询结果,将结果封装为JavaBean对象        需要指定类型  重写get set  tostring  方法
     1.query的参数:RowMapper 需要手封装

        public void select_list_table(){      将字段封装在 自定义类型  再放入 list集合中
        String sql ="select * from wyc";
        List<table> tables = template.query(sql, new RowMapper<table>() {
            @Override
            public table mapRow(ResultSet res, int i) throws SQLException {
                table table = new table();
                int id = res.getInt("id");
                String name = res.getString("name");
                int age = res.getInt("age");
                table.setId(id);
                table.setName(name);
                table.setAge(age);
                return table;}});
        for (table table : tables) {
            System.out.println(table);}} 
table{id=8, name='bbb', age=100, local='Beijing', salary=9999.0, join_time=null}.... 

   2.一般我们使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean的自动封装
      new BeanPropertyRowMapper<类型>(类型.class)   非常方便

    @Test     将字段封装在 自定义类型  再放入 list集合中
    public void select_list_table2(){
        String sql = "select * from wyc";
        List<table> lists = template.query(sql, new BeanPropertyRowMapper<table>(table.class));
        for (table list : lists) {
            System.out.println(list);
        }}  
table{id=8, name='bbb', age=100, local='Beijing', salary=9999.0, join_time=null}....

5.queryForObject

    查询结果,将结果封装为对象    一般用于聚合函数的查询

    @Test          查询总得字段数
    public void count(){
        String sql ="select count(id) from wyc";
        Integer integer = template.queryForObject(sql, int.class);
        System.out.println(integer);}      8
    

    

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TopicExpertiseModel =================== /** Copyright (C) 2013 by SMU Text Mining Group/Singapore Management University/Peking University TopicExpertiseModel is distributed for research purpose, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. If you use this code, please cite the following paper: Liu Yang, Minghui Qiu, Swapna Gottipati, Feida Zhu, Jing Jiang, Huiping Sun and Zhong Chen. CQARank: Jointly Model Topics and Expertise in Community Question Answering. In Proceedings of the 22nd ACM International Conference on Information and Knowledge Management (CIKM 2013). (http://dl.acm.org/citation.cfm?id=2505720) Feel free to contact the following people if you find any problems in the package. yang.liu@pku.edu.cn * */ Brief Introduction =================== 1. Community Question Answering (CQA) websites, where people share expertise on open platforms, have become large repositories of valuable knowledge. To bring the best value out of these knowledge repositories, it is critically important for CQA services to know how to find the right experts, retrieve archived similar questions and recommend best answers to new questions. To tackle this cluster of closely related problems in a principled approach, we proposed Topic Expertise Model (TEM), a novel probabilistic generative model with GMM hybrid, to jointly model topics and expertise by integrating textual content model and link structure analysis. Based on TEM results, we proposed CQARank to measure user interests and expertise score under different topics. Leveraging the question answering history based on long-term community reviews and voting, our method could find experts with both similar topical preference and high topical expertise. 2. This package implements Gibbs sampling for Topic Expertise Model for jointly modeling topics and expertise in question answering communities. More details of our model are described in the following paper: Liu Yang, Minghui Qiu, Swapna Gottipati, Feida Zhu, Jing Jiang, Huiping Sun and Zhong Chen. CQARank: Jointly Model Topics and Expertise in Community Question Answering. In Proceedings of the 22nd ACM International Conference on Information and Knowledge Management (CIKM 2013). (http://dl.acm.org/citation.cfm?id=2505720)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值