jfinal操作数据库使用findById方法时,表的主键字段必须是id么
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jfinal操作数据库使用findById方法时,表的主键字段必须是id么相关的知识,希望对你有一定的参考价值。
参考技术A 可以根据自己使用的主键查找例如:你的用户主键设置设置是uid,那么你就可以使用Record user = Db.findById("user", "uid", 123);这是record+db操作的,希望有帮助!建议你使用record+db操作,可以进行复杂的一些数据操作! 参考技术B Record user = new Record().set("name", "James").set("age", 25);Db.save("user", user);
user.get("id");本回答被提问者采纳
初尝 JFinal 项目
temp1:
与JAVA项目类似,有属性方法、操作方法、Sql语句操作、jdbc、配置文件
对比:|| JAVA: Bean / Srv(Server) / SqlMap / jdbc.properties / SqlMapConfig、struts ||
|| JFinal: Model / Controller / 省略 / jdbc.properties / JFinalConfig ||
temp2:
属性:JavaBean手动添加属性并获取get/set方法。 例:private int id; ——> Source ——> Generate Getters and Setters...
JFinalModel自动添加属性不需要获取get/set方法。 例: private static final long serialVersionUID = 1L;
public static final JFinalModel dao = new JFinalModel();
操作:JavaSrv直接书写增删更查方法。
例:private String returnStr;
/**
* 添加方法
* @param bean
* @return 御手洗红豆
* @throws Exception
*/
public String addJava ( JavaBean bean ) throws Exception {
try {
HashMap<String, String> param = new HashMap<String, String>();
//Java类型
if(!StringTool.IfNull(bean.getJavatype())){
param.put("Javatype", bean.getJavatype());
}
this.addRecord("addJava", param); //引号里内容SqlMap中会用到
returnStr = Const.SYSTEM_STATUS_SUCCESS;
} catch (Exception e) {
// TODO: 返回系统状态错误字符串
returnStr = Const.SYSTEM_STATUS_ERROR;
e.printStackTrace();
throw e;
}
return returnStr;
}
/**
* 删除方法
* @param bean
* @return 御手洗红豆
* @throws Exception
*/
public String DelJava ( JavaBean bean ) throws Exception {
try {
HashMap<String, String> param = new HashMap<String, String>();
if(!StringTool.IfNull(bean.getId())){
param.put("id", bean.getId());
}
this.deleteRecord("DelJava", param);
returnStr = Const.SYSTEM_STATUS_SUCCESS;
} catch (Exception e) {
// TODO: handle exception
returnStr = Const.SYSTEM_STATUS_ERROR;
e.printStackTrace();
throw e;
}
return returnStr;
}
//更新方法与前两个差异在 this.updateRecord("UpdateJava", param);
//查询
public List<JavaBean> SelectJava ( JavaBean bean ) throws Exception {
List<JavaBean> javaList = null;
try {
HashMap<String, String> param = new HashMap<String, String>();
if(!StringTool.IfNull(bean.getId())){
param.put("id", bean.getId());
}
if(!StringTool.IfNull(bean.gettype())){
param.put("type", bean.gettype());
}
roleList = this.getRecords("SelectJava", param);
return javaList;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw e;
}
}
//查询总数(为分页查询做准备)
public int SelectJavaCount(JavaBean bean) throws Exception {
int count = -1;
try {
HashMap<String, String> param = new HashMap<String, String>();
if(!StringTool.IfNull(bean.getId())){
param.put("Id", bean.getId());
}
if(!StringTool.IfNull(bean.getType())){
param.put("Type", bean.getType());
}
count = Integer.parseInt(this.getSingleValue("SelectJavaCount", param).toString());
return count;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
/**
* 分页 查询
* @param bean 查询条件
* @param pageNum 当前页码
* @param pageCount 每页显示数量
* @return
* @throws Exception
*/
public List<JavaBean> SelectJavaPage (JavaBean bean, int pageNum, int pageCount) throws Exception {
try {
HashMap<String, String> param = new HashMap<String, String>();
if(!StringTool.IfNull(bean.getId())){
param.put("Id", bean.getId());
}
if(!StringTool.IfNull(bean.getType())){
param.put("Type", bean.getType());
}
if......
param.put("pageBegin", ((pageNum-1)*pageCount) + "");
param.put("pageCount", pageCount + "");
List<JavaBean> javaList = this.getRecords("SelectJavaPage", param);
return javaList;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
》》 JFinalController书写修改方法
/**
* 获取联盟列表
* @author
*/
public void getJavaList(){
// 取出用户信息,添加用户Log用
UserModel userModel = getSessionAttr(SystemStateConstant.SessionLoginUser);
int totalPage = 1;// 总页数
long totalRow = 0;// 总记录数
int pageNumber= getParaToInt("pageNumber", 1);// 当前页码
String urlParas= getPara("urlParas", "");
String code= getPara("code", "");// 查询条件:编号
String name= getPara("name", "");// 查询条件:名称
if(code.equals("") && name.equals("") && !urlParas.equals("")){
try {
urlParas = URLDecoder.decode(urlParas, "UTF-8"); //数据转码UTF-8
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
urlParas = "";
}
}else{
urlParas = "";
if(!code.equals("")){
urlParas += " and code like ‘%" + code + "%‘";
}
if(!name.equals("")){
urlParas += " and name like ‘%" + name + "%‘";
}
if......
}
// 取得总记录数
String sql = "select count(*) totalRow from Java where isdel=0" + urlParas;
totalRow = JavaModel.dao.findFirst(sql).getLong("totalRow");
// 计算总页数,不能整除的进1
totalPage = BigDecimal.valueOf(totalRow).divide(SystemStateConstant.PAGENUM, BigDecimal.ROUND_UP).intValue();
sql = "select * from (select * from Java where isdel=0" + urlParas +" order by code) t limit " + ((pageNumber-1) * SystemStateConstant.PAGENUM.intValue()) + "," + SystemStateConstant.PAGENUM.intValue();
List<JavaModel> dateList = JavaModel.dao.find(sql);
setAttr("pageNumber", pageNumber);
setAttr("totalPage", totalPage);
setAttr("totalRow", totalRow);
setAttr("dateList", dateList);
try {
setAttr("urlParas", URLEncoder.encode(urlParas, "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
setAttr("urlParas", "");
}
//进入编辑页
public void toedit(){
String id= getPara("id", "-1");
String pageNumber= getPara("pageNumber");
String urlParas= getPara("urlParas");
JavaModel JavaInfo = JavaModel.dao.findById(id);
setAttr("msg", "");// 错误信息,进入时为空即可
setAttr("pageNumber", pageNumber);
setAttr("urlParas", urlParas);
setAttr("JavaInfo", JavaInfo);
render("/ace/JavaPage/formPage.html");
}
//查看联盟详情
public void view(){
String id= getPara("id", "-1");
JavaModel JavaInfo = JavaModel.dao.findById(id);
setAttr("JavaInfo", JavaInfo);
}
// 编辑处理
public void edit(){
// 取出用户信息,添加用户Log用
UserModel userModel = getSessionAttr(SystemStateConstant.SessionLoginUser);
String id= getPara("id", "");
String pageNumber= getPara("pageNumber");
String urlParas= getPara("urlParas");
JavaModel JavaInfo = getModel(JavaModel.class, "", true);
String msg = "";
if(id.equals("")){
String sql = "select count(*) totalRow from Java where code=‘" + JavaInfo.getStr("code") + "‘";
long sumnum = JavaModel.dao.findFirst(sql).getLong("totalRow");
if(sumnum == 0){
JavaInfo.set("createDate", DateTimeUtil.getNowTime());
if(JavaInfo.get("homepage") != null && !"".equals(JavaInfo.get("homepage"))){
JavaInfo.set("ishomepage", "1");
}else{
JavaInfo.set("ishomepage", "2");
}
JavaInfo.save();
logHandler.seaveLog(OptTypeEnum.USEROPT, OptModelConstant.MODEL_Java, OptTypeConstant.OPTCODE_ADD, "添加联盟:" + JavaInfo.getStr("code") + " | " + JavaInfo.getStr("name") + ",成功,管理员:" + userModel.getStr("name") + "。", 1, 2, userModel.getInt("id"));
}else{
msg = "添加失败,该编码的联盟已经添加过,请查证!";
logHandler.seaveLog(OptTypeEnum.USEROPT, OptModelConstant.MODEL_Java, OptTypeConstant.OPTCODE_ADD, "添加联盟:" + JavaInfo.getStr("code") + " | " + JavaInfo.getStr("name") + ",失败,管理员:" + userModel.getStr("name") + "。", 2, 2, userModel.getInt("id"));
}
}else{
JavaInfo.set("id", id);
if(JavaInfo.get("homepage") != null && !"".equals(JavaInfo.get("homepage"))){
JavaInfo.set("ishomepage", "1");
}else{
JavaInfo.set("ishomepage", "2");
}
JavaInfo.update();
logHandler.seaveLog(OptTypeEnum.USEROPT, OptModelConstant.MODEL_Java, OptTypeConstant.OPTCODE_UPD, "编辑联盟:" + JavaInfo.getStr("code") + " | " + JavaInfo.getStr("name") + ",管理员:" + userModel.getStr("name") + "。", 1, 2, userModel.getInt("id"));
}
if(msg.equals("")){
redirect("/Java/getJavaList?pageNumber=" + pageNumber + "&urlParas=" + urlParas);
}else{
setAttr("msg", msg);
setAttr("pageNumber", pageNumber);
setAttr("urlParas", urlParas);
setAttr("JavaInfo", JavaInfo);
render("/ace/JavaPage/formPage.html");
}
}
/**
* 删除处理
* @author
*/
public void del(){
// 取出用户信息,添加用户Log用
UserModel userModel = getSessionAttr(SystemStateConstant.SessionLoginUser);
String id= getPara("id", "");
String pageNumber= getPara("pageNumber");
String urlParas= getPara("urlParas");
JavaModel model = new JavaModel();
model.set("id", id);
model.set("isDel", 1);
model.update();
logHandler.seaveLog(OptTypeEnum.USEROPT, OptModelConstant.MODEL_Java, OptTypeConstant.OPTCODE_DEL, "删除联盟(id):" + model.getStr("id") + ",管理员:" + userModel.getStr("name") + "。", 1, 2, userModel.getInt("id"));
redirect("/Java/getJavaList?pageNumber=" + pageNumber + "&urlParas=" + urlParas);
}
jdbc无区别
配置文件区别: JAVA配置:Struts.xml
例:<!-- Login action -->
<action name="Login_*" class="LoginAction" method="{1}">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="checkUser" />
<result name="succeed" type="redirectAction">Login_toIndex.action</result>
<result name="toindex">ace/main_page.jsp</result>
<result name="fail">Login.jsp</result>
<result name="login">Login.jsp</result>
</action>
JFinal配置:JFinalConfig.java
例:/**
* jFinal路由配置
*/
@Override
public void configRoute(Routes me) {
me.add("/", IndexController.class); // 访问系统跳入登录页
me.add("/user", UserController.class); // User管理
me.add("/roles", RolesController.class);// 角色管理
me.add("/auths", AuthsController.class);// 权限管理
}
/***
* 数据库连接配置
*/
@Override
public void configPlugin(Plugins me) {
C3p0Plugin c3p0 = new C3p0Plugin(getProperty("jdbc.url"), getProperty("jdbc.username"),
getProperty("jdbc.password"));
c3p0.setDriverClass(getProperty("jdbc.driverClassName"));
me.add(c3p0);
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0);
me.add(arp);
arp.setDialect(new AnsiSqlDialect());
arp.setContainerFactory(new CaseInsensitiveContainerFactory());
arp.addMapping("roles", RolesModel.class);// 角色表
arp.addMapping("auths", AuthsModel.class);// 权限表
arp.addMapping("roleAuths", RoleAuthsModel.class);// 角色权限表
}
/***
* 启动运行时运行
*/
public void afterJFinalStart() {
}
/**
* 建议使用 JFinal 手册推荐的方式启动项目
* 运行此 main 方法可以启动项目,此main方法可以放置在任意的Class类定义中,不一定要放于此
*/
public static void main(String[] args) {
JFinal.start("WebRoot", 8080, "/", 5);
}
以上是关于jfinal操作数据库使用findById方法时,表的主键字段必须是id么的主要内容,如果未能解决你的问题,请参考以下文章