- What does Dao do
DAO(Data Access Object) data access object is an object-oriented database interface. Generally, we put the functions related to accessing the database in Dao. With the Java language, you can use JDBC to access the database, use the packages in java.sql and javax.sql, and code directly, but this is not efficient. So now software engineers generally use ORM tools, such as Hibernate,Mybatis, and Bee, which appears in the context of the rapid development of the Internet
Hibernate,Mybatis Generally need to write Javabean,also xml Match files or write comments in Javabean. and Bee Just pure Javabean,even to the extent that Javabean You can have none in the get,set method. Completed table related Javabean Wait, that's the realization SQL Related functions. Mybatis Need to write more SQL Statement, even if it only looks up the information of a single table; and Hibernate It is written in an object-oriented manner SQL Not flexible enough. Bee It's an out of the box ORM Framework, these work have been carried out by Bee of Suid,SuidRich When the interface is responsible, programmers don't need to worry about these. Suid suid = BeeFactory.getHoneyFactory().getSuid(); Orders orders1 = new Orders(); orders1.setName("Bee"); List<Orders> list1 = suid.select(orders1); //select
Look at the above program section. With four statements, you can return the relevant information of the order table through List. There are two statements that are entity related. Next, let's take a look at how to automatically generate entity Java beans.
2. Automatically generate Java beans
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);//Generate toString method config.setGenSerializable(true);
//Change to your real pathchange 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"); // Generate multiple tables at the same time
} catch (BeeException e) { e.printStackTrace(); }
}
}
After configuring the driverName,url,username,password and other information of the database, you can generate the Java Bean corresponding to the DB table. GenBean also provides Java beans that can generate all tables at once. Is it very efficient.
3. Automatically generate Spring based Rest code (back-end MVC code)
The following is a service for querying historical order information, which is separated from the front end and back end. The back end uses Spring's RestController annotation to return the Json format. More importantly, this class is automatically generated, and the data of other tables can be generated automatically. Is it very efficient.
/* * 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; } }
Now let's look at how the code of this class is generated.
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!");
}
}
Is it very simple? A few lines of code can continuously generate similar code. The back-end MVC code should be developed with Bee tools.
For more examples, visit: bee-exam: Bee-exam is the test and example of ORM framework Bee.
Or: GitHub - automvc/bee-exam: Bee-exam is the example of ORM framework Bee.
Bee is simple and easy to use: single table operation and multi table association operation. You can complete SQL operation without writing SQL and with few statements; you can learn to use it in 10 minutes.
Bee has powerful functions: complex queries also support the object-oriented method, with higher paging query performance. The first level cache can support personalized optimization. For advanced requirements, it is also convenient to customize SQL statements.