第一步:建立好对应的项目结构:
BaseDao :里面 拿来 封装 连接数据的操作 StudentDaoImple 到时候 直接继承 少写很多代码
JdbcUtils: 用来获取数据库的链接
jdbc.peoperties: 用来写数据的设置
第二步:导入对应的jar包:
第三步 获取数据库链接:
因为这个操作经常要用到 所以写到一个工具类里面:
JdbcUtils:
没啥好说的照抄就行
public class JdbcUtils {
//获取数据库的链接
private static DruidDataSource source;
static {
try {
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties ps=new Properties();
ps.load(is);
source = (DruidDataSource) DruidDataSourceFactory.createDataSource(ps);
} catch (Exception e) {
e.printStackTrace();
}
}
// :获取数据库链接
public static Connection getconnection() {
Connection connection = null;
try {
connection = source.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
//关闭数据库链接
public static void closeconnection(Connection connection){
if (connection!=null){
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
为了方便修改 数据库信息设置到 外部文件;
jdbc.peoperties:
username=root
password=1234
url=jdbc:mysql://localhost:3306/peixun?characterEncoding=UTF-8
driverClassName=com.mysql.jdbc.Driver
第四步 配置好Dao
BaseDao :
package zyc.dao.imple;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import zyc.utils.JdbcUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
/**
* @description:
* @author: Code-zyc
* @date: Created in 2020/12/21 9:53
* @version: v1.0
* @modified By:
*/
public class BaseDao {
private QueryRunner queryRunner = new QueryRunner();
/**
* @descript : 查询单个数据
* @author :ZhaoYicong
* @date :2020/12/21 12:59
* @Param: type class类 sql sql语句 args 为占位符准备
* @return :
* @throws :
* @since :
*/
public <T> T selectForOne(Class<T> type,String sql,Object ...args){
Connection connection = JdbcUtils.getconnection();
try {
return queryRunner.query(connection, sql, new BeanHandler<>(type), args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.closeconnection(connection);
}
return null;
}
/**
* @descript :查询多个数据
* @author :ZhaoYicong
* @date :2020/12/21 13:09
* @Param:
* @return :
* @throws :
* @since :
*/
public <T> List<T> selectForMore(Class<T> type,String sql,Object...args){
Connection connection = JdbcUtils.getconnection();
try {
queryRunner.query(connection,sql,new BeanListHandler<>(type),args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.closeconnection(connection);
}
return null;
}
/**
* @descript :表单的删除,修改 ,添加
* @author :ZhaoYicong
* @date :2020/12/21 13:16
* @Param:
* @return : -1 代表数据更新失败
* @throws :
* @since :
*/
public int update(String sql,Object...args){
Connection connection = JdbcUtils.getconnection();
try {
return queryRunner.update(connection,sql,args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.closeconnection(connection);
}
return -1;
}
}
第五步测试:
@Test
public void test2(){
BaseDao baseDao =new BaseDao();
String sql="SELECT * FROM student";
List<Student> students = baseDao.selectForMore(Student.class, sql);
for (Student student:students){
System.out.println(student.toString());
}
}