本文共 1801 字,大约阅读时间需要 6 分钟。
Mybatis通用Mapper
极其方便的使用Mybatis单表的增删改查2.2.0
2.2.0版本之后,通过SqlMapper可以支持多表的操作,但是需要在代码中直接写SQL。
即使不使用通用mapper,相信SqlMapper也一定符合部分人的需求。
示例:
selectList:
//查询,返回List
Map 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);转载地址:http://igbuo.baihongyu.com/