basedao mysql_操作数据库的通用BaseDAO

package com.dao;

import java.io.InputStream;

import java.lang.reflect.Method;

import java.sql.*;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import com.entity.User;

public class BaseDAO {

private static String driveClassName = "com.mysql.jdbc.Driver";

private static String url = "jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8";

private static String user = "root";

private static String password = "123";

static {

try {

Class.forName(driveClassName);

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}

private Connection conn = null;

public BaseDAO() {

try {

conn = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

e.printStackTrace();

}

}

public void save(T entity) throws Exception {

String sql = "insert into " + entity.getClass().getSimpleName().toLowerCase() + "(";

List list = this.matchPojoMethods(entity, "get");

Iterator iter = list.iterator();

while (iter.hasNext()) {

Method method = iter.next();

sql += method.getName().substring(3).toLowerCase() + ",";

}

sql = sql.substring(0, sql.lastIndexOf(",")) + ") values(";

for (int j = 0; j < list.size(); j++) {

sql += "?,";

}

sql = sql.substring(0, sql.lastIndexOf(",")) + ")";

PreparedStatement statement = conn.prepareStatement(sql);

int i = 0;

iter = list.iterator();

while (iter.hasNext()) {

Method method = iter.next();

if (method.getReturnType().getSimpleName().indexOf("String") != -1) {

statement.setString(++i, this.getString(method, entity));

} else if (method.getReturnType().getSimpleName().indexOf("Date") != -1) {

statement.setDate(++i, this.getDate(method, entity));

} else if (method.getReturnType().getSimpleName().indexOf("InputStream") != -1) {

statement.setAsciiStream(++i, this.getBlob(method, entity), 1440);

} else {

statement.setInt(++i, this.getInt(method, entity));

}

}

statement.executeUpdate();

}

public void update(T entity) throws Exception {

String sql = "update " + entity.getClass().getSimpleName().toLowerCase() + " set ";

List list = this.matchPojoMethods(entity, "get");

Method tempMethod = null;

Method idMethod = null;

Iterator iter = list.iterator();

while (iter.hasNext()) {

tempMethod = iter.next();

if (tempMethod.getName().lastIndexOf("Id") != -1 && tempMethod.getName().substring(3).length() == 2) {

idMethod = tempMethod;

iter.remove();

} else if ((entity.getClass().getSimpleName() + "Id").equalsIgnoreCase(tempMethod.getName().substring(3))) {

idMethod = tempMethod;

iter.remove();

}

}

iter = list.iterator();

while (iter.hasNext()) {

tempMethod = iter.next();

sql += tempMethod.getName().substring(3).toLowerCase() + "= ?,";

}

sql = sql.substring(0, sql.lastIndexOf(","));

sql += " where " + idMethod.getName().substring(3).toLowerCase() + " =?";

System.out.println("--" + sql);

PreparedStatement statement = conn.prepareStatement(sql);

int i = 0;

iter = list.iterator();

while (iter.hasNext()) {

Method method = iter.next();

if (method.getReturnType().getSimpleName().indexOf("String") != -1) {

statement.setString(++i, this.getString(method, entity));

} else if (method.getReturnType().getSimpleName().indexOf("Date") != -1) {

statement.setDate(++i, this.getDate(method, entity));

} else if (method.getReturnType().getSimpleName().indexOf("InputStream") != -1) {

statement.setAsciiStream(++i, this.getBlob(method, entity), 1440);

} else {

statement.setInt(++i, this.getInt(method, entity));

}

}

if (idMethod.getReturnType().getSimpleName().indexOf("String") != -1) {

statement.setString(++i, this.getString(idMethod, entity));

} else {

statement.setInt(++i, this.getInt(idMethod, entity));

}

statement.executeUpdate();

}

public void delete(T entity,String id) throws Exception {

String sql = "delete from " + entity.getClass().getSimpleName().toLowerCase() + " where id="+id;

Method idMethod = null;

List list = this.matchPojoMethods(entity, "get");

Iterator iter = list.iterator();

while (iter.hasNext()) {

Method tempMethod = iter.next();

if (tempMethod.getName().lastIndexOf("Id") != -1 && tempMethod.getName().substring(3).length() == 2) {

idMethod = tempMethod;

iter.remove();

} else if ((entity.getClass().getSimpleName() + "Id").equalsIgnoreCase(tempMethod.getName().substring(3))) {

idMethod = tempMethod;

iter.remove();

}

}

sql += idMethod.getName().substring(3).toLowerCase() + " = ?";

System.out.println("--" + sql);

PreparedStatement statement = conn.prepareStatement(sql);

int i = 0;

if (idMethod.getReturnType().getSimpleName().indexOf("String") != -1) {

statement.setString(++i, this.getString(idMethod, entity));

} else {

statement.setInt(++i, this.getInt(idMethod, entity));

}

statement.executeUpdate();

}

@SuppressWarnings("unchecked")

public List findAll(T entity) throws Exception{

List list=new ArrayList();

String sql = "select * from " + entity.getClass().getSimpleName().toLowerCase();

PreparedStatement statement = conn.prepareStatement(sql);

ResultSet rsResultSet=statement.executeQuery(sql);

while (rsResultSet.next()) {

User user=new User();

user.setId(rsResultSet.getInt(1));

user.setUsername(rsResultSet.getString(2));

user.setPassword(rsResultSet.getString(3));

user.setNumber(rsResultSet.getString(4));

user.setPermissions(rsResultSet.getInt(5));

list.add((T) user);

}

return list;

}

public T findById(T entity, int id) throws Exception {

String sql = "select * from " + entity.getClass().getSimpleName().toLowerCase() + " where ";

Method idMethod = null;

List list = this.matchPojoMethods(entity, "set");

Iterator iter = list.iterator();

while (iter.hasNext()) {

Method tempMethod = iter.next();

if (tempMethod.getName().indexOf("Id") != -1 && tempMethod.getName().substring(3).length() == 2) {

idMethod = tempMethod;

} else if ((entity.getClass().getSimpleName() + "Id").equalsIgnoreCase(tempMethod.getName().substring(3))) {

idMethod = tempMethod;

}

}

sql += idMethod.getName().substring(3, 4).toLowerCase() + idMethod.getName().substring(4) + " = ?";

System.out.println(sql);

PreparedStatement statement = conn.prepareStatement(sql);

statement.setInt(1, id);

ResultSet rs = statement.executeQuery();

iter = list.iterator();

while (rs.next()) {

while (iter.hasNext()) {

Method method = iter.next();

if (method.getParameterTypes()[0].getSimpleName().indexOf("String") != -1) {

this.setString(method, entity, rs.getString(method.getName().substring(3).toLowerCase()));

} else if (method.getParameterTypes()[0].getSimpleName().indexOf("Date") != -1) {

this.setDate(method, entity, rs.getDate(method.getName().substring(3).toLowerCase()));

} else if (method.getParameterTypes()[0].getSimpleName().indexOf("InputStream") != -1) {

this.setBlob(method, entity,

rs.getBlob(method.getName().substring(3).toLowerCase()).getBinaryStream());

} else {

this.setInt(method, entity, rs.getInt(method.getName().substring(3).toLowerCase()));

}

}

}

return entity;

}

private List matchPojoMethods(T entity, String methodName) {

Method[] methods = entity.getClass().getDeclaredMethods();

List list = new ArrayList();

for (int index = 0; index < methods.length; index++) {

if (methods[index].getName().indexOf(methodName) != -1) {

list.add(methods[index]);

}

}

return list;

}

public Integer getInt(Method method, T entity) throws Exception {

return (Integer) method.invoke(entity, new Object[] {});

}

public String getString(Method method, T entity) throws Exception {

return (String) method.invoke(entity, new Object[] {});

}

public InputStream getBlob(Method method, T entity) throws Exception {

return (InputStream) method.invoke(entity, new Object[] {});

}

public Date getDate(Method method, T entity) throws Exception {

return (Date) method.invoke(entity, new Object[] {});

}

public Integer setInt(Method method, T entity, Integer arg) throws Exception {

return (Integer) method.invoke(entity, new Object[] { arg });

}

public String setString(Method method, T entity, String arg) throws Exception {

return (String) method.invoke(entity, new Object[] { arg });

}

public InputStream setBlob(Method method, T entity, InputStream arg) throws Exception {

return (InputStream) method.invoke(entity, new Object[] { arg });

}

public Date setDate(Method method, T entity, Date arg) throws Exception {

return (Date) method.invoke(entity, new Object[] { arg });

}

public static void main(String[] args) throws Exception {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值