发布于 2015-03-11 05:16:50 | 174 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的MyBatis教程,程序狗速度看过来!

MyBatis 基于Java的持久层框架

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。


 

Mybatis通用Mapper

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

2.2.0

  • 新增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 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。



历史版本 :
Mybatis 通用 Mapper 3.4.5 发布,增加代码生成器插件
MyBatis 逆向工程 MyBatis-CMEU v2.1.2 发布
Mybatis 分页插件 PageHelper 5.1.2 发布
mybatis-plus 2.1.1 发布,新增 mybatis 多租户 SQL 解析
MyBatis-Guice 3.10 发布,修复多项错误
mybatis-plus 2.1.1-SNAPSHOT,代号:小锅盖
MyBatis Ignite 1.0.6 发布,MyBatis 缓存插件
MyBatis 3.4.5 发布,Java 数据持久层框架
mybatis-plus 2.1.0 发布,代号:小秋秋
MyBatis 逆向工程 MyBatis-CMEU v2.1.0 发布
Mybatis 分页插件 PageHelper 5.0.4 发布
Mybatis-Plus 2.0.9 发布,简化开发 mybatis 快速入门
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务