1. XenForo 1.5.14 中文版——支持中文搜索!现已发布!查看详情
  2. Xenforo 爱好者讨论群:215909318 XenForo专区

新闻 Mybatis 通用 Mapper 2.2.0 发布 下载

本帖由 漂亮的石头2015-03-11 发布。版面名称:软件资讯

  1. 漂亮的石头

    漂亮的石头 版主 管理成员

    注册:
    2012-02-10
    帖子:
    486,020
    赞:
    46
    Mybatis通用Mapper

    极其方便的使用Mybatis单表的增删改查




    [COLOR=rgba(0, 0, 0, 0.8)]2.2.0[/COLOR]


    • 新增SqlMapper,可以使用MyBatis直接执行sql,详细文档



    2.2.0版本之后,通过SqlMapper可以支持多表的操作,但是需要在代码中直接写SQL。

    即使不使用通用mapper,相信SqlMapper也一定符合部分人的需求。

    示例:

    selectList:


    //查询,返回List<Map>
    List<Map<String, Object>> list = sqlMapper.selectList("select * from country where id < 11");

    //查询,返回指定的实体类
    List<Country> countryList = sqlMapper.selectList("select * from country where id < 11", Country.class);

    //查询,带参数
    countryList = sqlMapper.selectList("select * from country where id < #{id}", 11, Country.class);

    //复杂点的查询,这里参数和上面不同的地方,在于传入了一个对象
    Country country = new Country();
    country.setId(11);
    countryList = sqlMapper.selectList("<script>" +
    "select * from country " +
    " <where>" +
    " <if test=\"id != null\">" +
    " id &lt; #{id}" +
    " </if>" +
    " </where>" +
    "</script>", country, Country.class);

    selectOne:



    Map<String, Object> map = sqlMapper.selectOne("select * from country where id = 35");

    map = sqlMapper.selectOne("select * from country where id = #{id}", 35);

    Country country = sqlMapper.selectOne("select * from country where id = 35", Country.class);

    country = sqlMapper.selectOne("select * from country where id = #{id}", 35, Country.class);

    insert,delete,update:



    //insert
    int result = sqlMapper.insert("insert into country values(1921,'天朝','TC')");

    Country tc = new Country();
    tc.setId(1921);
    tc.setCountryname("天朝");
    tc.setCountrycode("TC");
    //注意这里的countrycode和countryname故意写反的
    result = sqlMapper.insert("insert into country values(#{id},#{countrycode},#{countryname})"
    , tc);


    //update
    result = sqlMapper.update("update country set countryname = '天朝' where id = 35");

    tc = new Country();
    tc.setId(35);
    tc.setCountryname("天朝");

    int result = sqlMapper.update("update country set countryname = #{countryname}" +
    " where id in(select id from country where countryname like 'A%')", tc);


    //delete
    result = sqlMapper.delete("delete from country where id = 35");
    result = sqlMapper.delete("delete from country where id = #{id}", 35);
    Mybatis 通用 Mapper 2.2.0 发布下载地址
     
正在加载...