栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis注解sql语句(mybatis 注解 字段映射)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis注解sql语句(mybatis 注解 字段映射)

文章目录

MyBaits参数传递

单个参数多个参数 注解开发

注解完成增删改查

提示 简单举例

编写接口方法编写测试方法

MyBaits参数传递

MyBatis接口方法中可以接收各种各样的参数,MyBatis底层对于这些参数进行不同的封装处理方式

单个参数

MyBatis提供了 ParamNameResolver类来进行参数封装

    POJO类型:直接使用.属性名 和 参数占位符名称 一致

    Map集合:直接使用,键名 和 参数占位符名称 一致

    Collection:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

    map.put(“arg0”,collection集合)

    map.put(“collection,collection集合”)

    List:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

    map.put(“arg0”,list集合)

    map.put(“collection”,list集合)

    map.put(“list”,list集合)

    Array:封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

    map.put(“arg0”,数组)

    map.put(“array”,数组)

    其他类型:直接使用

多个参数
//封装为Map集合,可以使用@Param注解,替换Map集合中默认的arg键名

map.put("arg0",参数值1)

map.put("param1",参数值1)

map.put("param2",参数值2)

map.put("agr1",参数值2)

//-----------------@Param("username")

map.put("username",参数值1)

map.put("param1",参数值1)
    
map.put("param2",参数值2)
    
map.put("agr1",参数值2)
注解开发 注解完成增删改查

使用注解开发会比配置文件开发更加方便

@Select

@Insert

@Update

@Delete

提示

注解完成简单功能配置文件完成复杂功能 简单举例

@Select("select * from tb_user where id = #{id}")
public User selectById(int id);
编写接口方法

编写BrandMapper.java

    @Select("select * from tb_brand where id = #{id}")
    Brand selectById02(int id);
编写测试方法

编写MyBatisTest.java

      @Test
    public void testSelectById02() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        Brand brand = brandMapper.selectById02(1);

        System.out.println(brand);

        sqlSession.close();

    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/776495.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号