JDBC为java提供了操作数据库的能力,但是操作过于繁琐,所以在真正的使用过程中使用原生jdbc来进行研发的情况少之又少,以至于衍生出来了很多基于JDBC的框架,比如大名鼎鼎的Hibernate,现在比较火的Mybatis以及Spring之前推出的JdbcTemplate和现在主推SpringDataJPA等等,众多的JDBC框架中要集成进来需要做一定量的配置,尤其是在多数据源的情况下有时会比较繁琐,以至于我选择了Dbutils。
下面咱们就开始Dbutils的简单应用
创建maven项目
引入相关依赖
#jdbc.driverClassName= jdbc.url=jdbc:mysql://127.0.0.1:3306/dbutils?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull jdbc.username=dbutils jdbc.password=dbutils jdbc.filters=stat jdbc.initialSize=2 jdbc.maxActive=300 jdbc.maxWait=60000 jdbc.timeBetweenEvictionRunsMillis=60000 jdbc.minEvictableIdleTimeMillis=300000 jdbc.validationQuery=SELECT 1 jdbc.testWhileIdle=true jdbc.testOnBorrow=false jdbc.testOnReturn=false jdbc.poolPreparedStatements=false jdbc.maxPoolPreparedStatementPerConnectionSize=200 |
package utils; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; import java.util.Properties; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.alibaba.druid.pool.DruidDataSource; /** * Dutils数据库操作工具类 * lizhilun */ public class Dbutils { public static DruidDataSource dataSource = new DruidDataSource(); public static QueryRunner runner; static { Properties properties = new Properties(); InputStream in = Dbutils.class.getClassLoader().getResourceAsStream("db.properties"); try { properties.load(in); } catch (IOException e) { e.printStackTrace(); } dataSource.setUrl(properties.getProperty("jdbc.url")); dataSource.setUsername(properties.getProperty("jdbc.username")); dataSource.setPassword(properties.getProperty("jdbc.password")); runner = new QueryRunner(dataSource); } /** * 公共查詢 返回的是List集合类型 * * @param t 泛型 * @param sql sql语句 * @param objs 查询参数 * @return * @throws Exception */ public static List return list; } /** * 公共查詢 返回的是List集合类型 * * @param t 泛型 * @param sql sql语句 * @param objs 查询参数 * @return * @throws Exception */ public static List List return mapList; } /** * 查询单个字段, 返回Long * @param sql * @param objs * @return * @throws Exception */ public static Long query4Num(String sql, Object... objs) throws Exception { Long query = runner.query(sql, new ScalarHandler return query; } /** * 查询单条记录 * * @return * @throws Exception */ public static Object countQuery(String sql) throws Exception { Object result = runner.query(sql, new ScalarHandler<>()); return result; } /** * 执行数据更新 * * @param sql * @param objs * @return * @throws Exception */ public static Integer executeUpdate(String sql, Object... objs) throws Exception { return runner.execute(sql, objs); } } |
package entity; import java.io.Serializable; /** * @Descrption user表实体类 * @author lizhilun * */ public class User implements Serializable{ private static final long serialVersionUID = 1L; private Integer id;//主键 private String name;//姓名 private Integer age;//年龄 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } |
package com.test; import java.util.List; import entity.User; import utils.Dbutils; public class TestDbutil { //为了代码简介,异常我直接抛了 public static void main(String[] args)throws Exception { String sql="select * from user where 1=1;"; //执行查询 List<?> resultList = Dbutils.commonQuery(User.class, sql); System.out.println(resultList); } } |
| 留言与评论(共有 0 条评论) “” |