这次把批量删除写上了,也完善了非空判断
这是添加页面,物资名称下拉框是动态的!
这是Dao层,因为批量删除获取的id不止单个,所以使用list[]接收jsp页面参数,查询所有方法中的page和limit是页码和条数,是用来进行分页功能的。
public interface t_orderDao {
// 查询所有
public List<t_order> findall(@Param("page")Integer page,@Param("limit")Integer limit,@Param("material")String material,@Param("type")Integer type);
// 根据物资名称和状态查询
public Integer findbywz(@Param("material")String material,@Param("type")Integer type);
// 新增
public Integer add(t_order tt);
// 修改
public Integer update(t_order tt);
// 删除(修改)
public Integer delete(Integer oid);
// 查询物资
public List<t_material> wuz();
// 批量删除
public Integer delsOrder(@Param("oids")Integer[] oids);
}
这是Mapper.xml,实现Dao层的方法。这次数据库中是有两张表,查询所有的sql语句是两表联查。在Dao层方法中添加@Param注解后就不需要在<select><insert><update><delete>元素中添加parameterType属性。
在批量删除方法delsOrder中,foreach的collection,item属性要和jsp前端页面传入的id参数一致,#{oids}也是如此。
<if test="material != null and material!=''">这里的material不是数据库中的字段,要和前端输入框中的name名一致
<mapper namespace="com.yzd.dao.t_orderDao">
<!-- 查询所有 -->
<select id="findall" resultType="com.yzd.entity.t_order">
select t_order.oid,t_material.mid,t_material.material,t_order.num,t_order.ordertime,t_order.type,t_order.remark,t_order.delete_flag
from t_material inner join t_order on t_material.mid=t_order.mid
<where>
<if test="material != null and material!=''">
and `material` like concat('%',#{material},'%')
</if>
<if test="type != null and type!=''">
and `type` = #{type}
</if>
</where>
and t_order.delete_flag=0
order by oid
limit #{page},#{limit}
</select>
<!-- 查询物资和状态 -->
<select id="findbywz" resultType="Integer">
<!-- select t_order.oid,t_material.material,t_order.num,t_order.ordertime,t_order.type,t_order.remark
from t_material,t_order
where t_material.mid=t_order.mid and t_material.material like '%'${material}'%' and t_order.type=#{type}
-->
select count(*) from t_material,t_order
<where>
t_material.mid=t_order.mid
<if test="material != null and material!=''">
and `material` like concat('%',#{material},'%')
</if>
<if test="type != null and type!=''">
and `type` = #{type}
</if>
</where>
and delete_flag=0
</select>
<!-- 添加 -->
<insert id="add" parameterType="com.yzd.entity.t_order">
insert into t_order(mid,num,ordertime,type,remark,delete_flag)
values(#{mid},#{num},now(),#{type},#{remark},default)
</insert>
<!-- 修改 -->
<update id="update" parameterType="com.yzd.entity.t_order">
update t_order set num=#{num},ordertime=now(),type=#{type},remark=#{remark} where oid=#{oid}
</update>
<!-- 删除 -->
<update id="delete" parameterType="com.yzd.entity.t_order">
update t_order set delete_flag=1 where oid=#{oid}
</update>
<!-- 查询物资 -->
<select id="wuz" resultType="com.yzd.entity.t_material">
select * from t_material
</select>
<!-- 批量删除 -->
<update id="delsOrder">
update t_order set delete_flag=1 where oid in
<foreach collection="oids" item="oids" separator="," open="(" close=")">
#{oids}
</foreach>
</update>
</mapper>
这是Service层,和Dao层类似。
public interface t_orderService {
// 查询所有
public List<t_order> findall(Integer page,Integer limit,String material,Integer type);
// 根据物资名称和状态查询
public Integer findbywz(String material,Integer type);
// 添加
public Integer add(t_order tt);
// 修改
public Integer update(t_order tt);
// 删除
public Integer delete(Integer oid);
// 查询物资
public List<t_material> wuz();
// 批量删除
Integer delsOrder(Integer[] oids);
}
这是ServiceImpl实现层,@Service和@Autowired注解不能忘记写!这个也没什么可以讲的,就是调用Dao层,实现Dao接口中的方法