课程/Java/后端开发
通过自动回复机器人学Mybatis---加强版
-
-
钢管舞学员
2021-03-19
- 拦截器的流程
-
截图
0赞 · 0采集
-
-
波阿斯
2017-12-26
- 拦截器实现3
过滤客户 ----- id.matches(".+ByPage$")
获取购票信息 ----- page
购票 ----- 原始sql-->pageSql
送票 ----- return invocation.proceed();
-
0赞 · 0采集
-
-
qq_醉卧伊云上_03761087
2017-12-22
- 过程啊
-
截图
0赞 · 0采集
-
-
木木嗷
2017-10-23
- 拦截器实现3
过滤客户 ----- id.matches(".+ByPage$")
获取购票信息 ----- page
购票 ----- 原始sql-->pageSql
送票 ----- return invocation.proceed();
-
截图
0赞 · 0采集
-
-
木木嗷
2017-10-23
- 其他相关代码:
@Message.xml
<select id="queryMessageListByPage" parameterType="java.util.Map" resultMap="MessageResult">
select <include refid="columns"></include> from MESSAGE
<where>
<if test="message.command != null and !"".equals(message.command.trim())">
and COMMAND=#{message.command}
</if>
<if test="message.description != null and !"".equals(message.description.trim())">
and DESCRIPTION like '%' #{message.description} '%'
</if>
</where>
order by ID
</select>
@Configuration.xml
<plugins>
<!-- <plugin interceptor="com.imooc.interceptor.PageInterceptor" ></plugin> 如果不需要初始化参数(setProperties()),则直接这条语句即可 -->
<plugin interceptor="com.imooc.interceptor.PageInterceptor">
<property name="test" value="abc"></property>
</plugin>
</plugins>
Ps:一个拦截器对应一个<plugin>。
-
0赞 · 1采集
-
-
霜花似雪
2017-10-07
- 拦截器实现3
过滤客户 ----- id.matches(".+ByPage$")
获取购票信息 ----- page
购票 ----- 原始sql-->pageSql
送票 ----- return invocation.proceed();
-
截图
0赞 · 1采集
-
-
霜花似雪
2017-10-07
- 其他相关代码:
@Message.xml
<select id=”queryMessageListByPage” parameterType=”java.util.Map” resultMap=”MessageResult”>
select <include refid=”columns”></include> from MESSAGE
<where>
<if test=”message.command != null and !”“.equals(message.command.trim())”>
and COMMAND=#{message.command}
</if>
<if test=”message.description != null and !”“.equals(message.description.trim())”>
and DESCRIPTION like ‘%’ #{message.description} ‘%’
</if>
</where>
order by ID
</select>
@Configuration.xml
<plugins>
<!— <plugin interceptor=”com.imooc.interceptor.PageInterceptor” ></plugin> 如果不需要初始化参数(setProperties()),则直接这条语句即可 —>
<plugin interceptor=”com.imooc.interceptor.PageInterceptor”>
<property name=”test” value=”abc”></property>
</plugin>
</plugins>
Ps:一个拦截器对应一个<plugin>。
-
截图
0赞 · 0采集
-
-
丶子非鱼
2017-06-26
- 拦截器5
-
截图
0赞 · 0采集
-
-
丶子非鱼
2017-06-26
- 拦截器4
-
截图
0赞 · 0采集
-
-
丶子非鱼
2017-06-26
- 拦截器3
-
截图
0赞 · 0采集
-
-
丶子非鱼
2017-06-26
- 拦截器2
-
截图
0赞 · 0采集
-
-
丶子非鱼
2017-06-26
- 拦截器总结1
-
截图
0赞 · 0采集
-
-
DR枫林残忆
2017-05-20
- 拦截器实现3
过滤客户 ----- id.matches(".+ByPage$")
获取购票信息 ----- page
购票 ----- 原始sql-->pageSql
送票 ----- return invocation.proceed();
-
0赞 · 0采集
-
-
DR枫林残忆
2017-05-20
- 拦截器实现2
-
截图
0赞 · 0采集
-
-
DR枫林残忆
2017-05-20
- 拦截器实现1
-
截图
0赞 · 0采集
-
-
NICE_Love
2017-03-17
- 1 2 3
-
截图
0赞 · 0采集
-
-
慕粉2216198324
2016-11-09
- 返还控制权
-
截图
0赞 · 0采集
-
-
慕粉2216198324
2016-11-09
- 第三步
-
截图
0赞 · 0采集
-
-
慕粉2216198324
2016-11-09
- 第二步
-
截图
0赞 · 0采集
-
-
慕粉2216198324
2016-11-09
- 注册拦截器
-
截图
0赞 · 0采集
-
-
小浩233
2016-08-19
- 其他相关代码:
@Message.xml
<select id="queryMessageListByPage" parameterType="java.util.Map" resultMap="MessageResult">
select <include refid="columns"></include> from MESSAGE
<where>
<if test="message.command != null and !"".equals(message.command.trim())">
and COMMAND=#{message.command}
</if>
<if test="message.description != null and !"".equals(message.description.trim())">
and DESCRIPTION like '%' #{message.description} '%'
</if>
</where>
order by ID
</select>
@Configuration.xml
<plugins>
<!-- <plugin interceptor="com.imooc.interceptor.PageInterceptor" ></plugin> 如果不需要初始化参数(setProperties()),则直接这条语句即可 -->
<plugin interceptor="com.imooc.interceptor.PageInterceptor">
<property name="test" value="abc"></property>
</plugin>
</plugins>
Ps:一个拦截器对应一个<plugin>。
-
0赞 · 1采集
-
-
小浩233
2016-08-19
- @PageInterceptor.java相关代码——(下):
@Override
public Object plugin(Object target) {
System.out.println(test);
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
this.test=properties.getProperty("test");
}
}
Ps1:执行顺序:setProperties-->plugin-->[intercept]
Ps2:若因为某些原因获取不到想要调用的方法等,可以利用反射思想获取。
Ps3:
两个特殊的符号'^'和'$'。他们的作用是分别指出一个字符串的开始和结束。例子如下:
"^The":表示所有以"The"开始的字符串("There","The cat"等);
"of despair$":表示所以以"of despair"结尾的字符串;
"^abc$":表示开始和结尾都是"abc"的字符串——呵呵,只有"abc"自己了;
"notice":表示任何包含"notice"的字符串。
附:"ab+":表示一个字符串有一个a后面跟着至少一个b或者更多。
-
0赞 · 1采集
-
-
小浩233
2016-08-19
- @PageInterceptor.java相关代码——(中):
if(id.matches(".+ByPage$")){
BoundSql boundSql=statementHandler.getBoundSql();
//原始的SQL语句,但是当中的#{}改为?
String sql=boundSql.getSql();
//查询总条数的SQL语句
String countSql="select count(*) from ("+sql+")a";
Connection conn=(Connection) invocation.getArgs()[0];
PreparedStatement countStatement= conn.prepareStatement(countSql);
/**
* 解决当中的#{}改为?后的参数传递问题
*/
ParameterHandler parameterHandler=(ParameterHandler) metaObject.getValue("delegate.parameterHandler");
parameterHandler.setParameters(countStatement);
ResultSet rs=countStatement.executeQuery();
//获取的是Dao层中的“parameter”参数
Map<?, ?> parameter=(Map<?, ?>) boundSql.getParameterObject();
Page page=(Page) parameter.get("page");
if(rs.next()){//因为只有一条数据,所以不需要while循环
page.setTotalNumber(rs.getInt(1));
}
//改造后带分页查询的SQL语句
String pageSql=sql+" limit "+page.getDbIndex()+","+page.getDbNumber();
metaObject.setValue("delegate.boundSql.sql", pageSql);
}
return invocation.proceed();
}
-
0赞 · 1采集
-
-
小浩233
2016-08-19
- @PageInterceptor.java相关代码——(上):
@Intercepts({ @Signature(args = { Connection.class }, method = "prepare", type = StatementHandler.class) })
public class PageInterceptor<V, K> implements Interceptor {
private String test;
@Override
public Object intercept(Invocation invocation) throws Throwable {
/**
* 注解里写的参数封装成invocation,通过invocation获取,最后记得invocation.proceed()返回以上操作的代理对象并继续执行。
*/
//getTarget():return Plugin.wrap(target, this);
StatementHandler statementHandler=(StatementHandler) invocation.getTarget();
//通过反射获取对象
MetaObject metaObject=MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
//通过RoutingStatementHandler中的delegate匹配对应的BaseStatementHandler
MappedStatement mappedStatement=(MappedStatement) metaObject.getValue("delegate.mappedStatement");
//配置文件中SQL语句的ID
String id=mappedStatement.getId();
-
0赞 · 1采集
-
-
黑白K
2016-06-23
- 先注册,在写实现
-
截图
0赞 · 0采集
-
-
黑白K
2016-06-23
- @PageInterceptor.java相关代码——(中):
if(id.matches(”.+ByPage$”)){
BoundSql boundSql=statementHandler.getBoundSql();
//原始的SQL语句,但是当中的#{}改为?
String sql=boundSql.getSql();
//查询总条数的SQL语句
String countSql=”select count(*) from (”+sql+”)a”;
Connection conn=(Connection) invocation.getArgs()[0];
PreparedStatement countStatement= conn.prepareStatement(countSql);
/**
* 解决当中的#{}改为?后的参数传递问题
*/
ParameterHandler parameterHandler=(ParameterHandler) metaObject.getValue(”delegate.parameterHandler”);
parameterHandler.setParameters(countStatement);
ResultSet rs=countStatement.executeQuery();
//获取的是Dao层中的“parameter”参数
Map<?, ?> parameter=(Map<?, ?>) boundSql.getParameterObject();
Page page=(Page) parameter.get(”page”);
if(rs.next()){//因为只有一条数据,所以不需要while循环
page.setTotalNumber(rs.getInt(1));
}
//改造后带分页查询的SQL语句
String pageSql=sql+” limit “+page.getDbIndex()+”,”+page.getDbNumber();
metaObject.setValue(”delegate.boundSql.sql”, pageSql);
}
return invocation.proceed();
}
-
0赞 · 0采集
-
-
黑白K
2016-06-23
- @PageInterceptor.java相关代码——(下):
@Override
public Object plugin(Object target) {
System.out.println(test);
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
this.test=properties.getProperty(”test”);
}
}
Ps1:执行顺序:setProperties—>plugin—>[intercept]
Ps2:若因为某些原因获取不到想要调用的方法等,可以利用反射思想获取。
Ps3:
两个特殊的符号’^’和’$’。他们的作用是分别指出一个字符串的开始和结束。例子如下:
“^The”:表示所有以”The”开始的字符串(”There”,”The cat”等);
“of despair$”:表示所以以”of despair”结尾的字符串;
“^abc$”:表示开始和结尾都是”abc”的字符串——呵呵,只有”abc”自己了;
“notice”:表示任何包含”notice”的字符串。
附:”ab+”:表示一个字符串有一个a后面跟着至少一个b或者更多。
-
0赞 · 0采集
-
-
黑白K
2016-06-23
- @PageInterceptor.java相关代码——(上):
@Intercepts({ @Signature(args = { Connection.class }, method = “prepare”, type = StatementHandler.class) })
public class PageInterceptor<V, K> implements Interceptor {
private String test;
@Override
public Object intercept(Invocation invocation) throws Throwable {
/**
* 注解里写的参数封装成invocation,通过invocation获取,最后记得invocation.proceed()返回以上操作的代理对象并继续执行。
*/
//getTarget():return Plugin.wrap(target, this);
StatementHandler statementHandler=(StatementHandler) invocation.getTarget();
//通过反射获取对象
MetaObject metaObject=MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory());
//通过RoutingStatementHandler中的delegate匹配对应的BaseStatementHandler
MappedStatement mappedStatement=(MappedStatement) metaObject.getValue(”delegate.mappedStatement”);
//配置文件中SQL语句的ID
String id=mappedStatement.getId();
-
0赞 · 0采集
-
-
黑白K
2016-06-23
- 其他相关代码:
@Message.xml
<select id=”queryMessageListByPage” parameterType=”java.util.Map” resultMap=”MessageResult”>
select <include refid=”columns”></include> from MESSAGE
<where>
<if test=”message.command != null and !”“.equals(message.command.trim())”>
and COMMAND=#{message.command}
</if>
<if test=”message.description != null and !”“.equals(message.description.trim())”>
and DESCRIPTION like ‘%’ #{message.description} ‘%’
</if>
</where>
order by ID
</select>
@Configuration.xml
<plugins>
<!— <plugin interceptor=”com.imooc.interceptor.PageInterceptor” ></plugin> 如果不需要初始化参数(setProperties()),则直接这条语句即可 —>
<plugin interceptor=”com.imooc.interceptor.PageInterceptor”>
<property name=”test” value=”abc”></property>
</plugin>
</plugins>
Ps:一个拦截器对应一个<plugin>。
-
0赞 · 0采集
-
-
慕仔6423193
2016-06-01
- 拦截器步骤04
-
截图
0赞 · 1采集