接口自动化之数据库操作


一.引入数据库依赖

maven的pom.xml中引入依赖



mysql
mysql-connector-java
5.1.38
test




commons-dbutils
commons-dbutils
1.6
test

二.JDBC

Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。

三.连接数据库

public static Connection JDBCConnection(){
定义数据库连接对象
java.sql.Connection conn=null;
try {
conn= DriverManager.getConnection(url,user,password); //连接数据库
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}

四.关闭数据库

public static void unconnection(Connection connection){
if (connection!=null){ //先判断连接如果不为空,则执行以下代码
try {
connection.close(); //关闭数据库
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}

五.查询数据库一条数据

public static Map selectone(String sql){    //获取一条数据库数据为键值对,因此返回数据给map
Connection connection=JDBCConnection(); //查询之前先连接数据库,JDBCConnection()为以上二次分装过的连接数据库方法
QueryRunner queryRunner=new QueryRunner();
Map result=null;
try {
result=queryRunner.query(connection,sql,new MapHandler()); //查询一条数据,sql为查询一条数据的SQL语句
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
unconnection(connection); //查询结束后要执行finally里的代码:关闭数据库。unconnection()为以上二次封装过的关闭数据库方法
}
return result;
}

六.查询数据库多条数据

public static List> allselect(String sql){       //获取多条数据库数据为多个键值对,因此将map键值对放在list里
Connection connection=JDBCConnection(); //查询之前先连接数据库,JDBCConnection()为以上二次封装过的连接数据库方法
QueryRunner queryRunner=new QueryRunner();
List> result=null;
try {
result=queryRunner.query(connection,sql,new MapListHandler()); //查询多条数据,sql为查询多条数据的SQL数据
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
unconnection(connection); //查询结束后要执行finally里的代码:关闭数据库。unconnection()为以上二次封装过的关闭数据库方法
}
return result;
}

七.查询数据库一个数据

public static Object singledata(String sql){
Connection connection=JDBCConnection();
QueryRunner queryRunner=new QueryRunner();
Object result=null;
try {
result=queryRunner.query(connection,sql,new ScalarHandler()); //查询一个数据,sql为查询一个数据的SQL语句
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
unconnection(connection);
}
return result;
}

八.数据库的增删改

public static void updatedatas(String sql){
Connection connection=JDBCConnection();
QueryRunner queryRunner=new QueryRunner();
try {
queryRunner.update(connection,sql); //sql为SQL增删改语句
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
unconnection(connection);
}
}

 九.数据库断言

如果接口响应结果有数据,可在数据校验列中增加key:SQL语言,value:期望结果。

public static void assertSQL(Pojo pojo){
String sqlassert=pojo.getSql(); //获取表格中的数据库校验列
if (sqlassert!=null){ //如果这一行中的数据库校验列为空,执行以下代码
Map sqlassertmap= JSONObject.parseObject(sqlassert,Map.class); //表格中的json数据转化为map格式
for (String key:sqlassertmap.keySet()){ //循环map格式数据中的key,key为SQL语句
Object expectedvalue=sqlassertmap.get(key); //通过key得到对应的value
if (expectedvalue instanceof BigDecimal){ //如果期望值为小数
Object actualvalue=singledata(key); //singledata()是以上写的二次封装查询数据库一个数据的方法,查询数据库得到实际值
Assert.assertEquals(actualvalue,expectedvalue); //断言数据库实际值和期望值
}else if (expectedvalue instanceof Integer){ //如果数据库为整数
Long expectedvalue=((Integer)expectedvalue).longValue();
Object actualvalue=singledata(key);
Assert.assertEquals(actualvalue,expectedvalue);
}
}
}
}