NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
tips:java代码注释的模板 ~~~ /\*\* \* ${tags} \*//\*\* \* ${tags} \* ${return\_type} \* @author 李淼 \* @time ${date}${time} \*//\* (non-Javadoc) \* ${see\_to\_overridden} \*//\*\* \* \*//\*\* \* @param ${param} the ${bare\_field\_name} to set \*//\*\* \* @project\_name ${project\_name} \* @package\_name ${package\_name} \* @class\_name ${type\_name} \* @author 李淼 \* @company yhouse \* @version 1.0 \* @time ${date}  ${time} \*//\*\* \* @return the ${bare\_field\_name} \*//\*\* \* ${tags} \* ${see\_to\_target} \*//\*\* \* \*/ ~~~ 使用说明: 1.新建xml文件,将上面的内容粘贴到里面 2.在myeclipse中  window-preference   java-CodeTemplate   import导入 ![](https://box.kancloud.cn/0635228d1374eb7db0fae7a5c18d61fb_708x517.png) 6.7.1. 需求 根据商品 ID 查询商品信息,返回 json 数据。 6.7.2. Dao 层的实现 逆向工程已经生产了底层的代码,当前业务需要的功能已经实现,所以不用在写代码。 6.7.3. Service 实现 在 interface 中添加接口 ItemService ![](https://box.kancloud.cn/2c9d6c326144b8e938f4f648c5bfc884_325x94.png) 方法需求:传入参数 itemId,返回值为 TbItem 对象。 ~~~ package com.igeek.egobuy.service; import com.igeek.egobuy.pojo.TbItem; /** * @project_name buy-manager-interface * @package_name com.igeek.egobuy.service * @class_name ItemService * @author limiao * @Description:商品服务接口 * @company yhouse * @version 1.0 * @time 2019年1月12日 下午12:43:41 */ public interface ItemService { /** * * @param itemId * @return * TbItem * 通过id查询商品 * @author limiao * @time 2019年1月12日下午12:45:42 */ TbItem getById(long itemId); } ~~~ 在 service 工程中添加 ItemServiceImpl 实现类 ![](https://box.kancloud.cn/70db6f8d78b9b650d8143bd91e2b52b1_291x92.png) ~~~ package com.igeek.egobuy.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.igeek.egobuy.mapper.TbItemMapper; import com.igeek.egobuy.pojo.TbItem; import com.igeek.egobuy.service.ItemService; /** * @project_name buy-manager-service * @package_name com.igeek.egobuy.service.impl * @class_name ItemServiceImpl * @author limiao * @Description:商品的服务实现类 * @company yhouse * @version 1.0 * @time 2019年1月12日 下午12:47:22 */ @Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper tbItemMapper; /* (non-Javadoc) * @see com.igeek.egobuy.service.ItemService#getById(long) */ @Override public TbItem getById(long itemId) { // TODO Auto-generated method stub //直接通过id查询 TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId); return tbItem; //条件查询 // TbItem item=null; // TbItemExample example=new TbItemExample(); // Criteria criteria = example.createCriteria(); // criteria.andIdEqualTo(itemId); // List<TbItem> items = tbItemMapper.selectByExample(example); // if(items.size()>0){ // item=items.get(0); // } // return item; } } ~~~ 6.7.4. Controller 层的实现 在 web 工程中添加 ItemController 类 ~~~ package com.igeek.egobuy.controller; import org.springframework.beans.factory.annotation.Autowired; 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.igeek.egobuy.pojo.TbItem; import com.igeek.egobuy.service.ItemService; /** * @project_name buy-manager-web * @package_name com.igeek.egobuy.controller * @class_name ItemController * @author limiao * @Description:商品的表现层 * @company yhouse * @version 1.0 * @time 2019年1月12日 下午1:01:42 */ @Controller public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/item") @ResponseBody public TbItem getItemById(@RequestParam("id")Long itemId){ TbItem item = itemService.getById(itemId); return item; } } ~~~ 6.7.5. Mapper 绑定异常解决 启动项目,访问时会出现如下异常: ![](https://box.kancloud.cn/0c842380aa6513f3501990ad604c57b7_1289x649.png) 原因:逆向工程生成的 mapper 文件没有打包到项目中。 解决方案: 在 buy-manager-dao 工程的 pom 文件中添加如下配置: ~~~ <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> ~~~