1.数据库连接池
数据库连接池其实就是一个容器,这个容器中有很多个连接对象,当需要操作数据库的时候只需要在数据库连接池中获取一个连接对象即可。访问完数据库后,就会把连接对象归还给数据库连接池。并不会将连接对象给销毁而是归还给数据库连接池。当然这些操作都由底层进行封装,用户只需要调用方法即可。
使用druid以及JDBCTemplate操作数据库的步骤:
1.导入依赖:
<!-- 阿里巴巴数据库连接池druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.7</version>
</dependency>
<!-- JDBCTemplate -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.10</version>
</dependency>
2.编写druid.properties配置文件(文件名称可以随意但是必须是.properties)
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000
3.编写JDBCUtil工具类
package ala.Utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/*
* @Author: ala
* @Date: 2021-09-23-15:04
* @ClassName: JDBCUtil
* @Describe: 获取连接池对象、连接对象、关闭资源的工具类
*/
public class JDBCUtil {
//创建好数据库连接池对象 不用创建连接对象因为数据库连接池对象就可以创建连接对象
private static DataSource ds;
//静态代码块用于完成基本的加载
static {
try {
//加载配置文件
Properties pro = new Properties();
pro.load(JDBCUtil.class.getClassLoader()
.getResourceAsStream("druid.properties"));
//获取DataSource(数据库连接池)对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取数据库连接池对象
public static DataSource getDataSource(){
return ds;
}
//获取连接对象
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//归还连接对象
public static void close(ResultSet res, Statement stat,Connection conn){
if (res != null){
try {
res.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stat != null){
try {
stat.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void close(Statement stat,Connection conn){
close(null, stat, conn);
}
}
4.使用JDBCTemplate进行增删改查的基本功能
package ala.JDBCTemplate;
import ala.Utils.JDBCUtil;
import ala.domain.User;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/*
* @Author: ala
* @Date: 2021-09-23-16:15
* @ClassName: JDBCTemplateDemo2
* @Describe:
*/
public class JDBCTemplateDemo2 {
//进行数据的修改
@Test
public void test1(){
//创建一个jdbcTemplate对象
JdbcTemplate template = new JdbcTemplate(JDBCUtil.getDataSource());
//编写sql语句----将id为1的密码修改为000
String sql = "update user set password = ? where id = ?";
//执行sql
int update = template.update(sql, "000", 1);
System.out.println(update);
}
//插入数据
@Test
public void test2(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "insert into user(username,password) values(?,?)";
int update = jdbcTemplate.update(sql, "ala", "fsdf");
System.out.println(update);
}
//删除数据
@Test
public void test3(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "delete from user where id = ?";
int update = jdbcTemplate.update(sql, 1);
System.out.println(update);
}
//查询单条数据
@Test
public void test4(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "select * from user where id = ?";
Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(sql, 2);
System.out.println(stringObjectMap);
}
//查询多条记录
@Test
public void test5(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "select * from user";
List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
//查询多条记录将其封装进javaBean----自己封装javaBean
@Test
public void test6(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "select * from user";
List<User> query = jdbcTemplate.query(sql, new RowMapper<User>() {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
User user = new User();
user.setId(id);
user.setUsername(username);
user.setPassword(password);
return user;
}
});
for (User user : query) {
System.out.println(user);
}
}
//查询多条记录将其封装进javaBean----使用模板
@Test
public void test7(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "select * from user";
List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
for (User user : query) {
System.out.println(user);
}
}
//聚合函数的使用
@Test
public void test8(){
JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtil.getDataSource());
String sql = "select count(id) from user";
Long res = jdbcTemplate.queryForObject(sql, Long.class);
System.out.println(res);
}
}
|