關於 Java ORM 框架 Orman 的簡單應用

import org.orman.mapper.EntityList; 
import org.orman.mapper.Model; 
import org.orman.mapper.annotation.Entity; 
import org.orman.mapper.annotation.OneToMany; 
import org.orman.mapper.annotation.PrimaryKey; 
@Entity 
public class userFlag extends Model{ 
    @PrimaryKey(autoIncrement=true) 
    public long id; 
    public String flag; 
    @OneToMany(onField = "flagid", toType = userInfo.class)//這裏的onField 填寫 userInfo裏的屬性 
    public EntityList userInfos = new EntityList(userFlag.class, userInfo.class, this); 
}



import org.orman.mapper.Model;
import org.orman.mapper.annotation.Entity;
import org.orman.mapper.annotation.ManyToOne;
import org.orman.mapper.annotation.PrimaryKey;
@Entity 
public class userInfo extends Model {
    @PrimaryKey(autoIncrement=true)
    public long id;
    //public long flagid;
    public String username;
    public String pwd;
    /**
     * 1爲正常註冊,2爲一鍵註冊
     */
    public String type;
    public String comefrom;
    public String comefromtype;//1爲android,2爲ios
    public String channlecode;//渠道標識
    public String nettype;//網絡類型
    public String ipaddress;//ip地址
    /**
     * 0,不可修改,1可修改
     */
    public String canUpdate;

    @ManyToOne
    public userFlag flagid;//這裏的 flagid,在數據庫裏會被映射成一個Integer字段,存放 userFlag實體的id
    
}

import java.net.URL;
import org.orman.dbms.Database;
import org.orman.dbms.sqlite.SQLite;
import org.orman.mapper.MappingSession;
import org.orman.util.logging.Log;
import org.orman.util.logging.LoggingLevel;
import org.orman.util.logging.StandardLogger;
import org.apache.commons.lang.StringUtils;
public class dbService {

public static dbService dbs =null;
public static dbService getinstaDbService()
{
if(dbs==null)
{
dbs = new dbService();
initDB();
return dbs;
}else {
return dbs;
}
}
public static void main(String args[])
{
initDB();
}

/**
* @param args
*/
public static void initDB() {
String dbpath = getResourcePath();
dbpath = dbpath.substring(0,dbpath.lastIndexOf("/"));
System.out.println(dbpath);
Database db = new SQLite(dbpath+"/baina.db");
        Log.setLogger(new StandardLogger());
        Log.setLevel(LoggingLevel.TRACE);
        MappingSession.registerPackage("baina.db.vo");
MappingSession.registerDatabase(db);
MappingSession.start();
}


private static String getResourcePath() {
String className = dbService.class.getName();
String classNamePath = className.replace(".", "/") + ".class";
//System.out.println(classNamePath);
URL is = dbService.class.getClassLoader().getResource(classNamePath);
String path = is.getFile();
path = StringUtils.replace(path, "%20", " ");
return StringUtils.removeStart(path, "/");
}
}

import java.util.List;
import org.orman.mapper.C;
import org.orman.mapper.Model;
import org.orman.mapper.ModelQuery;

public class TestDb {


/**
* @param args
*/
public static void main(String[] args) {
dbService dbs = dbService.getinstaDbService();
//數據插入
//userFlag uFlag = new userFlag();
//uFlag.flag="xxx-huawei-c8825";
//uFlag.insert();
//帶級聯關係的查詢
List<userFlag> filtered = Model.fetchQuery(ModelQuery.select().
from(userFlag.class).where(C.eq(userFlag.class, "flag", "xxx-huawei-c8825")).getQuery(), userFlag.class);
userFlag uFlag =null;
uFlag=filtered.get(0);
//數據插入
// userInfo uInfo = new userInfo();
// uInfo.username="bbb";
// uInfo.pwd="bbb";
// uInfo.comefrom="bainawebtest";
// uInfo.comefromtype="1";
// uInfo.flagid=uFlag;
// uInfo.type="2";
// uInfo.canUpdate="1";
// uInfo.insert();



}


}
相關文章
相關標籤/搜索