您的位置:首页 > 编程语言 > Java开发

mybatis+springMVC的多表分页

2016-04-25 19:21 525 查看
po类直接生成

user的扩展类userCustom

package com.cn.imax.po;

/**
* @ClassName: UserCustom
* @Description: 用户的扩展类信息
* @author: Administrator
* @date: 2016年4月15日 上午9:22:37
*/
//通过此类映射订单和用户查询的结果,让此类继承包括 字段较多的pojo类
public class UserCustom extends User{

//添加用户的扩展信息
//添加用户属性
/*permission.name,
*/
//权限信息
private Permission permission;

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Permission getPermission() {
return permission;
}

public void setPermission(Permission permission) {
this.permission = permission;
}

}


userService,userMapperCustom类调用的方法略

userMapperCustom.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cn.imax.mapper.UserMapperCustom">
<!-- 注意唯一标识 -->
<resultMap id="BaseResultMap" type="com.cn.imax.po.UserCustom">
<id column="username" property="username" jdbcType="VARCHAR" />
<result column="id" property="id" jdbcType="INTEGER" />
<result column="pwd" property="pwd" jdbcType="VARCHAR" />
<result column="right_id" property="rightId" jdbcType="INTEGER" />
<!-- 配置映射的关联权限信息 -->
<!-- association:用户映射关联查询单个对象的信息 -->
<!-- property:要将关联查询的用户信息映射到UserCustom中哪个属性 -->
<association property="permission" javaType="com.cn.imax.po.Permission">
<!-- id关联用户的唯一标识 column: 用与表示权限唯一信息的列 javaType:映射到right的哪个属性 -->
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
</association>
</resultMap>

<!-- 定义用户查询的sql片段,就是用户查询条件 -->
<sql id="query_user_where">
<!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
<!-- 用户查询条件通过UserQueryVo包装对象 中userCustom属性传递 -->
<if test="userCustom!=null">
<if test="userCustom.username!=null and userCustom.username!=''">
and user.username LIKE '%${userCustom.username}%'
</if>
</if>
</sql>

<select id="queryUserListByPage" parameterType="java.util.Map"
resultMap="BaseResultMap">
<!-- SELECT user.* FROM user -->
SELECT
user.id,
username,
pwd,
permission.name
FROM
permission,
user
<where>
user.right_id = permission.id
<include refid="query_user_where"></include>
</where>
order by User.ID
</select>
</mapper>


分页拦截器

package com.cn.imax.interceptor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import com.cn.imax.entity.Page;

/**
* 分页拦截器
*/
@Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})})
public class PageInterceptor implements Interceptor {

@SuppressWarnings("unused")
private String test;
/**
* 拦截下来的对象的信息
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler)invocation.getTarget();
MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY);
MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement");
// 配置文件中SQL语句的ID
String id = mappedStatement.getId();
if(id.matches(".+ByPage$")) {
BoundSql boundSql = statementHandler.getBoundSql();
// 原始的SQL语句
String sql = boundSql.getSql();
// 查询总条数的SQL语句
String countSql = "select count(*) from (" + sql + ")a";
Connection connection = (Connection)invocation.getArgs()[0];
PreparedStatement countStatement = connection.prepareStatement(countSql);
ParameterHandler parameterHandler = (ParameterHandler)metaObject.getValue("delegate.parameterHandler");
parameterHandler.setParameters(countStatement);
ResultSet rs = countStatement.executeQuery();

Map<?,?> parameter = (Map<?,?>)boundSql.getParameterObject();
Page page = (Page)parameter.get("page");
if(rs.next()) {
page.setTotalNumber(rs.getInt(1));
}
// 改造后带分页查询的SQL语句
String pageSql = sql + " limit " + page.getDbIndex() + "," + page.getDbNumber();
metaObject.setValue("delegate.boundSql.sql", pageSql);
}
return invocation.proceed();
}

/**
* 拦截准备要分页的sql语句如果是就拦截不是就放掉
*/
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}

@Override
public void setProperties(Properties properties) {
this.test = properties.getProperty("test");
}

}


等一些基本代码的实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: