电商项目开发7
菜单管理
MenuDao.xml
<?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">
<!-- 命名空間,xml文件和dao接口对应起來 -->
<mapper namespace="com.zq.dao.MenuDao">
<resultMap type="com.zq.model.MenuCondition" id="menuMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="icon" column="icon"/>
<result property="sort" column="sort"/>
<result property="url" column="url"/>
</resultMap>
<select id="findById" parameterType="integer" resultMap="menuMap">
select * from menu where id = #{id}
</select>
<sql id="sqlWhere">
<where><!-- 这种写法会自动去掉第一个and -->
<if test="id!=null">
and id = #{id}
</if>
<if test="name!=null and name!=''">
and name = #{name}
</if>
<if test="icon!=null and icon!=''">
and icon = #{icon}
</if>
<if test="sort!=null">
and sort = #{sort}
</if>
<if test="url!=null and url!=''">
and url = #{url}
</if>
</where>
</sql>
<!-- 查询列表 -->
<select id="list" parameterType="MenuCondition" resultMap="menuMap">
select * from menu
<include refid="sqlWhere" />
</select>
<!-- id不需要,自增 -->
<insert id="create" parameterType="MenuCondition">
insert into
menu(
id , name , icon , sort , url )
values(
#{id} , #{name} , #{icon} , #{sort} , #{url} )
</insert>
<update id="update" parameterType="MenuCondition">
update menu
<set>
<if test="id!=null">
id = #{id},
</if>
<if test="name!=null and name!=''">
name = #{name},
</if>
<if test="icon!=null and icon!=''">
icon = #{icon},
</if>
<if test="sort!=null">
sort = #{sort},
</if>
<if test="url!=null and url!=''">
url = #{url},
</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="integer">
delete from menu where id = #{id}
</delete>
</mapper>
MenuDao.java
<?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">
<!-- 命名空間,xml文件和dao接口对应起來 -->
<mapper namespace="com.zq.dao.MenuDao">
<resultMap type="com.zq.model.MenuCondition" id="menuMap">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="icon" column="icon"/>
<result property="sort" column="sort"/>
<result property="url" column="url"/>
</resultMap>
<select id="findById" parameterType="integer" resultMap="menuMap">
select * from menu where id = #{id}
</select>
<sql id="sqlWhere">
<where><!-- 这种写法会自动去掉第一个and -->
<if test="id!=null">
and id = #{id}
</if>
<if test="name!=null and name!=''">
and name = #{name}
</if>
<if test="icon!=null and icon!=''">
and icon = #{icon}
</if>
<if test="sort!=null">
and sort = #{sort}
</if>
<if test="url!=null and url!=''">
and url = #{url}
</if>
</where>
</sql>
<!-- 查询列表 -->
<select id="list" parameterType="MenuCondition" resultMap="menuMap">
select * from menu
<include refid="sqlWhere" />
</select>
<!-- id不需要,自增 -->
<insert id="create" parameterType="MenuCondition">
insert into
menu(
id , name , icon , sort , url )
values(
#{id} , #{name} , #{icon} , #{sort} , #{url} )
</insert>
<update id="update" parameterType="MenuCondition">
update menu
<set>
<if test="id!=null">
id = #{id},
</if>
<if test="name!=null and name!=''">
name = #{name},
</if>
<if test="icon!=null and icon!=''">
icon = #{icon},
</if>
<if test="sort!=null">
sort = #{sort},
</if>
<if test="url!=null and url!=''">
url = #{url},
</if>
</set>
where id = #{id}
</update>
<delete id="delete" parameterType="integer">
delete from menu where id = #{id}
</delete>
</mapper>
MenuController.java
package com.zq.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 org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;
import com.zq.model.MenuCondition;//MenuCondition
import com.zq.service.MenuService;//Menu
@RequestMapping("/menu")
@Controller
public class MenuController {
@Autowired
private MenuService menuService;
@ResponseBody
@RequestMapping("/delete.do")
public boolean delete(Integer id){
try{
menuService.delete(id);
}catch(Exception e){
System.out.println(e.getMessage());
return false;
}
return true;
}
@ResponseBody
@RequestMapping("/findById.do")
public MenuCondition findById(Integer id){
return menuService.findById(id);
}
@ResponseBody
@RequestMapping("/create.do")
public boolean create(MenuCondition menu){
try{
menuService.create(menu);
}catch(Exception e){
System.out.println(e.getMessage());
return false;
}
return true;
}
@RequestMapping("/list.do")
public String list(MenuCondition menu,Model model,
@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
@RequestParam(required=true,value="pageSize",defaultValue="3") Integer pageSize
){
PageInfo<MenuCondition> menus = menuService.list(pageNum,pageSize,menu);
model.addAttribute("pageInfo", menus);
List<MenuCondition> menuList = menuService.list(null);
model.addAttribute("menuList", menuList);
return "menu";
}
@ResponseBody
@RequestMapping("/listData.do")
public List<MenuCondition> list(MenuCondition menu,Model model){
return menuService.list(menu);
}
}