我们平时使用到的持久层都是封装了jdbc,所以不必关心jdbc的具体方法,但是也有用到的时候。
首先,需要一个数据源,这个自己配置就好了,这里我只写了 jdbc 实现数据库操作方法
先申明一下我使用的数据库表
表: student
字段: id
, name
, age
, create_date
字段类型:string
,string
,int
,Timestamp
我们首先先私有一个dataSource,一个dao层实现类
private DataSource dataSource;
private JdbcDao dao;
这里提取一个公共方法
public ResponseResult getStudent(Student stu) throws SQLException {
//定义连接对象
Connection connection = dataSource.getConnection();
//定义传参对象,初始化为null,具体内容在执行方法中进行定义
PreparedStatement preparedStatement = null;
try {
//设置自动提交属性为false,我这里采取统一提交
connection.setAutoCommit(false);
//调用jdbc实现操作,这个块里也可以直接写入方法,我这样做只是为了减少重复代码,解耦思想
//1、查询
Student newStu = dao.findStudent(stu, connection, preparedStatement);
//2、单个新增、修改、删除
Boolean isOk = dao.saveStudent(stu, connection, preparedStatement);
//3、批量新增、修改、删除
deleteStudent(stu, connection, preparedStatement);
//进行提交操作
connection.commit();
} catch (SQLException e) {
//catch到错误就进行回滚
connection.rollback();
e.printStackTrace();
} finally {
//关闭连接!!!
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
}
return ResponseResultFactory.successResult(null);
}
所调用的方法
1、查询
public Student findStudent(Student stu, Connection connection, PreparedStatement preparedStatement) throws SQLException {
Student newStu = new Student();
String sql = "SELECT * from student where id = ? and name = ?";
//配置参数
preparedStatement = connection.prepareStatement(sql);
//第一个参数就是传参位置,从1开始。第二个参数是传参内容,可以使用大部分数据类型;
preparedStatement.setString(1, stu.getId());
preparedStatement.setString(2, stu.getName());
//执行查询sql,返回查询结果
ResultSet resultSet = preparedStatement.executeQuery();
//如果返回有值,进行对象set
if (resultSet.next()) {
newStu.setId(resultSet.getLong("id"));
newStu.setName(resultSet.getString("name"));
newStu.setAge(resultSet.getInt("age"));
} else {
newStu = null;
}
return newStu;
}
2、单个新增、修改、删除
新增、修改、删除的步骤都是一样的,只是sql不同罢了。
因此这里只展示了新增方法,其余相同。
还有sql的执行方法不同 preparedStatement.executeUpdate(),这里要注意
public Boolean saveStudent(Student stu, Connection connection, PreparedStatement preparedStatement) throws SQLException {
Boolean isOk = false;
String sql = "INSERT INTO student (`id`, `name`, `age`, `create_date`) VALUES (?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, stu.getId());
preparedStatement.setString(2, stu.getName());
preparedStatement.setInt(3, stu.setAge());
//这里的时间放置,我采用了Timestamp。它可以精确到秒,而Date只能精确到日
java.sql.Timestamp createDate = new java.sql.Timestamp(new Date().getTime());
preparedStatement.setTimestamp(4, createDate);
//执行新增sql,返回的是int,大于0为返回执行的行数,等于0说明未执行或者未成功
int i = preparedStatement.executeUpdate();
if (i > 0) {
isOk = true;
}
return isOk;
}
3、批量新增、修改、删除
批量新增、修改、删除的步骤都是一样的,也只是sql不同罢了。
因此这里只展示了删除方法,其余相同。
和单个操作不同的是,这里没有采用循环执行sql,而是将所有条件都加入批次中 addBatch(),最后统一执行批次 executeBatch(),这样的话效率更高
public void deleteStudent(List<Student> list,Connection connection, PreparedStatement preparedStatement) throws SQLException {
String sql = "DELETE FROM student WHERE id=? ";
preparedStatement = connection.prepareStatement(sql);
for (Student stu:list) {
preparedStatement.setString(1, stu.getId());
//加入批次中
preparedStatement.addBatch();
}
//执行批次
preparedStatement.executeBatch();
}