美文网首页
mapper调用的两种方式:注入mapper/SqlSessio

mapper调用的两种方式:注入mapper/SqlSessio

作者: 墨色尘埃 | 来源:发表于2017-12-17 15:56 被阅读238次

第一种:control类中注入xxMapper类,调用mapper方法

第二种:control类中注入SqlSession,EnterpriseBaseMapper类中定义的接口,selectByOrg是其中的一个方法,SqlSession的方法中,第二个参数是后台或前端传入的参数,可以是对象

"com.jsptpd.gayg.modules.cloud.dao.EnterpriseBaseMapper.selectByOrg"

示例一

@RestController
@RequestMapping("/v1/enterpriseInfo")
public class EnterpriseBaseControl {
    @Autowired
    private EnterpriseBaseMapper enterpriseBaseMapper;

    @Autowired
    SqlSession sqlSession;

    /**
     * 获取列表
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseObj<PageUtil> list(@RequestParam Map<String, String> formData) {

        formData = CommonUtil.convertFormData(formData);
        Integer integer = enterpriseBaseMapper.countByRole(formData);
        List<EnterpriseBase> enterpriseBases = enterpriseBaseMapper.selectByOrg(formData);

        PageUtil pageInfo = new PageUtil(integer, formData);
        pageInfo.setResultList(enterpriseBases);

        return new ResponseObj<PageUtil>(pageInfo, RetCode.SUCCESS);
    }

    /**
     * 通过sqlSession
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseObj<PageUtil> list1(@RequestParam Map<String, String> formData) {

        List<Object> objects = sqlSession.selectList("com.jsptpd.gayg.modules.cloud.dao.EnterpriseBaseMapper" +
                ".selectByOrg",......);

        return new ResponseObj<PageUtil>(pageInfo, RetCode.SUCCESS);
    }

}

示例二

@Component
public class RestExecute {
    @Autowired
    public SqlSession sqlSession;

    @Value("${pName}")
    private String pName;

    @Value("${modelName}")
    private String modelName;

    public ResponseObj<PageUtil> get(Map<String, Object> map) {
        String tableName = (String) map.get("_tbName");
        List<Integer> list = sqlSession.selectList(pName + tableName
                + "Mapper.countByRole", map);
        List<Object> busiInfoList = sqlSession.selectList(pName + tableName
                + "Mapper.selectByOrg", map);
        PageUtil pageUtil = new PageUtil(list.size(), map);
        pageUtil.setResultList(busiInfoList);
        return new ResponseObj<>(pageUtil, RetCode.SUCCESS);
    }

    public ResponseObj<Boolean> insert(@RequestBody Object obj) {
        String tableName = (String) ((Map) obj).get("_tbName");

        // TODO: 2017/12/16 需要前端传字段名为id过来,其他不行,后台再转换下
//        String id = (String) ((Map) obj).get("id");
//        try {
//            //利用反射,通过完整类名找到类对象
//            Object a = Class.forName(modelName + tableName).newInstance();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

        int i = sqlSession.insert(pName + tableName + "Mapper.insertSelective", obj);
        if (i == 1)
            return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
        return new ResponseObj<Boolean>(false, RetCode.SUCCESS);
    }

    public ResponseObj<Boolean> update(@RequestBody Object obj) {
        String tableName = (String) ((Map) obj).get("_tbName");
        int i = sqlSession.update(pName + tableName + "Mapper.updateByPrimaryKeySelective", obj);
        if (i == 1)
            return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
        return new ResponseObj<Boolean>(false, RetCode.SUCCESS);
    }

    public ResponseObj<Boolean> delelebylogic(@RequestBody Object obj) {
        String tableName = (String) ((Map) obj).get("_tbName");
        int i = sqlSession.update(pName + tableName + "Mapper.updateByPrimaryKeySelective", obj);
        if (i == 1)
            return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
        return new ResponseObj<Boolean>(false, RetCode.SUCCESS);
    }

    public ResponseObj<Boolean> delete(@RequestParam Object obj) {
        String tableName = (String) ((Map) obj).get("_tbName");
        int i = sqlSession.update(pName + tableName + "Mapper.deleteByPrimaryKey", obj);
        if (i == 1)
            return new ResponseObj<Boolean>(true, RetCode.SUCCESS);
        return new ResponseObj<Boolean>(false, RetCode.SUCCESS);
    }


}

相关文章

网友评论

      本文标题:mapper调用的两种方式:注入mapper/SqlSessio

      本文链接:https://www.haomeiwen.com/subject/xpaywxtx.html