MyBatis之无辅助实现物理分页
公司项目使用框架MyBatis+spring3+struts3+Maven3,我设计数据库和搭建框架,刚开始接触Maven,一开始考虑的是使用多模块Maven聚合,直接搞死个人,刚毕业半年,恶补Maven,终于将多模块框架搭建好了,可是由于开发组人员技术问题,改为单个的Maven框架,又搞半天,但由于之前的折磨,还好没费多长时间,但是在MyBtis时又出问题 直接抓狂,之前一直使用2.3.4.726,自动生成javabean没出过问题,然后用3.0的插件生成的表居然少字段,蛋疼!再之后,只要是WEB都会遇到的物理分页问题,网上各种拦截器,我以为3.0会支持,失望之余,想到MyBatis物理分页都是通过分页助手传参,用limit之类的函数实现,于是想着,要是能同时把2个javabean传入,那问题也就解决了,思路是对的,但怎么传多个对象参数,去API上没发现,在折磨了几个小时后,看到了一篇混合传参的文章,于是混合试了下,不好用,又继续折腾了几个小时,终于在http://bbs.csdn.net/topics/390659759看到一篇文章,受到启发。思路是将两个对象放到Map中,传到sqlMapper中,然后取条件。具体的示例如下:
基础测试类:
/** * dao测试类,没有采用spring注入,所有DAO测试可以继承该类进行测试 * @author DR.YangLong * */public abstract class DaoTest {// sqlSessionFactoryprivate static SqlSessionFactory sqlSessionFactory;// mybatis fileprivate static final String MYBATIS_CONFIG_FILENAME = "mybatis-config.xml";static {Reader reader = null;try {reader = Resources.getResourceAsReader(MYBATIS_CONFIG_FILENAME);} catch (IOException e) {System.out.println(e.getMessage());}// build SqlSessionFactory by SqlSessionFactoryBuilder,the builder used oncesqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);}// the method get SqlSessionFactorypublic static SqlSessionFactory getSqlSessionFactory() {return sqlSessionFactory;}// the tested method,this is templates method@Testpublic void addUser() {// SqlSessionSqlSession sqlSession = getSqlSessionFactory().openSession();try {//create dao by SqlSessionUserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);UserInfo userinfo = new UserInfo();userinfo.setUsername("Maven");userinfo.setPasw("thisistest");userinfo.setBirthday("1999-10-11");int id=mapper.insertSelective(userinfo);sqlSession.commit();System.out.println(id);} finally {sqlSession.close();}}}
/** * Author: Dream.YnagLong * Date: 13-12-19 * Time: 下午6:55 * E-mail:410357434@163.com */public class SuperUser extends DaoTest { @Test public void getUserList(){ SqlSession sqlSession = getSqlSessionFactory().openSession(); try { //create dao by SqlSession UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class); UserInfo userinfo = new UserInfo(); userinfo.setPhone("135646586"); userinfo.setUserlevel(1); userinfo.setVerifystatus(1); PagerHelper pagerHelper=new PagerHelper(); int totalrows=mapper.getUserInfoCount(userinfo); System.out.println(totalrows); pagerHelper.setTotalRows(totalrows); pagerHelper.setCurrentPage(1); Map map=new HashMap(); map.put("userinfo",userinfo); map.put("page",pagerHelper); List<UserInfo> list=mapper.getUserInfoList(map); for(UserInfo userIn:list){ System.out.println(userIn.getUsername()); } sqlSession.commit(); } finally { sqlSession.close(); } }}
<select id="getUserInfoCount" parameterType="com.renyuandao.model.UserInfo" resultType="java.lang.Integer"> select count(0) from user_info where 1=1 <if test="phone != null and phone!=''"> and phone = #{phone} </if> <if test="userlevel != null and userlevel!=''"> and userlevel = #{userlevel} </if> <if test="useraddress != null and useraddress!=''"> and useraddress = #{useraddress} </if> <if test="verifystatus != null and verifystatus!=''"> and verifystatus =#{verifystatus} </if> </select> <select id="getUserInfoList" resultMap="BaseResultMap"> select * from user_info where 1=1 <if test="phone != null and phone!=''"> and phone = #{userinfo.phone,jdbcType=VARCHAR}, </if> <if test="userlevel != null and userlevel!=''"> and userlevel = #{userinfo.userlevel,jdbcType=INTEGER}, </if> <if test="useraddress != null and useraddress!=''"> and useraddress = #{userinfo.useraddress,jdbcType=VARCHAR}, </if> <if test="verifystatus != null and verifystatus!=''"> and verifystatus = #{userinfo.verifystatus,jdbcType=INTEGER} </if> order by id limit #{page.startRow},#{page.pageSize} </select>