bitscn.com
> 准备
以mysql为例,执行下面的sql建立数据表
create table `t_user` (
`id` int(11) not null,
`name` varchar(50) default null,
`sex` char(4) default null,
`age` int(11) default null,
`career` varchar(100) default null,
primary key (`id`)
) engine=innodb default charset=utf8;
> 引入jar或maven依赖,需要jar包
gerald-jorm-1.0.5.jar 最新版本下载:http://sourceforge.net/projects/javaclub/files
commons-logging-1.1.1.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.6.jar
javassist-3.11.0.ga.jar 或 cglib-nodep-2.2.2.jar (根据实际情况选择性加入)
> 配置文件
在你的java工程的classpath下建立config.properties和jdbc.cfg.xml文件
config.properties内容:
# 下面路径可以根据实际情况指定,为相对classpath的路径地址
jdbc.config.path=jdbc.cfg.xml
jdbc.cfg.xml内容:
org.javaclub.jorm.jdbc.connection.impl.simpleconnection
mysqldialect
com.mysql.jdbc.driver
jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf-8
test
root
root
org.javaclub.jorm.jdbc.connection.impl.pooledconnection
mysqldialect
com.mysql.jdbc.driver
jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf-8
test
root
root
1
8
select 1
> 实体类user.java
@pk(value = id)
@entity(table=t_user)
public class user {
@id
private int id;
private string name;
private string sex;
private integer age;
private string career;
@nocolumn
private int kvalue;
public user() {
super();
}
public user(string name, string sex, integer age, string career) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.career = career;
}
public user(integer id, string name, string sex, integer age, string career) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.career = career;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getsex() {
return sex;
}
public void setsex(string sex) {
this.sex = sex;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public string getcareer() {
return career;
}
public void setcareer(string career) {
this.career = career;
}
public int getkvalue() {
return kvalue;
}
public void setkvalue(int kvalue) {
this.kvalue = kvalue;
}
public string tostring() {
stringbuffer sb = new stringbuffer();
sb.append([ + id + , + name + , + sex + , + age + , + career + ]);
return sb.tostring();
}
}
这里数据库字段和java实体类user的属性在命名上是一致的,如果不一致,比如如果表创建sql为:
create table `t_user` (
`user_id` int(11) not null,
`user_name` varchar(50) default null,
`sex` char(4) default null,
`col_age` int(11) default null,
`career_job` varchar(100) default null,
primary key (`id`)
) engine=innodb default charset=utf8;
那么对应的实体user应该写成:
@pk(value = id)
@entity(table=t_user)
public class user {
@id
@column(user_id)
private int id;
@column(user_name)
private string name;
// 与数据库字段命名一致,可以不指定@column
private string sex;
@column(col_age)
private integer age;
@column(career_job)
private string career;
@nocolumn
private int kvalue;
public user() {
super();
}
public user(string name, string sex, integer age, string career) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.career = career;
}
public user(integer id, string name, string sex, integer age, string career) {
super();
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.career = career;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string getsex() {
return sex;
}
public void setsex(string sex) {
this.sex = sex;
}
public integer getage() {
return age;
}
public void setage(integer age) {
this.age = age;
}
public string getcareer() {
return career;
}
public void setcareer(string career) {
this.career = career;
}
public int getkvalue() {
return kvalue;
}
public void setkvalue(int kvalue) {
this.kvalue = kvalue;
}
public string tostring() {
stringbuffer sb = new stringbuffer();
sb.append([ + id + , + name + , + sex + , + age + , + career + ]);
return sb.tostring();
}
}
> 对user的增删查改,usercrudtest.java,记得引入junit-4.8.2.jar
public class usercrudtest {
static session session;
@beforeclass
public static void before() {
session = jorm.getsession();
}
@afterclass
public static void after() {
jorm.free();
}
@test
public void save_user() {
session.clean(user.class);
user user = null;
for (int i = 0; i string sex = (i % 2 == 0 ? 男 : 女);
user = new user(strings.fixed(5), sex, numbers.random(98), strings.random(8));
session.save(user);
}
}
@test // 批量保存
public void batch_save_user() {
session.clean(user.class);
jdbcbatcher batcher = session.createbatcher();
user user = null;
for (int i = 0; i string sex = (i % 2 == 0 ? 男 : 女);
user = new user(strings.fixed(5), sex, numbers.random(98), strings.random(8));
batcher.save(user);
}
batcher.execute();
}
@test
public void loaduser() {
user user = session.read(user.class, 1);
// 这里user是一个代理对象,因为@entity(table=t_user, lazy = true)
system.out.println(user.getcareer());// 发出查询sql
}
@test
public void deletuser() {
user user = session.read(user.class, 1);
if(null != user) {
session.delete(user);
}
user = session.read(user.class, 1);
system.out.println(user);
}
@test
public void test_update_proxy() {
user u;
u = session.read(user.class, 2);
assert.assertnotnull(u);
assert.asserttrue(u instanceof jormproxy);
u.setname(gerald.chen);
session.update(u);
system.out.println(u.getname());
u = session.read(user.class, 2);
assert.asserttrue(gerald.chen.equals(u.getname()));
}
@test
public void queryuser() {
sqlparams params = new sqlparams();
params.setobjectclass(user.class);
params.setfirstresult(8);
params.setmaxresults(20);
list users = session.list(params);
system.out.println(users.size());
system.out.println(users);
}
}
作者“风故故,也依依”
bitscn.com
