Spring JDBCTemplate对象的增删查改写法 学习笔记

使用方法:这是操纵数据库的代码,前提是要已经写好了一个JDBCUtils的工具类,获得了数据库连接后才能增删改查。

先创建JDBCUtils工具类:https://blog.csdn.net/qq_40763929/article/details/104027165

然后就可以在下面代码中调用相应的函数了。

 

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

JdbcTemplate template = new JdbcTemplate(ds);

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

update()  //执行DML语句。增、删、改语句

插入: 

JdbcTemplate template = new JdbcTemplate(ds); 
String sql = "insert into student(name,sex,age) values(?,?,?)";
template.update(sql,"a","man",10);

修改:

JdbcTemplate template = new JdbcTemplate(ds);
String sql = "update student set name = 'name1'  where id = 1";
template.update(sql);

删除: 

JdbcTemplate template = new JdbcTemplate(ds);
String sql = "delete from student where name = 'a' ";
template.update(sql);

查询:

queryForMap()

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

例如:

String sql = "select * from emp where id = ? or id = ?";
Map<String, Object> map = template.queryForMap(sql, 1001,1002);
System.out.println(map);

//{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}

 

queryForList()
//查询结果将结果集封装为list集合。
//注意:将每一条记录封装为一个Map集合,再将Map集合装载到List集合中。

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

//查询结果,将结果封装为JavaBean对象。
//query的参数:RowMapper。
//一般我们使用BeanPropertyRowMapper实现类,可以完成数据到JavaBean的自动封装。

new BeanPropertyRowMapper<类型>(类型.class)

例如:

有一个自定义的 " UserInfo "类,包含 " username " 和 " password "两个值。

有两个方法:getUsername()  和  getPassword(),用于获取相应的值。

现在要从数据库中以 username 和 password 为条件查询用户信息,返回一个 UserInfo 类,则用法如下:

public class UserDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    public UserInfo query(UserInfo loginUser)
    {
        try {
            String sql = "select * from userinfo where username = ? and password = ?";
            UserInfo queryResult = template.queryForObject(sql, new BeanPropertyRowMapper<UserInfo>(UserInfo.class),
                    loginUser.getUsername(),loginUser.getPassword());

            return queryResult;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}



我的测试数据库为:

以下是增删查改代码:(要导入相应的包)

import Utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class JdbcTemplateDemo2 {

    //1. 获取JDBCTemplate对象
    static JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    public static void main(String[] args) {
        //update();
        //insert();
        //delete();
        //query1();
        //query2();
        test7();
    }
  /**
  *  修改数据库操作
 */
    public static void update(){
        //2. 定义sql
        String sql = "update student set name = 'name1'  where id = 1";
        //3. 执行sql
        int count = template.update(sql);
        System.out.println(count);
    }

    /**
     * 2. 添加一条记录
     */

    public static void insert(){
        String sql = "insert into student(name,sex,age) values(?,?,?)";
        int count = template.update(sql,"a","man",10);
        System.out.println(count);
    }

    /**
     * 3.删除刚才添加的记录
     */

    public static void delete(){
        String sql = "delete from student where name = 'a' ";
        int count = template.update(sql);
        System.out.println(count);
    }

    /**
     * 4.查询id为1的记录,将其封装为Map集合
     * 注意:这个方法查询的结果集长度只能是1
     */
    public static void query1(){
        String sql = "select * from student where id = ? ";
        Map<String, Object> map = template.queryForMap(sql, 1);
        System.out.println(map);
    }

    /**
     * 5. 查询所有记录,将其封装为List
     */

    public static void query2(){
        String sql = "select * from student";
        List<Map<String, Object>> list = template.queryForList(sql);

        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }


    /**
     * 7. 查询总记录数
     */


    public static void test7(){
        String sql = "select count(id) from student";
        Long total = template.queryForObject(sql, Long.class);
        System.out.println(total);
    }

}

 

 

 

 

posted @ 2020-01-18 09:36  RomanticChopin  阅读(778)  评论(0编辑  收藏  举报
-->
Live2D