dome gitub :https://github.com/shimingda/java-dome
DBUtils是Java编程中的数据库操作实用工具,小巧简单实用。
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
QueryRunner中提供对sql语句操作的API
ResultSetHandler接口,用于定义select操作后,怎样封装结果集
DBUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法
package dome.base;
import org.apache.commons.dbcp2.BasicDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 数据配置
* @author Somin
*/
public class DBConf
{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8";
static final String USER = "root";
static final String PASS = "1234";
public static Connection getConnection(){
try
{
Class.forName(DBConf.JDBC_DRIVER);
return DriverManager.getConnection(DBConf.DB_URL, DBConf.USER, DBConf.PASS);
}
catch (Exception e)
{
System.out.println("databases connection error");
return null;
}
}
public static DataSource getDataSource()
{
BasicDataSource basicDataSource=new BasicDataSource();
basicDataSource.setDriverClassName(JDBC_DRIVER);
basicDataSource.setUsername(USER);
basicDataSource.setPassword(PASS);
basicDataSource.setUrl(DB_URL);
return basicDataSource;
}
}
package dome.base;
/**
* 数据库实体类
* @author Somin
*/
public class User
{
public int id;
public String name;
public int age;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
import dome.base.DBConf;
import dome.base.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.Connection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* base test
* @author Somin
*/
public class BaseTest
{
QueryRunner queryRunner = new QueryRunner();
Connection conn= null;
@Before
public void init()
{
conn= DBConf.getConnection();
}
@Test
@Ignore
public void insertTest() throws Exception
{
String sql = "INSERT INTO user(name, age) VALUES(?,?)";
Object[] objectArr= queryRunner.insert(conn, sql, new ArrayHandler(), "root", 18);
System.out.println("数组长度:" + objectArr.length + ";第0个元素的值:" +objectArr[0]);
objectArr = queryRunner.insert(conn, sql, new ArrayHandler(), "test", 19);
System.out.println("数组长度:" + objectArr.length + ";第0个元素的值:" +objectArr[0]);
}
@Test
@Ignore
public void queryBeanTest() throws Exception
{
ResultSetHandler resultSetHandler=new BeanHandler(User.class);
String sql ="SELECT * FROM user where id =?";
User user=queryRunner.query(conn,sql,resultSetHandler,1);
System.out.println("ID:"+user.getId()+",name:"+user.getName());
ResultSetHandler> resultSetHandlers=new BeanListHandler(User.class);
String sqlList="SELECT * FROM user";
List users=queryRunner.query(conn,sqlList,resultSetHandlers);
users.forEach(u->{
System.out.println("id:"+u.getId()+",name:"+u.getName());
});
}
@Test
public void queryMapTest() throws Exception
{
String sql ="SELECT * FROM user where id =?";
Map resultMap = queryRunner.query(conn,sql,new MapHandler(),1);
System.out.println("获取一条数据:"+resultMap);
String sql2 ="SELECT * FROM user ";
List
package dome.advanced;
import dome.base.User;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 自定义映射
* @author Somin
*/
public class UserHandler extends BeanHandler
{
public UserHandler()
{
super(User.class);
}
@Override
public User handle(ResultSet rs) throws SQLException
{
return super.handle(rs);
}
}
import dome.advanced.UserHandler;
import dome.base.DBConf;
import dome.base.User;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.Connection;
/**
* advanced test
* @author Somin
*/
public class AdvancedTest
{
@Test
@Ignore
public void handlerTest() throws Exception
{
QueryRunner queryRunner = new QueryRunner();
Connection conn=DBConf.getConnection();
String sql ="SELECT * FROM user where id =?";
User user=queryRunner.query(conn,sql,new UserHandler(),1);
System.out.println("id:"+user.getId()+",name:"+user.getName());
}
@Test
public void dataSourceTest() throws Exception
{
QueryRunner queryRunner=new QueryRunner(DBConf.getDataSource());
String sql ="SELECT * FROM user where id =?";
User user=queryRunner.query(sql,new UserHandler(),1);
System.out.println("id:"+user.getId()+",name:"+user.getName());
}
}