示例下载地址:http://download.csdn.net/detail/geloin/4506640
本文基于Spring 注解,让Spring跑起来。本文使用Mysql数据库。
(1) 导入相关包,包结构如下图所示:
(2) 修改src/applicationContext.xml文件,结果如下所示:
(3) 在src下添加jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ruisystem
username=root
password=root
/**
*
* @author geloin
* @date 2012-5-5 上午10:24:43
*/
package com.geloin.spring.entity;
/**
*
* @author geloin
* @date 2012-5-5 上午10:24:43
*/
public class Menu {
/**
* 惟一标识
*/
private Integer id;
/**
* 父ID
*/
private Integer parentId;
/**
* 名称
*/
private String name;
/**
* 对应的地址
*/
private String url;
/**
* 是否显示在左侧
*/
private Integer isShowLeft;
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @return the id
*/
public Integer getId() {
return id;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @param id
* the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @return the parentId
*/
public Integer getParentId() {
return parentId;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @param parentId
* the parentId to set
*/
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @return the name
*/
public String getName() {
return name;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @return the url
*/
public String getUrl() {
return url;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @return the isShowLeft
*/
public Integer getIsShowLeft() {
return isShowLeft;
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:19
* @param isShowLeft
* the isShowLeft to set
*/
public void setIsShowLeft(Integer isShowLeft) {
this.isShowLeft = isShowLeft;
}
}
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:34
*/
package com.geloin.spring.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.geloin.spring.entity.Menu;
/**
*
* @author geloin
* @date 2012-5-5 上午10:26:34
*/
@Repository(value = "menuMapper")
public interface MenuMapper {
@Select(value = "${sql}")
@Results(value = { @Result(id = true, property = "id", column = "id"),
@Result(property = "parentId", column = "c_parent_id"),
@Result(property = "url", column = "c_url"),
@Result(property = "isShowLeft", column = "c_is_show_left"),
@Result(property = "name", column = "c_name") })
List
(6) 在com.geloin.spring.service中添加MenuService接口
/**
*
* @author geloin
* @date 2012-5-5 上午10:28:42
*/
package com.geloin.spring.service;
import java.util.List;
import com.geloin.spring.entity.Menu;
/**
*
* @author geloin
* @date 2012-5-5 上午10:28:42
*/
public interface MenuService {
/**
* 查询所有
*
* @author geloin
* @date 2012-5-5 上午10:28:55
* @return
*/
List
/**
*
* @author geloin
* @date 2012-5-5 上午10:29:22
*/
package com.geloin.spring.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.geloin.spring.entity.Menu;
import com.geloin.spring.mapper.MenuMapper;
import com.geloin.spring.service.MenuService;
/**
*
* @author geloin
* @date 2012-5-5 上午10:29:22
*/
@Repository(value = "menuService")
@Transactional
public class MenuServiceImpl implements MenuService {
@Resource(name = "menuMapper")
private MenuMapper menuMapper;
/*
* (non-Javadoc)
*
* @see com.geloin.spring.service.MenuService#find()
*/
@Override
public List
(8) 修改控制器LoginController
/**
*
* @author geloin
* @date 2012-5-5 上午9:31:52
*/
package com.geloin.spring.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.geloin.spring.entity.Menu;
import com.geloin.spring.service.MenuService;
/**
*
* @author geloin
* @date 2012-5-5 上午9:31:52
*/
@Controller
@RequestMapping(value = "background")
public class LoginController {
@Resource(name = "menuService")
private MenuService menuService;
/**
*
*
* @author geloin
* @date 2012-5-5 上午9:33:22
* @return
*/
@RequestMapping(value = "to_login")
public ModelAndView toLogin(HttpServletResponse response) throws Exception {
Map map = new HashMap();
List
(9) 编写/WEB-INF/pages/background/menu.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Insert title here
${item.id }--${item.name }--${item.parentId }--${item.url }--${item.isShowLeft }