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.PooledConnection
属性名称=“connection.dialect”>MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
1
8
select 1
> > 实体类User.java
@PK(value = "id")
@Entity(table="t_user")
public class User {
private int id ;
私有字符串名称;
私有字符串性别;
私有整数年龄;
私有字符串职业;
@NoColumn
private int kvalue;
public User() {
}
public User(字符串名称, 字符串性别, 整数年龄, 字符串职业) {
super();
this.name = 姓名;
this.sex = 性别;
this.age = 年龄;
}
public User(整数 id, 字符串姓名, 字符串性别, 整数年龄, 字符串职业) {
super();
this.id = id;
this.name = 姓名;
this.sex = 性别;
this.age = 年龄;
}
public int getId() {
}
public void setId(int id) {
}
public String getName() {
}
public void setName(String name) {
}
public String getSex() {
}
public void setSex(String sex) {
}
public Integer getAge() {
}
public void setAge(Integer Age) {
}
public String getCareer() {
}
public void setCareer(String Career) {
}
public int getKvalue() {
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() {
返回职业;
}
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", "姓名", "性别", "年龄", "职业"]");
return sb.toString();
}
}
> 对User的增删改查,UserCrudTest.java,记得导入junit-4.8.2.jar
public class UserCrudTest {
static 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 = 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 = 新用户(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 = 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() {
用户 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
System.out.println(users.size());
System.out.println(users);
}
}
作者“风故故,也依依”