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