diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..56c0c4e --- /dev/null +++ b/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + xyz.wbsite + wbsqlite + 1.0-SNAPSHOT + + + + org.xerial + sqlite-jdbc + 3.14.2.1 + + + \ No newline at end of file diff --git a/src/main/java/RowMapper.java b/src/main/java/RowMapper.java new file mode 100644 index 0000000..2a53630 --- /dev/null +++ b/src/main/java/RowMapper.java @@ -0,0 +1,6 @@ +import java.sql.ResultSet; +import java.sql.SQLException; + +public interface RowMapper { + public abstract T mapRow(ResultSet rs, int index) throws SQLException; +} diff --git a/src/main/java/SqliteClient.java b/src/main/java/SqliteClient.java new file mode 100644 index 0000000..408ac79 --- /dev/null +++ b/src/main/java/SqliteClient.java @@ -0,0 +1,316 @@ +import anonation.Varchar; + +import java.io.File; +import java.lang.reflect.Field; +import java.sql.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * SqliteClient + * + * @author wangbing + */ +public class SqliteClient { + + private Connection connection; + private Statement statement; + private ResultSet resultSet; + private String dbFilePath; + private List obList = new ArrayList<>(); + + /** + * 构造函数 + * + * @param dbFile 文件 + * @param classList 注册对象 + * @throws ClassNotFoundException + * @throws SQLException + */ + public SqliteClient(File dbFile, List classList) throws ClassNotFoundException, SQLException { + this.dbFilePath = dbFile.getAbsolutePath(); + this.obList.addAll(classList); + if (!dbFile.exists()) { + connection = getConnection(); + for (Class object : obList) { + StringBuffer sql = new StringBuffer(); + String name = object.getSimpleName(); + + sql.append("CREATE TABLE "); + sql.append(name); + sql.append(" ("); + + Field[] fields = object.getDeclaredFields(); + for (Field f : fields) { + if (f.isAnnotationPresent(Varchar.class)) { + Varchar bind = f.getAnnotation(Varchar.class); + int length = bind.value(); + sql.append(f.getName().toUpperCase()); + sql.append(" VARCHAR(" + length + "),"); + } + } + + sql.replace(sql.length() - 1, sql.length(), ""); + sql.append(")"); + System.out.println(sql.toString()); + execute(sql.toString()); + } + } + } + + public void insert(T t) throws SQLException, ClassNotFoundException { + try { + for (Class aClass : obList) { + if (t.getClass() == aClass) { + StringBuffer sql = new StringBuffer(); + String tableName = aClass.getSimpleName(); + sql.append("INSERT INTO "); + sql.append(tableName.toUpperCase()); + sql.append("("); + + Map keyValues = new HashMap<>(); + for (Field f : aClass.getDeclaredFields()) { + if (f.isAnnotationPresent(Varchar.class)) { + try { + f.setAccessible(true); + keyValues.put(f.getName().toUpperCase(), f.get(t)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + + StringBuffer fieldsSql = new StringBuffer(); + StringBuffer valueSql = new StringBuffer(); + + List argsList = new ArrayList<>(); + for (String key : keyValues.keySet()) { + fieldsSql.append(key.toUpperCase()); + fieldsSql.append(","); + + Object value = keyValues.get(key); + if (value instanceof String) { + valueSql.append("'"); + valueSql.append(value); + valueSql.append("'"); + } else { + valueSql.append(value); + } + valueSql.append(","); + } + String[] args = new String[argsList.size()]; + argsList.toArray(args); + + if (",".equals(fieldsSql.substring(fieldsSql.length() - 1, fieldsSql.length()))) { + fieldsSql.replace(fieldsSql.length() - 1, fieldsSql.length(), ""); + } + if (",".equals(valueSql.substring(valueSql.length() - 1, valueSql.length()))) { + valueSql.replace(valueSql.length() - 1, valueSql.length(), ""); + } + sql.append(fieldsSql); + sql.append(") VALUES ("); + sql.append(valueSql); + sql.append(")"); + System.out.println(sql.toString()); + executeUpdate(sql.toString()); + } + } + } finally { + destroyed(); + } + } + + public int delete(T t, String... wheres) throws SQLException, ClassNotFoundException { + for (Class aClass : obList) { + if (t.getClass() == aClass) { + StringBuffer sql = new StringBuffer(); + sql.append("delete from "); + sql.append(aClass.getSimpleName()); + + if (wheres.length > 0) { + sql.append(" where"); + for (int i = 0; i < wheres.length; i++) { + String where = wheres[i]; + sql.append(where); + sql.append(i != wheres.length ? " and " : ""); + } + } + return executeUpdate(sql.toString()); + } + } + return 0; + } + + public int update(T t, String... wheres) throws SQLException, ClassNotFoundException { + ArrayList list = new ArrayList<>(); + for (Class aClass : obList) { + if (t.getClass() == aClass) { + StringBuffer sql = new StringBuffer(); + sql.append("update set "); + sql.append(aClass.getSimpleName()); + + if (wheres.length > 0) { + sql.append(" where"); + for (int i = 0; i < wheres.length; i++) { + String where = wheres[i]; + sql.append(where); + sql.append(i != wheres.length ? " and " : ""); + } + } + + System.out.println(sql.toString()); + return executeUpdate(sql.toString()); + } + } + return 0; + } + + public List select(T t, String... wheres) throws SQLException, ClassNotFoundException { + ArrayList list = new ArrayList<>(); + for (Class aClass : obList) { + if (t.getClass() == aClass) { + StringBuffer sql = new StringBuffer(); + sql.append("select * from "); + sql.append(aClass.getSimpleName()); + + if (wheres.length > 0) { + sql.append(" where"); + for (int i = 0; i < wheres.length; i++) { + String where = wheres[i]; + sql.append(where); + sql.append(i != wheres.length ? " and " : ""); + } + } + + System.out.println(sql.toString()); + list.addAll(executeQuery(sql.toString(),t)); + } + } + return list; + } + + /** + * 执行select查询,返回结果列表 + * + * @param sql sql select 语句 + * @param t 结果集的行数据处理类对象 + * @return + * @throws SQLException + * @throws ClassNotFoundException + */ + public List executeQuery(String sql, T t) throws SQLException, ClassNotFoundException { + List rsList = new ArrayList(); + try { + resultSet = getStatement().executeQuery(sql); + //2、获取字段列表 + List fs = new ArrayList<>(); + for (Field f : t.getClass().getDeclaredFields()) { + if (f.isAnnotationPresent(Varchar.class)) { + fs.add(f); + } + } + while (resultSet.next()) { + try { + T o = (T) t.getClass().newInstance(); + for (int i = 0; i < fs.size(); i++) { + Field field = fs.get(i); + field.setAccessible(true); + String string = resultSet.getString(field.getName()); + field.set(o, string); + } + rsList.add(o); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } finally { + destroyed(); + } + return rsList; + } + + /** + * 执行sql语句 + * + * @param sql + * @throws SQLException + * @throws ClassNotFoundException + */ + public void execute(String sql) throws SQLException, ClassNotFoundException { + try { + getStatement().execute(sql); + } finally { + destroyed(); + } + } + + /** + * 执行多个sql更新语句 + * + * @param sqls + * @throws SQLException + * @throws ClassNotFoundException + */ + public int executeUpdate(String... sqls) throws SQLException, ClassNotFoundException { + int count = 0; + try { + for (String sql : sqls) { + count += getStatement().executeUpdate(sql); + } + } finally { + destroyed(); + } + return count; + } + + /** + * 获取数据库连接 + * + * @return 数据库连接 + * @throws ClassNotFoundException + * @throws SQLException + */ + private Connection getConnection() throws ClassNotFoundException, SQLException { + if (null == connection) { + Class.forName("org.sqlite.JDBC"); + connection = DriverManager.getConnection("jdbc:sqlite:" + dbFilePath); + } + return connection; + } + + private Statement getStatement() throws SQLException, ClassNotFoundException { + if (null == statement) { + statement = getConnection().createStatement(); + } + return statement; + } + + /** + * 数据库资源关闭和释放 + */ + public void destroyed() { + try { + + if (null != resultSet) { + resultSet.close(); + resultSet = null; + } + + if (null != statement) { + statement.close(); + statement = null; + } + if (null != connection) { + connection.close(); + connection = null; + } + + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/SqliteTest.java b/src/main/java/SqliteTest.java new file mode 100644 index 0000000..f90e911 --- /dev/null +++ b/src/main/java/SqliteTest.java @@ -0,0 +1,30 @@ +import entity.User; + +import java.io.File; +import java.sql.*; +import java.util.ArrayList; +import java.util.List; + + +public class SqliteTest { + public static void main(String[] args) { + try { + List arrayList = new ArrayList(); + arrayList.add(User.class); + SqliteClient h = new SqliteClient(new File("D:\\test.db"),arrayList); + + User user = new User(); + user.setName("wangbing"); + user.setPassword(null); + h.insert(user); + + List select = h.select(user); + System.out.println(""); + + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } catch (SQLException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/anonation/Varchar.java b/src/main/java/anonation/Varchar.java new file mode 100644 index 0000000..d6d24fd --- /dev/null +++ b/src/main/java/anonation/Varchar.java @@ -0,0 +1,13 @@ +package anonation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Varchar { + + int value() default 20; +} diff --git a/src/main/java/entity/User.java b/src/main/java/entity/User.java new file mode 100644 index 0000000..b9adf11 --- /dev/null +++ b/src/main/java/entity/User.java @@ -0,0 +1,27 @@ +package entity; + +import anonation.Varchar; + +public class User { + + @Varchar(40) + private String name; + @Varchar(40) + private String password; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +}