您的位置:首页 > 编程语言 > Java开发

Druid-数据库连接池技术入门 配合Spring JdbcTemplate 使用

2019-05-01 10:11 260 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_44844335/article/details/89636110

1.Druid是什么?

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能

2.下载地址

maven中央仓库: http://central.maven.org/maven2/com/alibaba/druid/

3.基本用法
首先创建在项目下创建lib目录 ,并将jar包导入 ,
接下里配置 druid.properties

driverClassName=com.mysql.cj.jdbc.Driver  //驱动器
url=jdbc:mysql://localhost:3306/travel //url
username=root //用户名
password=root//密码
initialSize=5 // 连接池中连接的初始化数量
maxActive=10 //最大数量
maxWait=3000 //出现异常等待的时间

首先加载配置文件

Properties pro = new Properties();
InputStream inputStream =  DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(inputStream)

然后获取连接池对象

DataSource ds = DruidDataSourceFactory.createDataSource(pro);
Connection coon = ds.getConnection();

我们可以将druid连接池做成一个工具集合 下面是我的JDBCUtils

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtils {
private static DataSource ds;
static {
Properties pro = new Properties();
try {
pro.load(JDBCUtils.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 void close(Statement stmt,Connection conn){
close(null,stmt,conn);
}

public static void close(ResultSet rs , Statement stmt, Connection conn){

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

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

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

public static DataSource getDataSource(){
return ds;
}

}

4.Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
首先导入jar 包

创建JdbcTemplate对象。依赖于数据源DataSource

JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

下面是数据库

将其封装为JavaBean

public class Emp {
private Integer id;
private String name;
private Integer age;
// 为什么 javaBean 不能定义为 基本类型 ? 因为 数据库之中的值可能为null 所以 要用封装类型。
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name +
", age=" + age +
'}';
}
}

调用JdbcTemplate的方法来完成查询所以数据的操作

String sql ="select * from people  ";
List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class));
for (Emp emp : list) {
System.out.println(emp);
}

看下结果 成功输出

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: