电商项目开发7

电商项目开发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);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值