sqlite-annotation-convention and@H_502_1@@H_502_1@sqlite-java-tool@H_502_1@@H_502_1@@H_502_1@@H_502_1@@H_502_1@@H_502_1@
http://pan.baidu.com/s/1eQuwa34@H_502_1@@H_502_1@@H_502_1@@H_502_1@@H_502_1@
最近一直在做毕业设计,之前用过Hibernate@H_502_1@,发现Hibernate@H_502_1@用起来真的很烦便。在android@H_502_1@中数据库为sqlite@H_502_1@,没有类似Hibernate@H_502_1@的工具,使得开发过程中时不时的回去查看sqlite@H_502_1@的sql@H_502_1@语句的编写,非常影响开发效率。@H_502_1@
@H_502_1@偶然间在CSDN@H_502_1@的博客中发现了一个用于android@H_502_1@的类似Hibernate@H_502_1@的工具。该工具lk_blog@H_502_1@大大开发的一款专门适用于android sqlite@H_502_1@的一款数据库操作工具,其原理是通过Annotation@H_502_1@帮助程序员生成sqlite@H_502_1@的sql@H_502_1@语句,并通过android@H_502_1@中的sqliteDataBaseHelper@H_502_1@获取sqliteDataBase@H_502_1@然后执行工具生成的sql@H_502_1@语句,从而达到操作数据库的目的。@H_502_1@
@H_502_1@以上只是对AHibernate@H_502_1@的一些简单的概述,只是讲述了AHibernate@H_502_1@的基本原理,更详细的内容大家去看lk_blog@H_502_1@的博客吧。http://www.jb51.cc/article/p-ffyujtpq-rw.html@H_502_1@@H_502_1@
@H_502_1@我在这里写文章当然是为了推广我写的sqlite@H_502_1@工具啦。笔者在看了lk_blog@H_502_1@大大写的工具后很感兴趣,研究AHibernate@H_502_1@源码后恍然大悟,遂萌发了开发一个适用于多平台的数据库工具,这就是我独立开发的:@H_502_1@@H_502_1@
@H_502_1@1. sqlite-annotation-convention@H_502_1@帮助生成sql@H_502_1@语句的注解插件;@H_502_1@
@H_502_1@2. sqlite-java-tool pc@H_502_1@端(包括windows@H_502_1@,linux@H_502_1@,mac@H_502_1@)下的sqlite@H_502_1@数据库操作工具,该工具是基于sqlite-annotation-convention@H_502_1@插件的,实现了许多常用的操作,你也可以直接编写sql@H_502_1@代码,然后通过该工具执行。@H_502_1@
@H_502_1@下面我们进入正题,具体来讲解这两个插件的实现原理和使用方法。@H_502_1@
@H_502_1@ @H_502_1@在源码包中org.lion.java.sqlite.hibernate.annotation@H_502_1@的这个包内包含了所有注解元素。@H_502_1@@H_502_1@@H_502_1@
@H_502_1@ @H_502_1@@Table@H_502_1@有两个属性一个是value@H_502_1@,另外一个是primaryKeys@H_502_1@。value@H_502_1@是用来为表添加名称的属性,primaryKeys@H_502_1@是@PrimaryKey@H_502_1@的数组类型(@PrimaryKey@H_502_1@的默认value@H_502_1@属性就是主键对应的在类中的属性名称),primaryKeys@H_502_1@是用来为表添加复合主键的属性,如果需要自增的主键我们就用不到这个属性了。@H_502_1@
@H_502_1@@Tables@H_502_1@注解是声明某个类需要在数据库中创建多张表的注解,其默认属性value@H_502_1@就是@Table@H_502_1@的数组类型,你可以通过使用该注解将一个类声明为多个表,然后生成sql@H_502_1@的帮助类会为你创建多张表。@H_502_1@
@H_502_1@ @H_502_1@如果类中某一个属性被声明了@Id@H_502_1@那么它将会作为唯一该表的唯一主键,并且自增;如果你想省事,同样我们也提供不加注解自动生成自增的主键,你只需要在类中声明一个整形的属性,并且其名称为“id”@H_502_1@即可,sqliteTableHelper@H_502_1@助手类会根据你定义的名称以及注解为你生成自增的主键。@H_502_1@
@H_502_1@@Column@H_502_1@中包含很多属性,诸如notnull@H_502_1@,unique@H_502_1@等属性,你可以根据你的需要使用这些属性,默认情况下这些属性都是无效的。@H_502_1@
@H_502_1@ @H_502_1@@H_502_1@
//你可以像这样没有注解 class Model3 { private int id; private String value; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } //你同样可以使用基类,子类继承基类即可 public class Parent { private String property; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getProperty() { return property; } public void setProperty(String property) { this.property = property; } } import org.lion.java.sqlite.hibernate.annotation.Table; @Table class Model2 extends Parent{ private String value; private int id; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } //你还可以这样使用 import org.lion.java.sqlite.hibernate.annotation.Column; import org.lion.java.sqlite.hibernate.annotation.ForeignKey; import org.lion.java.sqlite.hibernate.annotation.Id; import org.lion.java.sqlite.hibernate.annotation.Table; @Table class Model1 { @Id private int modelId; @Column( unique=true,notnull=true ) private String name; @Column( check="age>18" ) private int age; @Column( default_value="only.night@qq.com" ) private String email; @Column( foreignkey=@ForeignKey( srcClass=Model2.class,column="id",onInsert=true ) ) private int model2Id; @Column( foreignkey=@ForeignKey( srcClass=Model3.class,column="id" ) ) private int model3Id; public int getModelId() { return modelId; } public void setModelId(int modelId) { this.modelId = modelId; } 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; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getModel2Id() { return model2Id; } public void setModel2Id(int model2Id) { this.model2Id = model2Id; } public int getModel3Id() { return model3Id; } public void setModel3Id(int model3Id) { this.model3Id = model3Id; } }@H_502_1@
2. sql@H_502_1@生成助手使用方法@H_502_1@
@H_502_1@
//你可以这样使用,下面这个其实就是sqlite-java-tool工具的全部代码,该工具就是试用了工具助手从而操作数据库的 import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.sqlException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Set; import org.lion.java.sqlite.hibernate.DataBaseDector; import org.lion.java.sqlite.hibernate.sqliteDataBaseSession; import org.lion.java.sqlite.hibernate.sqliteTableHelper; import org.lion.java.sqlite.hibernate.sqliteUtils; import org.lion.java.sqlite.hibernate.TableModel; import org.lion.java.sqlite.hibernate.annotation.Column; import org.sqlite.JDBC; public class sqliteDataBase implements sqliteUtils,DataBaseDector{ public static final String JDBC_DRIVER_CLASS = "org.sqlite.JDBC"; private Connection connection; private Statement statement; private sqliteDataBaseSession session; /** * create a database session to manage the sqlite database * @param dbName such as * @Windows D://sqlite/sqlite.sqlite * @Linux /home/username/sqlite.sqlite * @param packageToScan package to scan,in the package all the class will user for database model */ public sqliteDataBase( String dbName,String packageToScan ) { try { Class.forName( JDBC.class.getName() ); connection = DriverManager.getConnection( JDBC.PREFIX + dbName ); statement = connection.createStatement(); session = new sqliteDataBaseSession( dbName,packageToScan ); this.createTables(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (sqlException e) { e.printStackTrace(); } } /** * create a database session to manage the sqlite database * @param dbName * @Windows D://sqlite/sqlite.sqlite * @Linux /home/username/sqlite.sqlite * @param classes models classes */ public sqliteDataBase( String dbName,Class<?>[] classes ) { try { Class.forName( JDBC.class.getName() ); connection = DriverManager.getConnection( JDBC.PREFIX + dbName ); statement = connection.createStatement(); session = new sqliteDataBaseSession( dbName,classes ); this.createTables(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (sqlException e) { e.printStackTrace(); } } @Override public boolean isTableExist(String tablename) { String sql = session.isTableExist(tablename); try { System.out.println( sql ); ResultSet set = statement.executeQuery(sql); return set.next(); } catch (sqlException e) { e.printStackTrace(); } return false; } @Override public void createTables() { List<String> tables = session.createTables(); for (String sql : tables) { boolean mark = this.isTableExist(sql.substring( 13,sql.indexOf( "(" )-1 )); if (mark==false) { try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { } } } } @Override public void dropTables() { List<String> tables = session.dropTables(); for (String sql : tables) { try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } } @Override public void insert(String tablename,Object entity) { String sql = session.insert(tablename,entity); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public void insert(String tablename,List<Object> entities) { String sql = session.insert(tablename,entities); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public void delete(String tablename,Object entity) { String sql = session.delete(tablename,entity); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public void delete(String tablename,List<Object> entities) { String sql = session.delete(tablename,entities); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public void update(String tablename,Object entity) { String sql = session.update(tablename,entity); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public void update(String tablename,List<Object> entities) { String sql = session.update(tablename,entities); try { System.out.println( sql ); statement.execute(sql); } catch (sqlException e) { e.printStackTrace(); } } @Override public List<Object> select(String tablename,String column,String value) { String sql = session.select(tablename,column,value); try { System.out.println( sql ); ResultSet set = statement.executeQuery(sql); return getObjectList(tablename,set); } catch (sqlException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Override public List<Object> select(String tablename,String[] columns,String[] values) { String sql = session.select(tablename,columns,values); try { System.out.println( sql ); ResultSet set = statement.executeQuery(sql); return getObjectList(tablename,set); } catch (sqlException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Override public List<Object> selectAll(String tablename) { String sql = session.selectAll(tablename); try { System.out.println( sql ); ResultSet set = statement.executeQuery(sql); return getObjectList(tablename,set); } catch (sqlException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Override public void execute(String tablename,String sql) { String temp = session.execute(tablename,sql); try { System.out.println( temp ); statement.execute(temp); } catch (sqlException e) { e.printStackTrace(); } } @Override public void executeUpdate(String tablename,String sql) { String temp = session.executeUpdate(tablename,sql); try { System.out.println( temp ); statement.executeUpdate(temp); } catch (sqlException e) { e.printStackTrace(); } } @Override public List<Object> executeQurey(String tablename,String sql) { String temp = session.executeQurey(tablename,sql); try { System.out.println( temp ); ResultSet set = statement.executeQuery(temp); return getObjectList(tablename,set); } catch (sqlException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; } @Override public void close() { try { connection.close(); } catch (sqlException e) { e.printStackTrace(); } } public List<Object> getObjectList( String tablename,ResultSet set ) throws sqlException,InstantiationException,IllegalAccessException{ List<Object> objects = new ArrayList<Object>(); Set<TableModel> tableModels = session.getTableModels(); Class<?> tableClass = null; for (TableModel tableModel : tableModels) { if ( tableModel.getTableName().equals(tablename) ) { tableClass = tableModel.getClazz(); break; } } Set<Field> fields = sqliteTableHelper.getAllFields(tableClass); while( set.next() ){ Object entity = tableClass.newInstance(); for (Field field : fields) { Column column = null; field.setAccessible(true); if (field.isAnnotationPresent(Column.class)) { column = (Column) field.getAnnotation(Column.class); if (column.value().equals("") == false) { setFieldValue(field,entity,set,column.value()); } else { setFieldValue(field,field.getName()); } } else { setFieldValue(field,field.getName()); } } objects.add(entity); } return objects; } private void setFieldValue( Field field,Object entity,ResultSet set,String columnName ) throws IllegalAccessException,IllegalArgumentException,sqlException { if ((Integer.TYPE == field.getType() ) || (Integer.class == field.getType())) { field.set(entity,Integer.valueOf(set.getInt(columnName))); } else if (String.class == field.getType()) { field.set(entity,set.getString(columnName)); } else if ((Long.TYPE == field.getType()) || (Long.class == field.getType())) { field.set(entity,Long.valueOf(set.getLong(columnName))); } else if ((Float.TYPE == field.getType()) || (Float.class == field.getType())) { field.set(entity,Float.valueOf(set.getFloat(columnName))); } else if ((Short.TYPE == field.getType()) || (Short.class == field.getType())) { field.set(entity,Short.valueOf(set.getShort(columnName))); } else if ((Double.TYPE == field.getType()) || (Double.class == field.getType())) { field.set(entity,Double.valueOf(set.getDouble(columnName))); } else if (Character.TYPE == field.getType()) { String fieldValue = set.getString(columnName); if ((fieldValue != null) && (fieldValue.length() > 0)) { field.set(entity,Character.valueOf(fieldValue.charAt(0))); } } } }
@H_502_1@ @H_502_1@