1.Dao层方法
package com.tcc.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.tcc.bean.student;
import com.tcc.bean.type;
public interface usermapper {
//此处用到了注解@param它可以用来拼接到sql中
public List<student> list(@Param("sname")String sname);
public void delete(Integer id);
public student inli(student st);
}
2.Dao层实现类
<?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.tcc.dao.usermapper">
<select id="list" parameterType="student" resultMap="stu">
select * from t_student s,t_class c where s.cid=c.id
<if test="sname!=null">
and s.name like '%${sname}%' //判断出条件 拼接sql
</if>
</select>
<delete id="delete">
delete from t_student where id=#{id}
</delete>
</mapper>
3.Controller
package com.tcc.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tcc.bean.student;
import com.tcc.bean.type;
import com.tcc.service.userservice;
@Controller
@RequestMapping("user")
public class controller {
@Autowired
private userservice us;
@RequestMapping("list")
public String list(String sname,Model model){
List<student> list = us.list(sname);
model.addAttribute("list", list);
return "success";
}
@RequestMapping("delete")
//批量删除会有多个id这里用String[] ids来接收
public String delete(String[] ids){
//循环遍历让每一个id都执行删除方法
for (String string : ids) {
us.delete(Integer.parseInt(string));
}
return "redirect:/user/list";
}
}
3.jsp-jquery
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$(".qx").click(function () {
$("input[type=checkbox]:gt(0)").prop("checked",this.checked);
})//全选反选
$("#全选标签id").click(function(){
var array=new Array;
$("input[type=checkbox]:gt(0):checked").each(function(){
array.push($(this).parent().next().text());
});
alert(array);
location.href="${pageContext.request.contextPath}/user/delete?ids="+array;
});
});
</script>
<form action="****.action">//调用查询方法,把sname传到后台
<input type="text" name="sname">//sanme就是@param里面所写的参数
<input type="submit" value="查询">
</form>
package com.tcc.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.tcc.bean.student;
import com.tcc.bean.type;
public interface usermapper {
//此处用到了注解@param它可以用来拼接到sql中
public List<student> list(@Param("sname")String sname);
public void delete(Integer id);
public student inli(student st);
}
2.Dao层实现类
<?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.tcc.dao.usermapper">
<select id="list" parameterType="student" resultMap="stu">
select * from t_student s,t_class c where s.cid=c.id
<if test="sname!=null">
and s.name like '%${sname}%' //判断出条件 拼接sql
</if>
</select>
<delete id="delete">
delete from t_student where id=#{id}
</delete>
</mapper>
3.Controller
package com.tcc.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tcc.bean.student;
import com.tcc.bean.type;
import com.tcc.service.userservice;
@Controller
@RequestMapping("user")
public class controller {
@Autowired
private userservice us;
@RequestMapping("list")
public String list(String sname,Model model){
List<student> list = us.list(sname);
model.addAttribute("list", list);
return "success";
}
@RequestMapping("delete")
//批量删除会有多个id这里用String[] ids来接收
public String delete(String[] ids){
//循环遍历让每一个id都执行删除方法
for (String string : ids) {
us.delete(Integer.parseInt(string));
}
return "redirect:/user/list";
}
}
3.jsp-jquery
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(function(){
$(".qx").click(function () {
$("input[type=checkbox]:gt(0)").prop("checked",this.checked);
})//全选反选
$("#全选标签id").click(function(){
var array=new Array;
$("input[type=checkbox]:gt(0):checked").each(function(){
array.push($(this).parent().next().text());
});
alert(array);
location.href="${pageContext.request.contextPath}/user/delete?ids="+array;
});
});
</script>
<form action="****.action">//调用查询方法,把sname传到后台
<input type="text" name="sname">//sanme就是@param里面所写的参数
<input type="submit" value="查询">
</form>