Spring+MyBatis项目开发代码步骤

自己总结而已。

step1、pojo对象

根据db结构及需求文档,创建基础的pojo对象;

 

step2、映射接口

根据需求文档,定义基础的映射方法,例如基本的增、删、改,存在性检查,计数,分页搜索等。

public int insert(T t);
public int update(T t);
public int delete(int id);
public int count(String where);
public List<T> search(@Param("where")String where, @Param("offset")int offset, @Param("rows")int rows, @Param("orderby")String orderby);

其中:update和delete默认返回的是操作结果数目;insert通常可以返回increment或者条数;

传递多参数,可以用@Param进行标注

 

step3、映射文件

根据映射接口,编写Mapper文件,对应映射接口的指定类型参数,可以使用如下代码接收:

<if test="orderby!=null">
  order by #{orderby, jdbcType=VARCHAR}
</if>

对于返回单一类型的查询,建议指定resultType,避免运行时错误,例如:

<select id="count" resultType="int">
  select count(1) from tablename
  <include refid="searchWhere" />
   limit 1
</select>

 

step4、服务接口

很多时候,服务接口与dao操作接口并不需要1:1的关系,service方法可以灵活组装,调用多个dao方法;反过来也可以多个service方法,调用一个dao方法,例如:

public int insert(T t);

 

step5、服务实现

实现上面的接口,灵活调用dao方法,例如:

public int insert(T t) {
  if (dao.count("some where") > 0 ){
    return -1;
  }
  return dao.insert(t);
}

 

step6、添加测试类

 

step7、整合进其他应用

 

…………

转载于:https://www.cnblogs.com/bashenandi/p/6744049.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值