Spring3.05+SpringMVC+myBatis3.26整合开发
一.Student为实体对象,有sno,sname,age三个属性;
二.:模糊查询后台代码:
1.mapper层(DAO层)
StudentMapper.java类
List<Student> fuzzySearch(@Param("text")String text);
1> @Param("text")为别名;
2>导入Param包:import org.apache.ibatis.annotations.Param;
2.Student.xml文件
<sql id="Base_Column_List>
sno,sname,age
</sql>
<select id="fuzzySearch" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from dlsoftdb_NBG.dbo.students
where sname like '%${text}%'
</select>
3.service层
接口:studentService.java:
public List<Student> fuzzySearch(String sname);
实现:StuedntServiceImpl.java:
@Service("studentService")
@Transactional
public List<Student> fuzzySearch(String sname){
return studentMapper.searchStudentList(sname);
}
4.Controller层
studentController.java
@Controller
@RequestMapping("student")
@RequestMapping({"/fuzzySearch"})
@ResponseBody
public String searchStudnetList(HttpServletRequest request){
List<Student> stu=this.studentService.fuzzySearch(request.getParameter("sname"));
JSONObject jsonObjects=new JSONObject();
jsonObjects.put("Rows",stu);
String message=jsonObjects.toString();
return message;
}
三、模糊查询前台代码:
ligerUI插件
1.js代码:
function fuzzySearch(){
var text=$("name").val();
$.ajax({
url:'/项目名称/student/fuzzySearch',
type:'post',
dataType:'json',
data:{sname:text},
cache:false,
success:fucntion(msg){
findStduent(msg);
}
});
function findStudent(data){
$("#maingrid").ligerGrid({
checkbox:true,
columns:[
{ display:'学号',
name:'sno',
width:80,
type:'int',
editor:{
type:'text'
}
},
{ display:'用户名称',
name:'sname',
width:200,
editor:{
type:'text'
}
},
{ display:'年龄',
name:'age',
width:150,
editor:{
type:'text'
}
}
],
enbledEdit:true,//是否允许编辑
clickToEdit:false,//单元格编辑状态
isScroll:true,//设置为false时将不会显示滚动条,高度自适应
data:data,//数据来源
enabledSort:true,//是否允许排序
fixedCellHeight:true,//是否固定单元格的高度
width:'70%',
height:'400px'
});
}
2.<div>
<input id="name" type="text">
<input type="button" value="查询" οnclick="fuzzySearch()">
</div>