Template的简单使用及所需驱动

两盒软妹~` 提交于 2020-02-09 18:24:36

Template的简单使用及所需驱动

使用template前准备事项

  1. 需要将以下jar包导入libs中
    在这里插入图片描述
  2. 使用template需要传入一个连接池datasource对象,本文使用druid连接池
  3. 不要忘记添加druid的配置文件

template实现增删改操作

添加数据操作
JdbcTemplate template1 = new JdbcTemplate(JDBCUtils_druid.getDataSource());
        String sql1 = "insert into student values(?,?,?)";
        int count1 = template1.update(sql1, 4, "卢本伟", 30);
        System.out.println(count1);
修改数据操作
JdbcTemplate template2 = new JdbcTemplate(JDBCUtils_druid.getDataSource());
        String sql2 = "update student set age = ? where id = ?";
        int count2 = template2.update(sql2,35,4);
        System.out.println(count2);
删除数据操作
JdbcTemplate template3 = new JdbcTemplate(JDBCUtils_druid.getDataSource());
        String sql3 = "delete from student where id = ?";
        int count3 = template3.update(sql3,4);
        System.out.println(count3);

template实现查询操作

将查询结果封装为map集合
String sql = "select * from student where id = ?";
        Map<String, Object> map = template.queryForMap(sql, 1);
        System.out.println(map);

注意:只能查询单行数据

将查询结果封装为list集合
String sql = "select * from student";
        List<Map<String, Object>> list = template.queryForList(sql);

        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
将查询结果封装为JavaBean对象
JdbcTemplate template = new JdbcTemplate(JDBCUtils_druid.getDataSource());
         String sql = "select * from student";
        List<Student> list = template.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
        for (Student s: list) {
            System.out.println(s);
        }

注意:需要传入一个具体的类,该类的成员变量应包括查询结果集的所有字段,并且需要提供标准的getter&setter方法

将查询结果封装为object对象
String sql = "select count(id) from student";
			       Integer total = template.queryForObject(sql, Integer.class);
			       System.out.println(total);

注意:一般用于单行单列的聚合函数结果集查询

druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1
username=root
password=root
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大等待时间
maxWait=3000

druid工具类

/********************************************************************************************************
 此为Druid的工具类
 *******************************************************************************************************/
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils_druid {
    /************************************************************************************************
     静态代码块加载druid的配置文件并注册驱动
     ************************************************************************************************/
    private static DataSource ds ; //ds为连接池对象

    static{
        try {
            Properties pro = new Properties();
            pro.load(JDBCUtils_druid.class.getClassLoader().getResourceAsStream("druid.properties"));

            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**************************************************************************
     获取连接池的连接对象
     **************************************************************************/
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    /***************************************************************************
     获取连接池对象
     **************************************************************************/
    public static DataSource getDataSource(){
        return  ds;
    }

    /***************************************************************************
     释放资源
     ***************************************************************************/
    public static void close(PreparedStatement pstmt, Connection conn){
        close(null,pstmt,conn);
    }

    public static void close(ResultSet rs , PreparedStatement pstmt, Connection conn){

        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(pstmt != null){
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(conn != null){
            try {
                conn.close();//归还连接而不是关闭
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!