示例下載地址:http://download.csdn.net/detail/geloin/4506640
本文基于Spring 注解,讓Spring跑起來(lái)。本文使用Mysql數(shù)據(jù)庫(kù)。
(1) 導(dǎo)入相關(guān)包,包結(jié)構(gòu)如下圖所示:

(2) 修改src/applicationContext.xml文件,結(jié)果如下所示:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www./schema/beans"
- xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:p="http://www./schema/p"
- xmlns:context="http://www./schema/context"
- xmlns:tx="http://www./schema/tx"
- xsi:schemaLocation="
- http:
- http:
- http:
- http:
- http:
- http:
-
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
-
- <!--創(chuàng)建jdbc數(shù)據(jù)源 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver}" />
- <property name="url" value="${url}" />
- <property name="username" value="${username}" />
- <property name="password" value="${password}" />
- </bean>
-
- <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
-
- <!-- 創(chuàng)建SqlSessionFactory,同時(shí)指定數(shù)據(jù)源 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
-
- <!-- 可通過(guò)注解控制事務(wù) -->
- <tx:annotation-driven />
-
- <!-- Mapper接口所在包名,Spring會(huì)自動(dòng)查找其下的Mapper -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.geloin.spring.mapper" />
- </bean>
-
- </beans>
(3) 在src下添加jdbc.properties
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql:
- username=root
- password=root
(4) 在com.geloin.spring.entity包下添加實(shí)體類,實(shí)體類對(duì)應(yīng)于數(shù)據(jù)表,其屬性與數(shù)據(jù)表相同或多于數(shù)據(jù)表。
(5) 在com.geloin.spring.mapper下添加實(shí)體類與數(shù)據(jù)表的映射關(guān)系(com.geloin.spring.mapper與applicationContext.xml中的配置一致)。
-
-
-
-
-
- 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;
-
-
-
-
-
-
- @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<Menu> operateReturnBeans(@Param(value = "sql") String sql);
- }
其中,@Repository表示這是一個(gè)被Spring管理的資源,資源名稱為menuMapper;@Select表示operateReturnBeans方法為一個(gè)select方法;@Results表示返回結(jié)果,@Result將返回結(jié)果中的字段名與實(shí)體類關(guān)聯(lián);@Param表示String sql這個(gè)變量是用于Mybatis的一個(gè)變量,其名稱為sql(value值),該變量在@Select中調(diào)用(通過(guò)${sql}調(diào)用)。
(6) 在com.geloin.spring.service中添加MenuService接口
-
-
-
-
-
- package com.geloin.spring.service;
-
- import java.util.List;
-
- import com.geloin.spring.entity.Menu;
-
-
-
-
-
-
- public interface MenuService {
-
-
-
-
-
-
-
- List<Menu> find();
- }
(7) 在com.geloin.spring.service.impl中添加MenuServiceImpl作為MenuService接口的實(shí)現(xiàn)
-
-
-
-
-
- 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;
-
-
-
-
-
-
- @Repository(value = "menuService")
- @Transactional
- public class MenuServiceImpl implements MenuService {
-
- @Resource(name = "menuMapper")
- private MenuMapper menuMapper;
-
-
-
-
-
-
- @Override
- public List<Menu> find() {
- String sql = "select * from tb_system_menu";
- return this.menuMapper.operateReturnBeans(sql);
- }
-
- }
其中,@Transactional表示該類被Spring作為管理事務(wù)的類,@Resource引入一個(gè)Spring定義的資源,資源名為menuMapper(name值),即為第七步定義的映射類。
(8) 修改控制器LoginController
-
-
-
-
-
- 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;
-
-
-
-
-
-
- @Controller
- @RequestMapping(value = "background")
- public class LoginController {
-
- @Resource(name = "menuService")
- private MenuService menuService;
-
-
-
-
-
-
-
-
- @RequestMapping(value = "to_login")
- public ModelAndView toLogin(HttpServletResponse response) throws Exception {
-
- Map<String, Object> map = new HashMap<String, Object>();
-
- List<Menu> result = this.menuService.find();
-
- map.put("result", result);
-
- return new ModelAndView("background/menu", map);
- }
- }
通過(guò)map將從數(shù)據(jù)庫(kù)中獲取的值傳遞到j(luò)sp頁(yè)面,"background/menu"值經(jīng)context-dispatcher.xml轉(zhuǎn)化后,變?yōu)?WEB-INF/pages/background/menu.jsp,即,方法toLogin的含義為:從數(shù)據(jù)庫(kù)中獲取菜單信息,然后將之存儲(chǔ)到map中,通過(guò)map把菜單列表傳遞到/WEB-INF/pages/background/menu.jsp頁(yè)面用于顯示。
(9) 編寫/WEB-INF/pages/background/menu.jsp頁(yè)面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java./jsp/jstl/core"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Insert title here</title>
- </head>
- <body>
- <c:forEach items="${result }" var="item">
- ${item.id }--${item.name }--${item.parentId }--${item.url }--${item.isShowLeft }<br />
- </c:forEach>
- </body>
- </html>
(10) 顯示結(jié)果

|