Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Micro-mvc framework supports hot deployment of business codes at all layers of mvc.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

The micro-mvc framework, which can be integrated with springmvc and springcloud, enables all controller, servicebean, dao, and sql business logic code to support hot deployment to facilitate developer tuning and production deployment.

Source code and demo address: https://github.com/jeffreyning/micro-mvc

Integration process with springmvc

Write Controller interface

After integration, the controller of Springmvc only writes interfaces, and the parameter names must be annotated with RequestParam.

Use the InjectGroovy annotation to declare the corresponding groovy implementation name in the interface.

Other controller is no different from traditional springmvc.

Package foo.web;import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.nh.micro.service.InjectGroovy;@Controller@RequestMapping ("test") @ InjectGroovy (name= "TestController") public interface TestController {@ RequestMapping ("echo") @ ResponseBodypublic Map echo (@ RequestParam (value= "str") String str,HttpServletRequest httpRequest);}

Write the implementation groovy of Controller layer

Package groovy;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import com.nh.micro.rule.engine.core.plugin.MicroAop;import com.nh.micro.rule.engine.core.plugin.MicroDefaultLogProxy;import com.nh.micro.service.InjectGroovy;import com.nh.micro.template.MicroDbProxy;import com.nh.micro.template.MicroTMProxy;import foo.service.TestService @ MicroAop (name= [MicroDefaultLogProxy.class,MicroTMProxy.class,MicroDbProxy.class], property= [",", "]) class TestController {@ Resource public TestService testService; public Map echo (String str,HttpServletRequest httpRequest) {System.out.println (" this is controller proxy "); testService.test (" 111"); Map retMap=new HashMap (); retMap.put ("status", "0"); return retMap;}}

Configure controller layer packet scanning

Use GroovyBeanScannerConfigurer instead of context:component-scan to scan the controller.

Service layer

Write ServiceBean interface

The package is scanned so that the controller layer can be loaded into the ServiceBean proxy object as resource. Note that the @ InjectGroovy annotation is used in the interface to declare the name of the corresponding groovy file that implements the business logic

Write ServiceBean interface

Package foo.service;import com.nh.micro.service.InjectGroovy;@InjectGroovy (name= "TestService") public interface TestService {public void test (String id);}

Groovy implementation of Service layer

Package groovy.service;import javax.annotation.Resource;import foo.dao.TestDao;import foo.dto.MicroTestDto;import foo.repository.*;import groovy.json.*;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.transaction.annotation.Transactional;import com.nh.micro.dao.mapper.DefaultPageInfo;import com.nh.micro.dao.mapper.InjectDao;import com.nh.micro.rule.engine.core.plugin.MicroAop Import com.nh.micro.rule.engine.core.plugin.MicroDefaultLogProxy;import com.nh.micro.template.MicroDbProxy;import com.nh.micro.template.MicroServiceTemplateSupport;import com.nh.micro.template.MicroTMProxy;@MicroAop (name= [MicroDefaultLogProxy.class,MicroTMProxy.class,MicroDbProxy.class], property= [",", "]) class TestService {@ Resource public TestDao testDao; public void test (String id) {Map paramMap=new HashMap (); paramMap.put (" id ", id) MicroTestDto microTestDto=testDao.queryInfoById (paramMap); List list=testDao.getInfoListAllMapper (microTestDto, "); DefaultPageInfo pageInfo=new DefaultPageInfo (); pageInfo.setPageNo (1); List retList=testDao.queryInfosByPage (paramMap, pageInfo); Long total=pageInfo.getTotal (); System.out.println (" total= "+ total);}}

Service layer packet scan configuration

Dao layer

To use the micro-dao module, you need to write dto classes, dao interfaces and sql files. By scanning the sql file and the dao interface, the service layer can load the dao proxy object using Resource annotations.

Write dto object classes

Use @ MicroTableName to map table names in the data and @ MicroMappingAnno to map column names in the table.

Package foo.dto;import java.util.Date;import com.nh.micro.orm.MicroMappingAnno;import com.nh.micro.orm.MicroTableName;@MicroTableName (name= "micro_test") public class MicroTestDto {@ MicroMappingAnno (name= "id") private String id; @ MicroMappingAnno (name= "meta_key") private String metaKey; @ MicroMappingAnno (name= "meta_name") private String metaName; @ MicroMappingAnno (name= "meta_type") private String metaType MicroMappingAnno (name= "create_time") private Date createTime; @ MicroMappingAnno (name= "meta_num") private Integer metaNum; public Integer getMetaNum () {return metaNum;} public void setMetaNum (Integer metaNum) {this.metaNum = metaNum;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this.createTime = createTime } public String getId () {return id;} public void setId (String id) {this.id = id;} public String getMetaKey () {return metaKey;} public void setMetaKey (String metaKey) {this.metaKey = metaKey;} public String getMetaName () {return metaName;} public void setMetaName (String metaName) {this.metaName = metaName } public String getMetaType () {return metaType;} public void setMetaType (String metaType) {this.metaType = metaType;}}

Write Dao interface

Package foo.dao;import java.util.List;import java.util.Map;import com.nh.micro.dao.mapper.ListInnerClass;import com.nh.micro.dao.mapper.MicroCommonMapper;import com.nh.micro.dao.mapper.MicroPageInfo;import com.nh.micro.orm.MicroDbName;import foo.dto.MicroTestDto;@MicroDbNamepublic interface TestDao extends MicroCommonMapper {public int updateInfo (Map paramMap); public int insertInfo (Map paramMap); @ ListInnerClass (name=MicroTestDto.class) public List queryInfosByPage (Map paramMap,MicroPageInfo pageInfo) Public MicroTestDto queryInfoById (Map paramMap);}

Dao layer packet scan configuration

Write sql templates

A syntax similar to jsp is used to write sql, which supports two replacement characters # and $. There is no need to distinguish between select and update, but use a uniform label. The id and dao interface method names are the same, and the sql file name is the same as the dao interface name.

Select * from micro_test where 1 and id = # {paramArray [0] .id} select * from micro_test insert into micro_test (id,meta_key) values (?,?) update micro_test set, meta_key=# {paramArray [0] .get ("meta_key")} Meta_name=# {paramArray [0] .get ("meta_name")} where id=# {paramArray [0] .get ("id")}

Sql file scan configuration

MicroDao description

Support for both mysql and oracle

Advantages of MicroDao over mybatis:

1the SQL script supports hot deployment to take effect in real time after modification.

2the mapping relationship between the bean and the database field is set to bean through annotations, but does not have to be reflected in the sql script.

3The jsp script supports writing similar to jsp, and there is no need to distinguish between select and update using different tags, so it is more flexible.

4, no need to use plug-ins, built-in support for physical paging.

5. No plug-ins are required, and the standard addition, deletion, modification and query function for bean is supported in the built-in.

6, do not need to use plug-ins, built-in support for read-write separation, sub-library sub-table.

7. Dynamic fields are supported for mysql5.7.

Support mapper, template and non-orm modes to support business systems

1Powermapper means that dao instances are automatically generated at runtime through scanning interfaces.

2. Dao template refers to the generation of a template subclass by inheriting the dao standard parent class

3. Non-orm means that you can perform more flexible database operations on non-orm by using microDao instances directly.

About transactions

Because the serviceBean interface object is published as springbean through package scanning, the original spring transaction mechanism can still be used.

SpringCloud integration

SpringCloud integration controller,service, dao layer and springmvc integration are consistent.

View the demo project in detail

Micro-springcloud-mvc

Aop Mechanism of Nhmicro

Aop proxy mechanism is provided when loading groovy. Transaction aop and data source switching aop are provided by default.

Transaction aop, which can recognize Transactional annotations when loading, and realize transaction control.

Do not write specific functional agents on your own.

To enable the proxy, you need to set the MicroAop annotation in the class of groovy to specify the proxy class.

@ MicroAop (name= [MicroDefaultLogProxy.class,MicroTMProxy.class,MicroDbProxy.class], property= [",", "])

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report