AHibernate简介
一句话描述,使用AHibernate框架进行sqlite数据库操作您将从重复性劳动中解脱出来.
6.日志较详细:因为android开发不支持热部署调试,运行报错时可根据日志来定位错误,这样可以减少运行Android的次数.
(二)不足之处:(三)作者寄语:
框架源代码见下一篇博客:http://blog.csdn.net/lk_blog/article/details/7456125,源代码供大家交流使用,欢迎朋友们对代码提供宝贵意见.
先看一个使用例子吧,看看您是否满意:
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("五期提高班");
student1.setTeacherId(teacherId.intValue());
Long studentId1 = studentDao.insert(student1);
Student student2 = new Student();
student2.setName("cls");
student2.setAge(26);
student2.setClasses("五期提高班");
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=五]
Student student4 = studentDao.get(studentId1.intValue());
System.out.println("student4" + student4);
// 方式2:查询出表中的所有记录
// 执行结果如下:
// list1:Student [id=1, name=lk,age=26,teacherId=1, classes=五期提高班]
// list1:Student [id=2, name=cls,age=26,teacherId=1, classes=五期提高班]
// list1:Student [id=3, name=lb,age=27,teacherId=1, classes=五期提高班]
List 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 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=五期提高班]
// list3:Student [id=3, name=lb,age=27,teacherId=1, classes=五期提高班]
List 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=五期提高班]
// list4:Student [id=2, name=cls,age=26,teacherId=1, classes=五期提高班]
// list4:Student [id=3, name=lb,age=27,teacherId=1, classes=五期提高班]
List 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
步骤1.引入AHibernate-1.0.jar 下载地址:http://download.csdn.net/detail/lk_blog/4222048解压后有源码和jar包,这个jar体积非常小,仅有十几K,对您的程序整体大小不会产生影响哦.
步骤2.建实体类
Person.java:
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;
}
}
Teacher.javapackage 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 + "]";
}
}
Student.java
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 + "]";
}
}
步骤3:
DBHelper.java
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);
}
}
StudentDaoImpl.java
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 {}
//实现接口: public class StudentDaoImpl extends BaseDaoImpl implements StudentDao
public class StudentDaoImpl extends BaseDaoImpl {
public StudentDaoImpl(Context context) {
super(new DBHelper(context));
}
}
TeacherDaoImpl.javapackage 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 {
public TeacherDaoImpl(Context context) {
super(new DBHelper(context));
}
}
运行文章开始处的例子试试吧.
程序运行时会根据注解自动建表,增删改查的数据库访问层不用写其他的代码就能直接使用.
最后,补充一些关于查看日志的内容,日志可是我们调试程序的利器哦:
(1)在LogCat中设置查看日志:
(2)日志输出结果: