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 < #{id}" + " </if>" + " </where>" + "</script>", country, Country.class) ;``` selectOne: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:
//insertint 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);//updateresult = 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);//deleteresult = sqlMapper.delete("delete from country where id = 35");result = sqlMapper.delete("delete from country where id = #{id}", 35);
相关资源:敏捷开发V1.0.pptx