基于SSM的java项目中文乱码和动态sql语句问题(以及easyui分页显示的问题)

1. 关于前台页面的汉字传输到后台变成乱码的问题

首先,要在项目的web.xml的配置文件中配置设置编码格式的过滤器

	<!-- 编码过滤器开始 -->	
	<filter>		
	<filter-name>encodingFilter</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>	
	</filter>	
	
	<filter-mapping>		
	<filter-name>encodingFilter</filter-name>		
	<!-- <url-pattern>/*</url-pattern> -->		
	<servlet-name>springmvc</servlet-name>	
	</filter-mapping>	
	<!-- 编码过滤器结束 -->

说明:servlet-name标签中的springmvc指代的是前端控制器DispatcherServlet的servlet-name。
如果以上配置已完成,还出现乱码,就要考虑Tomcat的编码设置了。
在Tomcat文件夹中,config文件夹下的server.xml中配置相关代码

<Connector port="8080" protocol="HTTP/1.1"  
         connectionTimeout="20000"          
         redirectPort="8443" URIEncoding="UTF-8" />

2. 关于在MyBatis自动生成的sql语句的xxxmapper.xml文件中,自己书写sql语句的一些问题

如果是模糊查询语句,并且是动态sql语句,获取的值是从前台页面传输过来的,此时如果值为String类型就可以写成

<if test="identity!=null and identity!=''">
	and identity like "%"#{identity}"%"
</if>

如果值为日期格式类型就只需要写是否是null的判断,就不要写空字符串的判断了,如果写了就会报错,报错的大致意思就是String类型的值不能转换为Date类型,正确写法如下:

<if test="startTime!=null">
	and createtime>=#{startTime}
</if>

还有关于日期格式类型的动态sql语句,比较一个时间点大于或者小于某个时间点时的写法

<if test="startTime!=null">
	and createtime>=#{startTime}
</if>
<if test="endTime!=null">
	and createtime <![CDATA[<=]]> #{endTime}
</if>

3. 关于使用easyui这个前端框架的分页查询时的一些问题

当使用DataGrid(数据表格)分页显示数据时,代码展示

<!-- 车辆数据表格开始 -->
	<table id="carTable" style="width:'100%';height:350px" ></table>

	<div id="toolbar">
		<a  class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" id="addCar">添加车辆</a>
		<a  class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" id="updateCar">修改车辆</a>
		<a  class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" id="deleteCar">删除车辆</a>
	</div>
  <!-- 车辆数据表格结束 -->
<script type="text/javascript">
 
 //加载表格数据
 $("#carTable").datagrid({
	 title:'车辆列表',
	 iconCls:'icon-save',
	 singleSelect:true,
	 collapsible:true,
	 url:'${ctx}/car/loadAllCar.action',
	 method:'get',
	 fitColumns:true,
	 toolbar:'#toolbar',
	 loadMsg:'数据正在努力加载中。。。',
	 emptyMsg:'查无数据',
	 pagination:true,
	 rownumbers:true,
	 
	 columns:[[
	           {field:'carnumber',width:80,title:'车辆号牌',width:100,align:'center'},
	           {field:'cartype',width:100,title:'车辆类型'},
	           
	           {field:'color',width:80,align:'center',title:'车辆颜色'},
	           {field:'price',width:80,title:'购买价格',align:'center'},
	           {field:'rentprice',width:80,title:'出租价格',align:'center'},
	           {field:'deposit',width:80,title:'出租押金',align:'center'},
	           {field:'description',width:80,align:'center',title:'车辆描述'},
	           {field:'isrenting',width:60,align:'center',title:'车辆是否出租',
                           formatter:function(value,row,index){
	        	           return value==1?"<font color='blue'>已出租</font>":"<font color='red'>未出租</font>";
	           }}
	  ]],
	  //可展开的效果
	  view:detailview,
	  detailFormatter:function(index,row){
		  return "<img width=200 height=150 src='"+row.carimg+"'/>";
	  }
 });
</script>

上面代码的说明:
iconCls 表示图标属性
plain 表示按钮是否为简单按钮,也可理解为是否有背景效果,true为简单按钮即无背景效果,false表示不是简单按钮即有背景效果
singleSelect 表示这个数据表格是否为单选,true则表示单选
collapsible 表示这个数据表格是否可以上下折叠
url 表示这个数据表格的数据来源,即通过请求得到数据返回值
method 表示请求的方式
fitColumns 表示数据表格的各列是否自适应,如果设置为false,还可以在columns中通过width自行设置每一列的宽度
toolbar 表示数据表格的工具栏(一般在表格标题的下面,内容的上面),还有一个buttons表示数据表格下面的工具栏
loadMsg 表示加载数据时显示的信息
emptyMsg 表示没有符合查询条件的结果时显示的信息
pagination 表示是否显示数据表格的分页栏,默认在表格的下面,可以设置它的位置
rownumbers 表示是否显示数据表格每一行的行号
columns 设置数据表格每一列对应的字段名(即传过来的结果的属性或者key值)
formatter 表示数据显示时进行相应转换的(调整)的函数
view 设置这个表示数据表格的每一行还可以继续展开,显示细节
detailFormatter 是用于设置展开时的显示效果
基于SSM的java项目中文乱码和动态sql语句问题(以及easyui分页显示的问题)
上面说明的是数据表格的基本用法,接下来讲解分页功能的一些具体情况
当我们点击数据表格的上一页或下一页或每页显示多少条数据时,它会自动的在请求的url后面加上page(表示第几页)和rows(表示每页显示多少条记录),此时我们就需要在类上加上这两个属性来接收这两个值,此时就用到了VO思想

package com.dxl.bus.vo;

import com.dxl.bus.domain.Car;

public class CarVo extends Car{
	
	private Integer page; //当前页
	private Integer rows; //每页显示的条数

	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
}

当我们查询到结果返回给这个数据表格时,需要调整为json格式进行传递,因为数据表格默认接收的是json格式的数据,所以此时我们自己要封装一个类,进行数据的传递

package com.dxl.sys.utils;

import java.util.List;

/**
 * 适配easyui表格数据的json对象
 * @author DXL
 */
public class DataGridView {

	private Long total;  //总记录数
	private List<?> rows;   //每页的数据	
	
	public DataGridView(Long total, List<?> rows) {
		super();
		this.total = total;
		this.rows = rows;
	}
	public DataGridView() {
		super();
	}
	public Long getTotal() {
		return total;
	}
	public void setTotal(Long total) {
		this.total = total;
	}
	public List<?> getRows() {
		return rows;
	}
	public void setRows(List<?> rows) {
		this.rows = rows;
	}	
}
	/*
	 *加载客户列表
	 */
	@RequestMapping("loadAllCar")
	@ResponseBody
	private DataGridView loadAllCar(CarVo carVo){
		return carService.queryAllCar(carVo);
	}