1. 程式人生 > >Jboot學習筆記

Jboot學習筆記

jboot專案地址

jboot文件

jfinal文件下載

專案依賴:

		<dependency>
			<groupId>io.jboot</groupId>
			<artifactId>jboot</artifactId>
			<version>1.4.7</version>
		</dependency>
		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId
>
guava</artifactId> <version>20.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.20</version> </dependency>

配置參考:

#type default undertow (support:undertow)
jboot.server.type=undertow
jboot.server.host=0.0.0.0
jboot.server.port=8091
jboot.server.contextPath=/

jboot.datasource.type = mysql
jboot.datasource.url = jdbc:mysql://localhost:3306/mydb?characterEncoding=utf8&useSSL=true
jboot.datasource.user = root
jboot.datasource.password = 123456

jboot.swagger.path = /swaggerui
jboot.swagger.title = Jboot API \u6D4B\u8BD5
jboot.swagger.description = \u914D\u7F6E\u8BF4\u660E
jboot.swagger.version = 1.0
jboot.swagger.termsOfService = http://jboot.io
jboot.swagger.contact = \u8054\u7CFB\u65B9\u5F0F...
jboot.swagger.host = 127.0.0.1:80

jboot Controller 程式碼參考:

@RequestMapping("/test")
@Api(description = "Jboot測試Controller", basePath = "/test", tags = "test")
public class TestController extends JbootController {

	public void testDB() {//sum(cash_amount)
		Record result = Db.findFirst("select id,sum_balance,cash_amount "
				+ "from t_buyer_user as a left join "
+ "(select cash_user_id,sum(cash_amount) as cash_amount " + "from t_balance_cash where cash_pay_status = 0 group by cash_user_id ) as b " + "on a.id = b.cash_user_id where a.id = ? ", 794L); renderJson(result); } public void subject() { Subject subject = SecurityUtils.getSubject(); renderJson(subject.getPrincipal()); } public void login() { UsernamePasswordToken token = new UsernamePasswordToken(getPara("username"), getPara("password")); try { Subject subject = SecurityUtils.getSubject(); subject.login(token); if (subject.isAuthenticated()) { renderJson("login"); return; } } catch (UnknownAccountException une) { renderJson("使用者名稱不存在"); } catch (LockedAccountException lae) { renderJson("使用者被鎖定"); } catch (IncorrectCredentialsException ine) { renderJson("使用者名稱或密碼不正確"); } catch (ExcessiveAttemptsException exe) { renderJson("賬戶密碼錯誤次數過多,賬戶已被限制登入1小時"); } catch (Exception e) { e.printStackTrace(); renderJson("登入失敗!未知錯誤,請稍後重試"); } finally { } } public void session() { setSessionAttr("s1", "okkpp"); getSessionAttr("s1"); HttpSession session = getSession(); session.getAttribute("s1"); renderJson(); } /** * 多次呼叫導致結果被覆蓋 * 記得使用return提前結束 */ public void rendertest() { boolean result = true; if(result) { renderJson("result 1"); return; } renderJson("result 2"); } /** * getBean getModel * 預設類名.屬性 * 或者使用 "" 代替 不使用類名 但是不在屬性範圍內的引數 會引起異常 * getModel專門用來獲取Model類 */ public void bean() { //TBuyerUser user = getBean(TBuyerUser.class, ""); //System.out.println(user.getNickname()); renderJson(); } public void model() { //TBuyerUser user = getModel(TBuyerUser.class, ""); //System.out.println(user.getNickname()); renderJson(); } @ApiOperation(value = "引數測試", httpMethod = "GET", notes = "註釋") @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "使用者id", paramType = "query", dataType = "String", required = false), @ApiImplicitParam(name = "okkpp", value = "欄位名", paramType = "query", dataType = "String", required = false), }) public void paramap(@Para("id") int id) { // 通過形參獲取的引數 依然會出現在getParaMap()中 System.out.println(getPara("okkpp")); Map<String, String[]> paraMap = getParaMap(); for (String key : paraMap.keySet()) { System.out.println(key + ":" + paraMap.get(key).toString()); } renderJson(); } /** * 入參為基本型別 @Para 必須標記value * @param id * @param num */ public void para2(int id, @Para("num")int num, @Para("str")String str) { System.out.println(id+"-"+num+str); renderJson(id+"-"+num+str); } public void para(@Para("model") Model model, @Para("") Model other) { // 入參形式應為 model.id @Para("")說明不需要帶字首直接id System.out.println("預設方法"); System.out.println(model.getMap().toString()); renderJson(); } public void test() { Map<String, Object> params = new HashMap<String, Object>(16); params.put("model", new Model(1, "mark")); params.put("model2", new Model(2, "okkpp")); params.put("model.id", 3); params.put("model.mark", "duck"); params.putAll(getParaMap()); Jboot.httpPost("http://127.0.0.1:8080/test/para", params); renderJson(); } // @Inject // private TBuyerUser dao = new TBuyerUser().dao(); public void db2() { // TBuyerUser user = dao.findFirst("select * from t_buyer_user where id = ?", getPara("id")); // if(null!=user) { // renderJson(user); // }else { // renderJson("not find"); // } } public void db() { //find方法把查詢的結果集都收集到map裡 存到Record的map List<Record> records = Db.find("select now()"); renderJson(records); } public static void main(String[] args) { Jboot.run(args); } }

jboot 原始碼生成器示例:

public class CodeGenerator {

    public static void main(String args[]){
        //依賴model的包名
        String modelPackage = "okkpp.model";
        //生成service 的包名
        String basePackage = "okkpp.service";

        //生成model dao層
        //JbootModelGenerator.run(modelPackage);
        String baseModelPackage = modelPackage + ".base";

        String modelDir = PathKit.getWebRootPath() + "/src/main/java/" + modelPackage.replace(".", "/");
        String baseModelDir = PathKit.getWebRootPath() + "/src/main/java/" + baseModelPackage.replace(".", "/");

        System.out.println("start generate...");
        System.out.println("generate dir:" + modelDir);

        //去掉表字首
        MetaBuilder metaBuilder = CodeGenHelpler.createMetaBuilder();
        metaBuilder.setRemovedTableNamePrefixes("t_");
        
        List<TableMeta> tableMetaList = metaBuilder.build();
        CodeGenHelpler.excludeTables(tableMetaList, null);

        new JbootBaseModelGenerator(baseModelPackage, baseModelDir).generate(tableMetaList);
        new JbootModelnfoGenerator(modelPackage, baseModelPackage, modelDir).generate(tableMetaList);
        
        //生成service
        //JbootServiceGenerator.run(basePackage, modelPackage);
        System.out.println("start generate...");

        new JbootServiceInterfaceGenerator(basePackage, modelPackage).generate(tableMetaList);
        new JbootServiceImplGenerator(basePackage , modelPackage).generate(tableMetaList);

    }
}