(十):如何高效地编写后端MVC代码

205 篇文章 0 订阅

​​​​​

  1. Dao要做什么工作

DAO(Data Access Object) 数据访问对象是一个面向对象的数据库接口。一般我们将访问数据库相关的功能放在Dao中。用Java语言,可以使用JDBC访问数据库,使用java.sql和javax.sql里的包,直接编码,但这样效率不高。所以现在软件工程师们一般都使用ORM工具,如Hibernate,Mybatis,还有在互联网高速发展背景下出现的Bee.

  Hibernate,Mybatis一般都需要写Javabean,还有xml匹配文件或将注解写在Javabean。而Bee只需要纯的Javabean,甚至Javabean里都可以没有get,set方法。

 完成了与表相关的Javabean等,就是实现SQL相关的功能了。Mybatis 需要写比较多SQL语句,即使只是查单个表的信息;而Hibernate则用面向对象的方式写SQL时不够灵活。Bee是一个开箱即用的ORM框架,这些工作已由Bee的Suid,SuidRich等接口负责,程序员何需去费心这些。

  Suid suid = BeeFactory.getHoneyFactory().getSuid();
  Orders orders1 = new Orders();
  orders1.setName("Bee");
  List<Orders> list1 = suid.select(orders1); //select

看以上的程序段,就4条语句,就可以将订单表的相关信息,通过List返回了。有两个语句,还是实体相关的。下面,就让我们一起来看下,如何自动生成实体Javabean。

2. 自动生成Javabean

public class GenBeanExam {
public static void main(String[] args) {
try{
String dbName=HoneyConfig.getHoneyConfig().getDbName();
// driverName,url,username,password config in bee.properties.

  GenConfig config = new GenConfig();
  config.setDbName(dbName);
  config.setGenToString(true);//生成toString方法
  config.setGenSerializable(true);


// 更改成本地的具体路径 change to your real path
config.setBaseDir("D:\xxx\yyy\bee-exam\src\main\java\");
config.setPackagePath("org.teasoft.exam.bee.osql.entity");

  GenBean genBean = new GenBean(config);
  genBean.genSomeBeanFile("Orders");


// genBean.genSomeBeanFile("Orders,user"); //同时生成多个表

  } catch (BeeException e) {
     e.printStackTrace();
  }


}
}


配置了数据库的driverName,url,username,password等信息,就可以生成DB表对应的Javabean了。GenBean还提供了可以一下就生成所有的表Javabean,是不是开发效率很高呀。

3.自动生成基于Spring的Rest代码(后端MVC代码)

以下是一个查询历史订单信息的服务,是前后端分离的,后端利用Spring的RestController注解,返回Json格式。更为重要的是,这个类是自动生成的,而且操作别的表的数据,也是可以自动生成的,是不是开发效率很高呀。

/*
 * Copyright 2016-2020 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.automvc.enet.order.rest;

import java.util.List;

import org.teasoft.bee.osql.BeeSQLException;
import org.teasoft.bee.osql.FunctionType;
import org.teasoft.bee.osql.service.ObjSQLRichService;
import org.teasoft.bee.osql.service.ObjSQLService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.automvc.enet.order.entity.Orderhistory;
import com.automvc.common.jquery.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.0
 * Create on 2019-04-16 11:48:24
 */
@RestController
@RequestMapping("/orderhistory")
public class OrderhistoryRest {
	@Autowired
	ObjSQLService objSQLService;
	
	@Autowired
	ObjSQLRichService objSQLRichService;
	
	@RequestMapping("/list")
	public Result list(Orderhistory orderhistory,
	     @RequestParam(value = "page", defaultValue = "1", required = false) int page, 
		 @RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {	
	  Result  result =new Result();
	  try{
		  int total = objSQLRichService.count(orderhistory);
		  List<Orderhistory> list=objSQLRichService.select(orderhistory, (page-1)*rows, rows);
		  result.setRows(list);
		  result.setTotal(total);
	  } catch (BeeSQLException e) {
		  result.setErrorMsg(e.getMessage());
	  }
		
	   return result;
	}
	
	@RequestMapping("/add")
	public Result insert(Orderhistory orderhistory){
		
	  Result  result =new Result();
	  try{
		  int num=objSQLService.insert(orderhistory);
		  result.setTotal(num);
		  if(num<=0) result.setErrorMsg("insert failed!");
	  } catch (BeeSQLException e) {
		  result.setErrorMsg(e.getMessage());
	  }
		return result;
	}
	
	@RequestMapping("/edit")
	public Result update(Orderhistory orderhistory){
		Result  result =new Result();
		try{
			int num=objSQLService.update(orderhistory);
			result.setTotal(num);
			if(num<=0) result.setErrorMsg("update failed!");
	    } catch (BeeSQLException e) {
		    result.setErrorMsg(e.getMessage());
	    }
		return result;
	}
	
	@RequestMapping("/del")
	public Result delete(String ids) {
		Result result = new Result();
		try {
			int num=objSQLRichService.deleteById(Orderhistory.class, ids);
			result.setTotal(num);
			if (num <= 0) result.setErrorMsg("delete failed!");
		} catch (BeeSQLException e) {
			result.setErrorMsg(e.getMessage());
		}
		return result;
	}
}

现在我们就来看下,这个类的代码,是如何生成的。

import org.teasoft.honey.osql.autogen.GenFiles;
/**

  • @author Kingstar
  • @since 1.7.2
    */
    public class GenFilesExam {

public static void main(String[] args) {

Map<String, String> map = new HashMap<>();
map.put("entityName1", "Orderhistory");
map.put("entityName", "orderhistory");
map.put("packageName", "com.automvc.enet.order.rest");
String basePath = "D:\\xxx\\yyy\\bee-exam\\src\\main\\java\\org\\teasoft\\exam\\bee\\osql\\autogen\\";

String templatePath = basePath + "OrderhistoryRest.java.template";
String targetFilePath = basePath + "OrderhistoryRest.java";
GenFiles.genFile(templatePath, map, targetFilePath);

System.out.println("finished!");

}

}

是不是很简单,几行代码,就可以源源不断的生成类似的代码了。后端MVC代码就应该这样用Bee的工具开发出来。

更多例子,请访问:bee-exam: Bee-exam is the test and example of ORM framework Bee.

或: GitHub - automvc/bee-exam: Bee-exam is the example of ORM framework Bee.

Bee简单易用:单表操作、多表关联操作,可以不用写sql,极少语句就可以完成SQL操作;10分钟即可学会使用。

Bee功能强大:复杂查询也支持向对象方式,分页查询性能更高,一级缓存即可支持个性化优化。高级需求,还可以方便自定义SQL语句。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值