首页 > 其他分享 >mybatis

mybatis

时间:2022-09-21 01:23:00浏览次数:62  
标签:MappedStatement Builder class mybatis new configuration id

1.概述

  本文主要演示如何使用代码直接使用mabatis而不是通过spring配置,mybatis本身就是一个拼sql都工具,xml里实现了多种sql表达式。

2.实现

     this.configuration = new Configuration();

        SqlSource raw = new XMLLanguageDriver().createSqlSource(configuration,"select * from ${srcTable} where id=#{id})",Map.class);

        MappedStatement.Builder mb = new MappedStatement.Builder(configuration,"mdata",raw, SqlCommandType.SELECT);

        List<ResultMap> resultMaps = new ArrayList<ResultMap>();

        resultMaps.add(new ResultMap.Builder(configuration,"mdataMap", Map.class,new ArrayList<ResultMapping>()).build());

        mb.resultMaps(resultMaps);

        MappedStatement mappedStatement = mb.build();

        configuration.addMappedStatement(mappedStatement);

        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);

调用时只需sqlSession.selectList("mdata")

mdata本质上只是id,如果是想使用mapper,只需将id定义为 全类名.方法名的形式

 

package com.demo;
public interface UserMapper{

      Map<String,String> test(Integer id);    

}
MappedStatement.Builder mb = new MappedStatement.Builder(configuration,"com.demo.UserMapper.test",raw, SqlCommandType.SELECT);

sqlSession.createMapper(UserMapper.class).test(1);

标签:MappedStatement,Builder,class,mybatis,new,configuration,id
From: https://www.cnblogs.com/yangyang12138/p/16714241.html

相关文章

  • MyBatis注解开发 | MyBatis
    1.常用注解@Insert新增@Update更新@Delete删除@Select查询@Result结果集@Results封装多个结果集@One一对一结果集@Many......
  • Mybatis使用PageHelper分页插件
    1<dependency>2<groupId>com.github.pagehelper</groupId>3<artifactId>pagehelper-spring-boot-starter</artifactId>4......
  • mybatisplus打印SQL日志 【mybatisplus专栏】
    一、mybatisplus如何打印SQL日志:在SpringBoot项目中添加以下配置#方式一mybatis-plus:configuration:log-impl:org.apache.ibatis.logging.stdout.StdOutImp......
  • 分析mybatis的#{}、${}(#绑定变量超过一定值导致Oracle挂掉)
    我后来改成了1万条,数据库相对小点压力不大,也不会出现重启的问题; 最近跟数据库干上了先说下问题起源,算奖确认订单,需要批量update订单,查了相关资料,是mybatis一次性绑定变......
  • MyBatis返回Map键值对数据Key值大小写问题
    Controller@RestController@RequestMapping("/web")publicclassMapKeyTest{@AutowiredprivateInvoicingBuyOrderServiceinvoicingBuyOrderService;......
  • Mybatis if标签判断数字大小
    if标签语法<select...>SQL语句1<iftest="条件表达式">SQL语句2</if></select>条件表达式中大于号小于号用gt,lt<iftest="numgt0">...</if><if......
  • Mybatis-plus 数据安全保护
    1、加密前 application-dev.ymlspring:datasource:url:jdbc:p6spy:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTim......
  • 05.有关MyBatis的配置与使用
    有关MyBatis的配置与使用虽然SpringDataJPA在国外广泛流行,但是在国内仍是MyBatis使用更加普遍整合MyBatis第一步:新建SpringBoot项目,在pom.xml中引入MyBatis的Starter......
  • MyBatisPlus-范围查询、模糊查询及排序查询
    MyBatisPlus-范围查询、模糊查询及排序查询原文链接:https://blog.csdn.net/m0_61961937/article/details/125967684一、范围查询二、模糊查询三、排序查询一、范围查......
  • Mybatis plus 查询
    1、子查询//管理主管查询IntegermanageSupervisor=query.getManageSupervisor();if(manageSupervisor!=null){......