Mybatis使用的几种方式

快来打我* 2022-05-23 12:05 296阅读 0赞

第一种 使用Mapper接口

  1. @Autowired
  2. private WechatBindUserMapper tWechatBindUserMapper;
  3. @Override
  4. public WechatBindUser queryWechatBindInfo(Map<String, Object> param) {
  5. return tWechatBindUserMapper.queryWechatBindInfo(param);
  6. }

第二种 使用sqlSession+statement

这方式不用写Mapper接口文件,通过

  1. Enterprise enterprise = (Enterprise) this.getSqlSession().
  2. selectOne("EnterpriseMapper.selectByEnterpriseId", enterpriseId);

第三种 自实现的一种通用查询方法

  1. public int update(T object) {
  2. EntityDef entityDef = EntityCache.getEntityDef(this.getTClass());
  3. Map params = new HashMap();
  4. params.put("schema", entityDef.getSchema());
  5. params.put("table", entityDef.getTable());
  6. params.put("id", entityDef.getIdUpperCase());
  7. params.put("idValue", entityDef.getIdValue(object));
  8. params.put("columnList", entityDef.getKeyValueList(object));
  9. return this.getSqlSession().update("core.entity.update", params);
  10. }

自定义了一种core.entity的基础mapper

  1. <!-- update record by primary key -->
  2. <update id="update" parameterType="java.util.HashMap">
  3. update
  4. <if test="schema!=null">
  5. ${schema}.${table}
  6. </if>
  7. <if test="schema==null">
  8. ${table}
  9. </if>
  10. <set>
  11. <if test="columnList!=null">
  12. <foreach collection="columnList" item="item" index="index"
  13. separator=",">
  14. <if test="item.value!=null">
  15. ${item.key} =
  16. #{item.value}
  17. </if>
  18. </foreach>
  19. </if>
  20. </set>
  21. where ${id} =#{idValue}
  22. </update>

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读