JDBC connection pool, JDBCTemplate

**

1. Database connection pool

**
1.1 Analysis

It is necessary to apply for connection resources every time and then release the resources after use. This is very time-consuming and causes a waste of resources.

1.2 Concept

The database connection pool is actually a container (collection) that stores database connections.

After the system is initialized, the container is created, and some connection objects will be applied for in the container. When the user accesses the database, the database connection object is obtained from the container. After the user completes the access, the connection object is returned to the container.

Database connection pool technology is similar to the thread pool in multi-threading

1.3 How to implement?

1) Standard interface: DataSource (data source, that is, connection pool), under the java.sql package, the methods included are:

获取连接:getConnection()
归还连接:connection.close()
	如果连接对象connection是从连接池中获取的,那么调用connection.close()方法,则不会关闭连接,而是归还连接。

2) Generally, you do not need to implement the DataSource interface yourself, but it is implemented by the database vendor.

1.C3P0:数据库连接池技术(技术较老)
2.Druid:数据库连接池技术(推荐,技术较新)

1.4 C3P0 database connection pool technology

The usage steps are:

1.导入jar包(两个)c3p0-0.9.5.2.jar和mchange-commons-java-0.2.12.jar
    注意不要忘记导入数据库的驱动jar包
2.定义配置文件:
    1.名称(必须是这两种名称,这样会自动找到文件,不是这个名称的话,会找不到文件):c3p0-config.xml or c3p0.properties
    2.路径:直接将文件放在src目录下即可。
3.创建核心对象,即数据库连接池对象:ComboPooledDataSource

1.5 Druid database connection pool technology

Steps for usage:

1.导入jar包:druid-1.0.9.jar,同样不要忘记导入数据库驱动jar包
2.定义配置文件,该文件有以下特点:
     1.是properties形式的
     2.可以是任意名称,可以放在任意目录下
         区别于C3P0,不会自动加载配置文件,C3P0的配置文件放在src目录下会自动加载
3.加载配置文件:Properties
4.获取数据库连接池对象:通过工厂类来获取,DruidDataSourceFactory
5.获取连接:getConnection

Define tool class:

1.导入jar包:druid-1.0.9.jar,同样不要忘记导入数据库驱动jar包
2.定义配置文件,该文件有以下特点:
    1.是properties形式的
    2.可以是任意名称,可以放在任意目录下
        区别于C3P0,不会自动加载配置文件,C3P0的配置文件放在src目录下会自动加载
3.加载配置文件:Properties
4.获取数据库连接池对象:通过工厂类来获取,DruidDataSourceFactory
5.获取连接:getConnection

**

二、Spring JDBC:JDBC Template

**

2.1 Concept

Spring framework's simple encapsulation of JDBC. Provides a JDBCTemplate object to simplify JDBC development

2.2 Usage steps

1.导入jar包
2.创建JDBCTemplate对象。创建的时候依赖于数据源DataSource(连接池)。
	jdbcTemplate template = new jdbcTemplate(ds);
3.调用JDBCTemplate对象的方法完成增删改查的操作
	update():执行DML语句
	queryForMap():查询结果,并将结果集封装为map集合
		将列名作为key,将值作为value,将这条记录封装为一个map集合
		注意事项:这个方法查询的结果集长度只能是1
	queryForList():查询结果,并将结果集封装为list集合
		注意:该方法是将每一条记录封装为一个map集合,再讲map集合装载到list集合中
	query():查询结果,并将结果集封装为JavaBean对象
		参数:RowMapper
			一般我们使用BeanPropertyRowMapper实现类,可以完成数据JavaBean的自动封装
			new BeanPropertyRowMapper<类型>(类型的字节码对象)
		注意:该方法是将每一条记录封装为一个JavaBean对象,此处设置JavaBean对象为Emp对象,然后再将JavaBean对象装载到list集合中
		在实际应用中,使用该方法,而不是将记录封装为map
	queryForObject():查询结果,并将结果集封装为对象
		一般用于聚合函数的查询

2.3 JavaBean

JavaBean is a class that conforms to the naming convention. It defines properties through getters and setters. Only getters and no setters are read-only properties.

Guess you like

Origin blog.csdn.net/weixin_42214237/article/details/126312776