演示样例下载地址:http://download.csdn.net/detail/geloin/4506640

本文基于Spring 注解,让Spring跑起来。本文使用Mysql数据库。

(1) 导入相关包,包结构例如以下图所看到的:

(2) 改动src/applicationContext.xml文件,结果例如以下所看到的:

[java] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  13. <!-- 引入jdbc配置文件 -->
  14. <context:property-placeholder location="classpath:jdbc.properties" />
  15. <!--创建jdbc数据源 -->
  16. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  17. destroy-method="close">
  18. <property name="driverClassName" value="${driver}" />
  19. <property name="url" value="${url}" />
  20. <property name="username" value="${username}" />
  21. <property name="password" value="${password}" />
  22. </bean>
  23. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  24. <bean id="transactionManager"
  25. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  26. <property name="dataSource" ref="dataSource" />
  27. </bean>
  28. <!-- 创建SqlSessionFactory。同一时候指定数据源 -->
  29. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <property name="dataSource" ref="dataSource" />
  31. </bean>
  32. <!-- 可通过注解控制事务 -->
  33. <tx:annotation-driven />
  34. <!-- Mapper接口所在包名。Spring会自己主动查找其下的Mapper -->
  35. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  36. <property name="basePackage" value="com.geloin.spring.mapper" />
  37. </bean>
  38. </beans>

(3) 在src下加入jdbc.properties

[java] view
plain
copy

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/ruisystem
  3. username=root
  4. password=root

(4) 在com.geloin.spring.entity包下加入实体类。实体类相应于数据表,其属性与数据表同样或多于数据表。

[java] view
plain
copy

  1. /**
  2. *
  3. * @author geloin
  4. * @date 2012-5-5 上午10:24:43
  5. */
  6. package com.geloin.spring.entity;
  7. /**
  8. *
  9. * @author geloin
  10. * @date 2012-5-5 上午10:24:43
  11. */
  12. public class Menu {
  13. /**
  14. * 惟一标识
  15. */
  16. private Integer id;
  17. /**
  18. * 父ID
  19. */
  20. private Integer parentId;
  21. /**
  22. * 名称
  23. */
  24. private String name;
  25. /**
  26. * 相应的地址
  27. */
  28. private String url;
  29. /**
  30. * 是否显示在左側
  31. */
  32. private Integer isShowLeft;
  33. /**
  34. *
  35. * @author geloin
  36. * @date 2012-5-5 上午10:26:19
  37. * @return the id
  38. */
  39. public Integer getId() {
  40. return id;
  41. }
  42. /**
  43. *
  44. * @author geloin
  45. * @date 2012-5-5 上午10:26:19
  46. * @param id
  47. *            the id to set
  48. */
  49. public void setId(Integer id) {
  50. this.id = id;
  51. }
  52. /**
  53. *
  54. * @author geloin
  55. * @date 2012-5-5 上午10:26:19
  56. * @return the parentId
  57. */
  58. public Integer getParentId() {
  59. return parentId;
  60. }
  61. /**
  62. *
  63. * @author geloin
  64. * @date 2012-5-5 上午10:26:19
  65. * @param parentId
  66. *            the parentId to set
  67. */
  68. public void setParentId(Integer parentId) {
  69. this.parentId = parentId;
  70. }
  71. /**
  72. *
  73. * @author geloin
  74. * @date 2012-5-5 上午10:26:19
  75. * @return the name
  76. */
  77. public String getName() {
  78. return name;
  79. }
  80. /**
  81. *
  82. * @author geloin
  83. * @date 2012-5-5 上午10:26:19
  84. * @param name
  85. *            the name to set
  86. */
  87. public void setName(String name) {
  88. this.name = name;
  89. }
  90. /**
  91. *
  92. * @author geloin
  93. * @date 2012-5-5 上午10:26:19
  94. * @return the url
  95. */
  96. public String getUrl() {
  97. return url;
  98. }
  99. /**
  100. *
  101. * @author geloin
  102. * @date 2012-5-5 上午10:26:19
  103. * @param url
  104. *            the url to set
  105. */
  106. public void setUrl(String url) {
  107. this.url = url;
  108. }
  109. /**
  110. *
  111. * @author geloin
  112. * @date 2012-5-5 上午10:26:19
  113. * @return the isShowLeft
  114. */
  115. public Integer getIsShowLeft() {
  116. return isShowLeft;
  117. }
  118. /**
  119. *
  120. * @author geloin
  121. * @date 2012-5-5 上午10:26:19
  122. * @param isShowLeft
  123. *            the isShowLeft to set
  124. */
  125. public void setIsShowLeft(Integer isShowLeft) {
  126. this.isShowLeft = isShowLeft;
  127. }
  128. }

(5) 在com.geloin.spring.mapper下加入实体类与数据表的映射关系(com.geloin.spring.mapper与applicationContext.xml中的配置一致)。

[java] view
plain
copy

  1. /**
  2. *
  3. * @author geloin
  4. * @date 2012-5-5 上午10:26:34
  5. */
  6. package com.geloin.spring.mapper;
  7. import java.util.List;
  8. import org.apache.ibatis.annotations.Param;
  9. import org.apache.ibatis.annotations.Result;
  10. import org.apache.ibatis.annotations.Results;
  11. import org.apache.ibatis.annotations.Select;
  12. import org.springframework.stereotype.Repository;
  13. import com.geloin.spring.entity.Menu;
  14. /**
  15. *
  16. * @author geloin
  17. * @date 2012-5-5 上午10:26:34
  18. */
  19. @Repository(value = "menuMapper")
  20. public interface MenuMapper {
  21. @Select(value = "${sql}")
  22. @Results(value = { @Result(id = true, property = "id", column = "id"),
  23. @Result(property = "parentId", column = "c_parent_id"),
  24. @Result(property = "url", column = "c_url"),
  25. @Result(property = "isShowLeft", column = "c_is_show_left"),
  26. @Result(property = "name", column = "c_name") })
  27. List<Menu> operateReturnBeans(@Param(value = "sql") String sql);
  28. }

当中。@Repository表示这是一个被Spring管理的资源,资源名称为menuMapper;@Select表示operateReturnBeans方法为一个select方法;@Results表示返回结果。@Result将返回结果中的字段名与实体类关联;@Param表示String sql这个变量是用于Mybatis的一个变量。其名称为sql(value值)。该变量在@Select中调用(通过${sql}调用)。

(6) 在com.geloin.spring.service中加入MenuService接口

[java] view
plain
copy

  1. /**
  2. *
  3. * @author geloin
  4. * @date 2012-5-5 上午10:28:42
  5. */
  6. package com.geloin.spring.service;
  7. import java.util.List;
  8. import com.geloin.spring.entity.Menu;
  9. /**
  10. *
  11. * @author geloin
  12. * @date 2012-5-5 上午10:28:42
  13. */
  14. public interface MenuService {
  15. /**
  16. * 查询全部
  17. *
  18. * @author geloin
  19. * @date 2012-5-5 上午10:28:55
  20. * @return
  21. */
  22. List<Menu> find();
  23. }

(7) 在com.geloin.spring.service.impl中加入MenuServiceImpl作为MenuService接口的实现

[java] view
plain
copy

  1. /**
  2. *
  3. * @author geloin
  4. * @date 2012-5-5 上午10:29:22
  5. */
  6. package com.geloin.spring.service.impl;
  7. import java.util.List;
  8. import javax.annotation.Resource;
  9. import org.springframework.stereotype.Repository;
  10. import org.springframework.transaction.annotation.Transactional;
  11. import com.geloin.spring.entity.Menu;
  12. import com.geloin.spring.mapper.MenuMapper;
  13. import com.geloin.spring.service.MenuService;
  14. /**
  15. *
  16. * @author geloin
  17. * @date 2012-5-5 上午10:29:22
  18. */
  19. @Repository(value = "menuService")
  20. @Transactional
  21. public class MenuServiceImpl implements MenuService {
  22. @Resource(name = "menuMapper")
  23. private MenuMapper menuMapper;
  24. /*
  25. * (non-Javadoc)
  26. *
  27. * @see com.geloin.spring.service.MenuService#find()
  28. */
  29. @Override
  30. public List<Menu> find() {
  31. String sql = "select * from tb_system_menu";
  32. return this.menuMapper.operateReturnBeans(sql);
  33. }
  34. }

当中,@Transactional表示该类被Spring作为管理事务的类,@Resource引入一个Spring定义的资源,资源名为menuMapper(name值),即为第七步定义的映射类。

(8) 改动控制器LoginController

[java] view
plain
copy

  1. /**
  2. *
  3. * @author geloin
  4. * @date 2012-5-5 上午9:31:52
  5. */
  6. package com.geloin.spring.controller;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import javax.annotation.Resource;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.servlet.ModelAndView;
  15. import com.geloin.spring.entity.Menu;
  16. import com.geloin.spring.service.MenuService;
  17. /**
  18. *
  19. * @author geloin
  20. * @date 2012-5-5 上午9:31:52
  21. */
  22. @Controller
  23. @RequestMapping(value = "background")
  24. public class LoginController {
  25. @Resource(name = "menuService")
  26. private MenuService menuService;
  27. /**
  28. *
  29. *
  30. * @author geloin
  31. * @date 2012-5-5 上午9:33:22
  32. * @return
  33. */
  34. @RequestMapping(value = "to_login")
  35. public ModelAndView toLogin(HttpServletResponse response) throws Exception {
  36. Map<String, Object> map = new HashMap<String, Object>();
  37. List<Menu> result = this.menuService.find();
  38. map.put("result", result);
  39. return new ModelAndView("background/menu", map);
  40. }
  41. }

通过map将从数据库中获取的值传递到jsp页面,"background/menu"值经context-dispatcher.xml转化后,变为/WEB-INF/pages/background/menu.jsp,即,方法toLogin的含义为:从数据库中获取菜单信息。然后将之存储到map中,通过map把菜单列表传递到/WEB-INF/pages/background/menu.jsp页面用于显示。

(9) 编写/WEB-INF/pages/background/menu.jsp页面

[java] view
plain
copy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <c:forEach items="${result }" var="item">
  12. ${item.id }--${item.name }--${item.parentId }--${item.url }--${item.isShowLeft }<br />
  13. </c:forEach>
  14. </body>
  15. </html>

(10) 显示结果

版权声明:本文为博主原创文章,未经博主同意不得转载。

最新文章

  1. 用Barcode生成条形码图片
  2. HDU 4801 Pocket Cube
  3. 关于Div的宽度与高度的100%设定
  4. cocos2d-x之加法计算器
  5. sharepoint 2010 在aspx 写lambda 时错误
  6. Java进阶代码
  7. c# 编程添加控件
  8. iOS之极光推送
  9. 如何在Eclipse中查看Android API源码以及support包源码
  10. qingshow “不积跬步无以至千里,不积小流无以成江海”。--荀子《劝学篇》 用tomcat+花生壳搭建自己的web服务器+域名(参考)
  11. 让Windows 8 / 8.1 以及 Windows Server 2012 / 2012 R2的桌面,显示我的电脑图标
  12. 简单的线性M移动平均
  13. 利用php数组函数进行函数式编程
  14. JQuery移动动画实现点击按钮切换图片--JQuery基础
  15. jquery传值与判断
  16. C语言数组操作和指针操作谁更高效
  17. (转)并发编程 – Concurrent 用户指南
  18. matplotlib笔记——legend用法
  19. Database Vault Administrator的使用
  20. tomcat架构分析(connector BIO 实现)

热门文章

  1. css3统一元素的宽和高
  2. VS 2015 报错 &quot; &#39;unistd.h&#39;: No such file or directory&quot; 的解决办法
  3. 防范跨站脚本攻击(XXS)的关键手段
  4. Docker相关概念
  5. LeetCode题解之Longest Continuous Increasing Subsequence
  6. Hibernate 批处理
  7. 如何用SQL脚本在SQL Server Replication中创建合并复制,以及怎么创建分区合并复制
  8. wc 统计文件的行数,字数,字符
  9. FZU Monthly-201903 获奖名单
  10. beta冲刺————第一天(1/5)