轻量级ORM框架DbUtils和OrmLite

  最近在树莓派上部署了一套数据抓取工具,需要将抓取的数据录入到MySQL,特意找了下一些轻量级的ORM框架,这里简单介绍下DbUtils和OrmLite的配置和使用。

1. DbUtils

  Apache旗下的,速度与稳定性不言而喻,但其配置还是有点啰嗦,依赖dbcp连接池。

  地址:http://commons.apache.org/proper/commons-dbutils/

添加pom依赖:

<dependency>
	<groupId>commons-dbutils</groupId>
	<artifactId>commons-dbutils</artifactId>
	<version>1.6</version>
</dependency>
<dependency>
	<groupId>commons-dbcp</groupId>
	<artifactId>commons-dbcp</artifactId>
	<version>1.4</version>
</dependency>

添加dbcp.properties配置文件,放在classpath中:

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.91.128:3306/db
username=steven
password=steven
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
maxWait=60000
#附带连接属性
connectionProperties=useUnicode=true;characterEncoding=utf8
#自动提交(auto-commit)
defaultAutoCommit=true
#只读(read-only)
defaultReadOnly=
#事务级别(TransactionIsolation)
defaultTransactionIsolation=READ_COMMITTED

新增JdbcUtil.java来加载属性文件

package com.dorole.utils;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

import com.mysql.jdbc.Connection;

public class JdbcUtil {
	private static DataSource ds;
	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	static {
		try {
			Properties prop = new Properties();
			InputStream in = JdbcUtil.class.getClassLoader()
					.getResourceAsStream("dbcp.properties");
			prop.load(in);
			ds = BasicDataSourceFactory.createDataSource(prop);
		} catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}

	public static DataSource getDataSource() {
		return ds;
	}

	public static Connection getConnection() throws SQLException {
		try {
			Connection conn = tl.get();
			if (conn == null) {
				conn = (Connection) ds.getConnection();
				tl.set(conn);
			}
			return conn;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

插入数据:

QueryRunner runner = new QueryRunner(JdbcUtil.getDataSource());
String sql = "INSERT INTO `orders` (`storeName`, `telecomNumber`) VALUES (?, ?)";
Object params[] = { order.getStoreName(), order.getTelecomNumber(), };
try {
	runner.update(sql, params);
} catch (SQLException e) {
	e.printStackTrace();
}

2. OrmLite

  这个在Android上用的比较多,结合轻量级的数据库HSQLDB,非常适合嵌入式设备,但这里我们使用MySQL做演示,有意思的是连BaseDao都给封装好了,配置简单,易于使用。

  地址:http://ormlite.com/

添加pom依赖:

<dependency>
	<groupId>com.j256.ormlite</groupId>
	<artifactId>ormlite-jdbc</artifactId>
	<version>4.9</version>
</dependency>

新增OrderDao接口,默认的增删改查已经有了,故可以留空:

package com.dorole.dao;

import com.dorole.model.Order;
import com.j256.ormlite.dao.Dao;

public interface OrderDao extends Dao<Order, Integer> {

}

新增OrderDaoImpl实现类

package com.dorole.dao.impl;

import java.sql.SQLException;

import com.dorole.dao.OrderDao;
import com.dorole.model.Order;
import com.j256.ormlite.dao.BaseDaoImpl;
import com.j256.ormlite.support.ConnectionSource;

public class OrderDaoImpl extends BaseDaoImpl<Order, Integer> implements
		OrderDao {
	public OrderDaoImpl(ConnectionSource connectionSource,
			Class<Order> dataClass) throws SQLException {
		super(connectionSource, dataClass);
	}
}

初始化:

OrderDao orderDao = null;
ConnectionSource connectionSource;
try {
	connectionSource = new JdbcConnectionSource(
			"jdbc:mysql://192.168.91.128:3306/db");
	((JdbcConnectionSource) connectionSource).setUsername("steven");
	((JdbcConnectionSource) connectionSource).setPassword("steven");
	orderDao = new OrderDaoImpl(connectionSource, Order.class);
} catch (SQLException e) {
	e.printStackTrace();
}

插入数据:

try {
	Order order = new Order();
	orderDao.create(order);
} catch (SQLException e) {
	e.printStackTrace();
}