day1
记录走过的坑
1 characterEncodingFilter字符编码过滤器
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--要使用的字符集)-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!--是否强制设置request的编码为encoding,默认false-->
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!--是否强制设置response的编码为encoding,建议设置为true,下面有关于这个参数的解释-->
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2 此处Mybatis的主配置文件路径不能加*
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:MybatisConfig.xml"></property>
<property name="mapperLocations" value="classpath*:com/kuilian/mapper/*Mapper.xml"></property>
</bean>
3.按条件进行分页查询sql语句
使用concat函数进行模糊查询
<select id="findAdminByKeyWord" parameterType="String" resultType="admin">
select * from t_admin
<where>
id like CONCAT("%",#{keyword},"%")
or
username like CONCAT("%",#{keyword},"%")
</where>
order by id,username
</select>
4.使用pageHelper插件
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 主配置文件路径-->
<property name="configLocation" value="classpath:MybatisConfig.xml"></property>
<!-- mapper配置文件地址-->
<property name="mapperLocations" value="classpath*:com/kuilian/mapper/*Mapper.xml"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<props>
<prop key="dialect">mysql</prop>
<prop key="reasonable">true</prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
5.使用layui的分页
(天才!!!自己研究了好久)
var laypage = layui.laypage;
laypage.render({
elem: 'test1'
,count: ${pageInfo.total} //数据总数,从服务端得到
,limit:${pageInfo.pageSize}
,page: true
//自定义hash值
,curr:'${param.pageNum}'
,jump: function(obj, first){
//obj包含了当前分页的所有参数,比如:
console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
console.log(obj.limit); //得到每页显示的条数
//首次不执行
if(!first){
console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
console.log(obj.limit); //得到每页显示的条数
<c:if test="${empty param.keyword}">
window.location.href = "to/admin/admin_page.html?pageNum=" + obj.curr;
</c:if>
<c:if test="${!empty param.keyword}">
window.location.href = "to/admin/admin_page.html?pageNum=" + obj.curr + "&keyword=" +${param.keyword};
</c:if>
}
}
})