演示样例下载地址: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) 显示结果

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

Spring 整合Mybatis实例的更多相关文章

  1. Spring学习总结(六)——Spring整合MyBatis完整示例

    为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...

  2. Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二

    接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...

  3. 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题

    https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...

  4. Spring整合MyBatis(三)sqlSessionFactory创建

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.SqlSessionFactoryBean的初始化 二.获取 ...

  5. Spring整合MyBatis(一)MyBatis独立使用

    摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. MyBatis本是Apache的一个开源项目iBatis,2010年这 ...

  6. Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)

    http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...

  7. spring整合mybatis详解

    在上篇螃蟹已经说明spring注解的最经典配置,接下来开始整合mybatis,这样整个项目就相对完整了. 有关本实例的源码可以到 <spring MVC注解实例及说明文档> 下载. 如需转 ...

  8. spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现

    知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...

  9. 深入源码理解Spring整合MyBatis原理

    写在前面 聊一聊MyBatis的核心概念.Spring相关的核心内容,主要结合源码理解Spring是如何整合MyBatis的.(结合右侧目录了解吧) MyBatis相关核心概念粗略回顾 SqlSess ...

随机推荐

  1. phpstudy集成下Apache配置部署https安全证书

    一..先申请到安全证书.(腾讯云或者阿里云申请免费1年的安全证书),怎么申请这里也说下(以腾讯云为例): 1.登录腾讯云QQ或微信登录都行,第一次登录要通过实名认证,点击[产品]---[ss证书l] ...

  2. Nginx控制并发连接数

    ngx_http_limit_conn_module这个模块用于限制每个定义的key值的连接数,特别是单IP的连接数. 不是所有的连接数都会被计数.一个符合计数要求的连接是整个请求头已经被读取的连接. ...

  3. 排错-windows下 ORA-12560 TNS 协议适配器错误解决方法

    排错-windows下_ORA-12560 TNS 协议适配器错误解决方法 by:授客 QQ:1033553122 问题描述: 修改SQL*Plus窗口属性后,重新打开SQL*Plus时出现ORA-1 ...

  4. 如何获取listview里面的edittext或者RadioGroup的值,涉及到引发的混乱现象

    最近要实现从数据库读数据,该数据对应listview的item布局里面的RadioButton值,并且item布局里面还有EditText的控件. 如何将每一条对应的listview对应值获取出来呢? ...

  5. 学习vue.js的正确姿势(转载)

    最近饶有兴致的又把最新版 Vue.js 的源码学习了一下,觉得真心不错,个人觉得 Vue.js 的代码非常之优雅而且精辟,作者本身可能无 (bu) 意 (xie) 提及这些.那么,就让我来吧:) 程序 ...

  6. Nginx服务器报 "Too Many Open Files"

    近日服务器上的运行的一个站点经常性出现500错误.查了下服务器负载,负载正常.而后查询了下nginx记录的站点运行错误日志,发现提示Too many open files.因为站点静态文件居多,而且h ...

  7. windows10如何打开vhd文件

    本人电脑安装了Visual Studio 2017,但是由于项目需求需要Core SDK(2.0)的版本支持,也就是2017最新版.所以现在需要利用visual Studio 2017最新版本的安装包 ...

  8. 更改 Windows VM 的可用性集

    以下步骤说明如何使用 Azure PowerShell 来更改 VM 的可用性集. 只能在创建 VM 时将 VM 添加到可用性集. 如果要更改可用性集,必须将虚拟机删除,并重新创建虚拟机. 使用 Po ...

  9. web导出excel文件的几种方法

    总的来说,两种方法:服务器端生成和浏览器端生成. 服务器端生成就是:根据用户请求,获取相应的数据,使用poi/jxl, jacob/jawin+excel,或是用数据拼html的table或是cvs纯 ...

  10. fab提供远程IP和账号密码

    #!/usr/bin/python #-*- coding: UTF-8 -*- from fabric.api import * from fabric.context_managers impor ...