使用Jorm简单的增删查改数据库
> 准备
以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内容:
<?xml version='1.0' encoding="UTF-8"?>
<jdbc-configuration>
<constant name="show_sql" value="true" />
<constant name="jdbc.batch_size" value="600" />
<constant name="bytecode.provider" value="cglib" />
<connections default="simple">
<connection name="simple">
<property name="connection.implementation">org.javaclub.jorm.jdbc.connection.impl.SimpleConnection</property>
<property name="connection.dialect">MySQLDialect</property>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.jdbcurl">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.database">test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
</connection>
<connection name="c3p0">
<property name="connection.implementation">org.javaclub.jorm.jdbc.connection.impl.PooledConnection</property>
<property name="connection.dialect">MySQLDialect</property>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.jdbcurl">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.database">test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool.min">1</property>
<property name="connection.pool.max">8</property>
<property name="connection.test.sql">select 1</property>
</connection>
</connections>
</jdbc-configuration>
> 实体类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 < 600; 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 < 600; 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<User> params = new SqlParams<User>();
params.setObjectClass(User.class);
params.setFirstResult(8);
params.setMaxResults(20);
List<User> users = session.list(params);
System.out.println(users.size());
System.out.println(users);
}
}
以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内容:
<?xml version='1.0' encoding="UTF-8"?>
<jdbc-configuration>
<constant name="show_sql" value="true" />
<constant name="jdbc.batch_size" value="600" />
<constant name="bytecode.provider" value="cglib" />
<connections default="simple">
<connection name="simple">
<property name="connection.implementation">org.javaclub.jorm.jdbc.connection.impl.SimpleConnection</property>
<property name="connection.dialect">MySQLDialect</property>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.jdbcurl">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.database">test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
</connection>
<connection name="c3p0">
<property name="connection.implementation">org.javaclub.jorm.jdbc.connection.impl.PooledConnection</property>
<property name="connection.dialect">MySQLDialect</property>
<property name="connection.driver">com.mysql.jdbc.Driver</property>
<property name="connection.jdbcurl">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8</property>
<property name="connection.database">test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool.min">1</property>
<property name="connection.pool.max">8</property>
<property name="connection.test.sql">select 1</property>
</connection>
</connections>
</jdbc-configuration>
> 实体类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 < 600; 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 < 600; 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<User> params = new SqlParams<User>();
params.setObjectClass(User.class);
params.setFirstResult(8);
params.setMaxResults(20);
List<User> users = session.list(params);
System.out.println(users.size());
System.out.println(users);
}
}
相关推荐
通过JORM,开发者可以将Java对象轻松地映射到数据库表,简化了数据库操作的复杂性,同时也提高了代码的可维护性和可读性。 JORM与Speedo JDO(Java Data Objects)的整合也是其亮点。JDO是一种标准的接口,允许...
随着新版本的发布,官方文档应该也得到了相应的更新,对新增功能和改进进行了详细的解释,以便开发者更好地理解和使用P4JORM 1.0.3。 7. **兼容性增强**: P4JORM 1.0.3可能增强了对不同JVM版本、数据库系统以及...
jorm-1.0.7.GA-javadoc
P4JORM,全称为“Perforce JORM”,是一款专为Java应用程序设计的对象关系映射(ORM)框架,它旨在简化数据库操作,通过提供一个高级接口来处理SQL查询和数据持久化。在P4JORM 1.0.1版本中,主要修复了一个与Java ...
This project is based on Java, is a lightweight ORM model. Only concerned about the Object-Relationl Mapping, therefore more simple and easier to use, easier to control. Key support functions and ...
开始那个版本是1.7,有些同学遇到“java.lang.UnsupportedClassVersionError: Bad version ”问题,如果是jdk 1.6,请用此版本,或者自己编译一个,因为已发布源码
不用写sql啦,不用创建表啦,一个比mybatis更轻量级,比hibernate好用的orm!
你还写jdbc呢?还在反复HQL拼写工作?p4jorm让你无需写一行代码,即可进行CURD,真正的简单易学。本人用了3天时间完成,还请各位多多指点 http://blog.csdn.net/partner4java/article/details/8559661 ...
**标题解析:** ...综上所述,jorm-unit 是一个面向Java开发者的测试工具,它简化了使用JPA的项目的单元测试,特别是在没有真实数据库服务器的环境中。它对于提高测试质量和开发效率有着显著的帮助。
在描述中提到,该OR-Mapper坚持使用简单的Pojo(Plain Old Java Object)或者处理复杂的对象图。Pojo是指没有任何特殊框架依赖或注解的普通Java对象,它们易于理解和维护。通过ORM,这些Pojo可以直接与数据库表对应...
4. **SAP DB, Hypersonic/Hsql, McKoi, Xindice, OJB, Hibernate, Castor, Torque, JORM, XMLBeans, JiBX, JAXB Reference Implementation, JaxMe**:这些是数据库访问和对象关系映射(ORM)工具,它们帮助开发者在 ...
其间,也诞生了许多优秀的MVC框架,如专注于控制层的Struts、WebWork, Struts2, JSF等框架,专注于业务逻辑方面的Spring框架、专注于持久层的Hibernate、iBatis、Castor、JORM等框架。由于最近用了一次SSI框架,因此...
健康的决定因素 健康的社会决定因素的开源可视化。 参考 NCHHSTP健康的社会决定因素。 (2014)。 检索自2016年3月14日,从检索 健康的决定因素。...和Jorm,L.(2010年12月)。 公共卫生分类项目(共和
最近收到的书籍 ...JORM, AF 阅读和拼写障碍的心理学。 波士顿:Routledge & Kegan Paul,1983 年,134 页,价格不详。 KNOWLES, D., & REEVES, N. 但是奶奶不需要她的袜子吗? 爱荷华州迪比克