使用Jorm处理特殊字段
> 引言
有时候我们有这样的需求,对象有一个属性可能有多个值,需要在数据库中作为一个字段存储
有时候我们有这样的需求,对象有一个属性可能有多个值,需要在数据库中作为一个字段存储
还是以User为例,career存储多个职业
> 建表
以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;
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;
> 代码
实体类 User.java
@Entity(table = "t_user")
@PK(value = "id")
public class User implements Serializable {
/** desc */
private static final long serialVersionUID = -4750351638245912867L;
@Id
private int id;
private String name;
private String sex;
private Integer age;
@Basic(processor=DefinedFieldProcessor.class)
private String[] career;
@NoColumn
private int kvalue;
public JawaUser() {
super();
}
public JawaUser(String name, String sex, Integer age, String[] career) {
super();
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() {
return "User [age=" + age + ", career=" + Arrays.toString(career)
+ ", id=" + id + ", kvalue=" + kvalue + ", name=" + name
+ ", sex=" + sex + "]";
}
}
@PK(value = "id")
public class User implements Serializable {
/** desc */
private static final long serialVersionUID = -4750351638245912867L;
@Id
private int id;
private String name;
private String sex;
private Integer age;
@Basic(processor=DefinedFieldProcessor.class)
private String[] career;
@NoColumn
private int kvalue;
public JawaUser() {
super();
}
public JawaUser(String name, String sex, Integer age, String[] career) {
super();
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() {
return "User [age=" + age + ", career=" + Arrays.toString(career)
+ ", id=" + id + ", kvalue=" + kvalue + ", name=" + name
+ ", sex=" + sex + "]";
}
}
属性字段处理类 DefinedFieldProcessor.java
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.javaclub.jorm.Session;
import org.javaclub.jorm.common.CommonUtil;
import org.javaclub.jorm.common.Reflections;
import org.javaclub.jorm.jdbc.process.FieldProcessor;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.javaclub.jorm.Session;
import org.javaclub.jorm.common.CommonUtil;
import org.javaclub.jorm.common.Reflections;
import org.javaclub.jorm.jdbc.process.FieldProcessor;
public class DefinedFieldProcessor implements FieldProcessor {
public Object insert(Session session, Object entity, Field field) {
String[] crs = (String[]) Reflections.getFieldValue(entity, field);
if(!CommonUtil.isEmpty(crs)) {
StringBuilder sbf = new StringBuilder();
for (int i = 0; i < crs.length; i++) {
if(i > 0) {
sbf.append(",");
}
sbf.append(crs[i]);
}
return sbf.toString();
}
return "";
}
public void load(Session session, Object entity, Field field, ResultSet rs,
int idx) throws SQLException {
String str = rs.getString(idx);
String[] crs = str.split(",");
Reflections.setFieldValue(entity, field, crs);
}
}
> 测试
import org.javaclub.jorm.Jorm;
import org.javaclub.jorm.Session;
import org.javaclub.jorm.common.Numbers;
import org.javaclub.jorm.common.Strings;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.javaclub.jorm.Session;
import org.javaclub.jorm.common.Numbers;
import org.javaclub.jorm.common.Strings;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class FieldProcessorTest {
static Session session;
@BeforeClass
public static void setUpBeforeClass() {
session = Jorm.getSession();
}
@AfterClass
public static void destroy() {
Jorm.free();
}
@Test
public void test_save() {
session.clean(User.class);
User u;
for (int i = 0; i < 100; i++) {
String sex = (i % 2 == 0 ? "男" : "女");
String[] cr = {};
if(i % 3 == 0) {
cr = new String[] {Strings.fixed(2), Strings.random(5), Strings.fixed(6)};
} else if(i % 3 == 1) {
cr = new String[] {Strings.fixed(2), Strings.random(5)};
} else {
cr = new String[] {Strings.fixed(2)};
}
u = new User(Strings.fixed(6), sex, Numbers.random(100), cr);
session.save(u);
}
for (int i = 0; i < 10; i++) {
u = session.read(User.class, i + 1);
System.out.println(u);
}
}
}
Jorm项目地址:http://javaclub.sourceforge.net/jorm.html