一句话描述,使用AHibernate框架进行sqlite数据库操作您将从重复性劳动中解脱出来.
(一)支持功能:
1.自动建表,支持属性来自继承类:可根据注解自动完成建表,并且对于继承类中的注解字段也支持自动建表.
2.自动支持增删改,增改支持对象化操作:增删改是数据库操作的最基本单元,不用重复写这些增删改的代码,并且添加和更新支持类似于hibernate中的对象化操作.
3.查询方式灵活:支持android框架提供的方式,也支持原生sql方式.
4.查询结果对象化:对于查询结果可自动包装为实体对象,类似于hibernate框架.
5.查询结果灵活:查询结果支持对象化,也支持结果为List<Map<String,String>>形式,这个方法在实际项目中很实用,且效率更好些.
6.日志较详细:因为android开发不支持热部署调试,运行报错时可根据日志来定位错误,这样可以减少运行Android的次数.
(二)不足之处:
1.id暂时只支持int类型,不支持uuid,在sqlite中不建议用uuid.
2.现在每个方法都自己开启和关闭事务,暂时还不支持在一个事务中做多个操作然后统一提交事务.
(三)作者寄语:
昔日有JavaScript借Java发展,今日也希望AHibernate借Hibernate之名发展.希望这个项目以后会成为开源社区的重要一员,更希望这个项目能给所有Android开发者带便利.欢迎访问我的博客:http://blog.csdn.net/lk_blog,这里有这个框架的使用范例和源码,希望朋友们多多交流完善这个框架,共同推动中国开源事业的发展,AHibernate期待与您共创美好未来!!!
框架源代码见下一篇博客:http://blog.csdn.net/lk_blog/article/details/7456125 ,源代码供大家交流使用,欢迎朋友们对代码提供宝贵意见.
先看一个使用例子吧,看看您是否满意:
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo;
import java.util.List;
import java.util.Map;
import com.tgb.lk.demo.R;
import com.tgb.lk.demo.dao.impl.StudentDaoImpl;
import com.tgb.lk.demo.dao.impl.TeacherDaoImpl;
import com.tgb.lk.demo.model.Student;
import com.tgb.lk.demo.model.Teacher;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//熟悉用接口的朋友注意哦,这里也可以定义为接口哦,见StudentDaoImpl.java中的注释.
TeacherDaoImpl teacherDao = new TeacherDaoImpl(MainActivity.this);
StudentDaoImpl studentDao = new StudentDaoImpl(MainActivity.this);
// 添加
Teacher teacher = new Teacher();
teacher.setName("米老师");
teacher.setAge(50);
teacher.setTitle("教授");
Long teacherId = teacherDao.insert(teacher);
Student student1 = new Student();
student1.setName("lk");
student1.setAge(26);
student1.setClasses("<span style="font-size:18px;">五期提高班</span>");
student1.setTeacherId(teacherId.intValue());
Long studentId1 = studentDao.insert(student1);
Student student2 = new Student();
student2.setName("cls");
student2.setAge(26);
student2.setClasses("<span style="font-size:18px;">五期提高班</span>");
student2.setTeacherId(teacherId.intValue());
Long studentId2 = studentDao.insert(student2);
Student student3 = new Student();
student3.setName("lb");
student3.setAge(27);
student3.setClasses("五期提高班");
student3.setTeacherId(teacherId.intValue());
Long studentId3 = studentDao.insert(student3);
// 查询
// 方式1:根据Id查询单个对象
// 结果:student1Student [id=1, name=lk,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
Student student4 = studentDao.get(studentId1.intValue());
System.out.println("student4" + student4);
// 方式2:查询出表中的所有记录
// 执行结果如下:
// list1:Student [id=1, name=lk,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
// list1:Student [id=2, name=cls,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
// list1:Student [id=3, name=lb,age=27,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
List<Student> list1 = studentDao.find();
for (Student student : list1) {
System.out.println("list1:" + student);
}
// 方式3:限制条件查询和查询结果
// 执行结果:list2:Student [id=2, name=cls,age=0,teacherId=0, classes=null]
List<Student> list2 = studentDao.find(new String[] { "id", "name" },
" id = ? ", new String[] { studentId2.toString() }, null, null,
null, null);
for (Student student : list2) {
System.out.println("list2:" + student);
}
// 方式4:使用sql查询出结果,此种方式是2,3,4中最灵活的.
// 执行结果:
// list3:Student [id=2, name=cls,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
// list3:Student [id=3, name=lb,age=27,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
List<Student> list3 = studentDao.rawQuery(
"select * from t_student where id in (?,?) ", new String[] {
studentId2.toString(), studentId3.toString() });
for (Student student : list3) {
System.out.println("list3:" + student);
}
// 方式4进阶:如果想查询出米老师的学生,可以这样实现:
// 执行结果:
// list4:Student [id=1, name=lk,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
// list4:Student [id=2, name=cls,age=26,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
// list4:Student [id=3, name=lb,age=27,teacherId=1, classes=<span style="font-size:18px;">五期提高班</span>]
List<Student> list4 = studentDao
.rawQuery(
"select s.* from t_student s join t_teacher t on s.teacher_id = t.id where t.name= ? ",
new String[] { "米老师" });
for (Student student : list4) {
System.out.println("list4:" + student);
}
// 方式5:我只想知道姓名和年龄,查询得到List<Map<String,String>>形式.只查2个字会比查询所有字段并封装为对象效率高吧,尤其字段值很多时我们的手机更喜欢这种方式哦.
// 结果:
// listMap1: name:lk;age:26
// listMap1: name:cls;age:26
// listMap1: name:lb;age:27
List<Map<String, String>> listMap1 = studentDao.query2MapList(
"select name,Age from t_student ", null);
for (Map<String, String> map : listMap1) {
// 查询的List中的map以查询sql中的属性值的小写形式为key,注意是小写形式哦.
System.out.println("listMap1: name:" + map.get("name") + ";age:"
+ map.get("age"));
}
// 方式5进阶:我想知道前2名学生的姓名和班主任姓名,这种方式是不是超灵活啊,用其他的方式查询都没这种方式好用吧,哈哈.
// 结果:
// listMap2: student_name:lk;teacher_name:米老师
// listMap2: student_name:cls;teacher_name:米老师
List<Map<String, String>> listMap2 = studentDao
.query2MapList(
"select s.name sname,t.name tname from t_student s join t_teacher t on s.teacher_id = t.id limit ? ",
new String[] { "2" });
for (Map<String, String> map : listMap2) {
System.out.println("listMap2: student_name:" + map.get("sname")
+ ";teacher_name:" + map.get("tname"));
}
// 更新
// 结果: Student [id=1, name=李坤,age=26,teacherId=1, classes=<span style="font-size:18px;">五期</span>]
student1 = studentDao.get(studentId1.intValue());
student1.setName("李坤");
student1.setClasses("五期");
studentDao.update(student1);
System.out.println(student1);
// 删除:支持单个id删除,也支持多个id同时删除哦.
studentDao.delete(studentId1.intValue());
studentDao.delete(new Integer[] { studentId2.intValue(),
studentId3.intValue() });
// 支持执行sql语句哦.
teacherDao.execSql("insert into t_teacher(name,age) values('米教授',50)", null);
}
}</span>
使用AHibernate步骤:
步骤1.引入AHibernate-1.0.jar 下载地址: http://download.csdn.net/detail/lk_blog/4222048 解压后有源码和jar包,这个jar体积非常小,仅有十几K,对您的程序整体大小不会产生影响哦.
步骤2.建实体类
Person.java:
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.model;
import com.tgb.lk.ahibernate.annotation.Column;
import com.tgb.lk.ahibernate.annotation.Id;
//此处没有加Table属性,它是其他类的基类,本类中用@Column注解的字段在子类中同样会被创建到表中.
public class Person {
@Id
@Column(name = "id")
private int id; // 主键,int类型,数据库建表时此字段会设为自增长
@Column(name = "name", length = 20)
private String name; // 名字长度一般不会超过20个字符吧,length=20数据字段的长度是20
@Column(name = "age", type = "INTEGER")
private int age; // 年龄一般是数值,用type = "INTEGER"规范一下吧.
// //假设您开始时没有此属性,程序开发中才想到此属性,去掉代码注释试试吧,数据库增删改查不用修改任何代码哦.
// @Column(name = "sex")
// private String sex;
// 有些字段您可能不希望保存到数据库中,不用@Column注释就不会映射到数据库.
private String noSaveFild;
//get和set方法.
//....
@Override
public String toString() {
return "id=" + id + ", name=" + name + ",age=" + age;
}
}
</span>
Teacher.java
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.model;
import com.tgb.lk.ahibernate.annotation.Column;
import com.tgb.lk.ahibernate.annotation.Table;
//自动生成的建表语句:
//crate table [t_teacher]: CREATE TABLE t_teacher (id INTEGER primary key autoincrement, title TEXT, name TEXT(20), age INTEGER )
@Table(name = "t_teacher")
public class Teacher extends Person {
@Column(name = "title")
private String title;// 职称
//get和set方法.
//....
@Override
public String toString() {
return "Teacher [" + super.toString() + ",title=" + title + "]";
}
}
</span>
Student.java
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.model;
import com.tgb.lk.ahibernate.annotation.Column;
import com.tgb.lk.ahibernate.annotation.Table;
//自动生成的建表语句:
//CREATE TABLE t_student (id INTEGER primary key autoincrement, classes TEXT, teacher_id INTEGER, name TEXT(20), age INTEGER )
@Table(name = "t_student")
public class Student extends Person {
@Column(name = "teacher_id")
private int teacherId;// 班主任id
@Column(name = "classes")
private String classes;// 班级
//get和set方法
//...
@Override
public String toString() {
return "Student [" + super.toString() + ",teacherId=" + teacherId
+ ", classes=" + classes + "]";
}
}
</span>
步骤3:
DBHelper.java
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.util;
import com.tgb.lk.ahibernate.util.MyDBHelper;
import com.tgb.lk.demo.model.Student;
import com.tgb.lk.demo.model.Teacher;
import android.content.Context;
public class DBHelper extends MyDBHelper {
private static final String DBNAME = "school.db";// 数据库名
private static final int DBVERSION = 1;
private static final Class<?>[] clazz = { Teacher.class, Student.class };// 要初始化的表
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION, clazz);
}
}
</span>
步骤4:
StudentDaoImpl.java
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.dao.impl;
import com.tgb.lk.ahibernate.dao.impl.BaseDaoImpl;
import com.tgb.lk.demo.model.Student;
import com.tgb.lk.demo.util.DBHelper;
import android.content.Context;
//如果您是J2EE高手一定希望支持接口吧,按下面的写法即可:
//写一个接口:public interface StudentDao extends BaseDao<Student> {}
//实现接口: public class StudentDaoImpl extends BaseDaoImpl<Student> implements StudentDao
public class StudentDaoImpl extends BaseDaoImpl<Student> {
public StudentDaoImpl(Context context) {
super(new DBHelper(context));
}
}
</span>
TeacherDaoImpl.java
[java] view plaincopy
<span style="font-size:18px;">package com.tgb.lk.demo.dao.impl;
import com.tgb.lk.ahibernate.dao.impl.BaseDaoImpl;
import com.tgb.lk.demo.model.Teacher;
import com.tgb.lk.demo.util.DBHelper;
import android.content.Context;
public class TeacherDaoImpl extends BaseDaoImpl<Teacher> {
public TeacherDaoImpl(Context context) {
super(new DBHelper(context));
}
}
</span>
步骤5:
运行文章开始处的例子试试吧.
程序运行时会根据注解自动建表,增删改查的数据库访问层不用写其他的代码就能直接使用.