Spring 整合Mybatis实例
演示样例下载地址:http://download.csdn.net/detail/geloin/4506640
本文基于Spring 注解,让Spring跑起来。本文使用Mysql数据库。
(1) 导入相关包,包结构例如以下图所看到的:

(2) 改动src/applicationContext.xml文件,结果例如以下所看到的:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <!-- 引入jdbc配置文件 -->
- <context:property-placeholder location="classpath:jdbc.properties" />
- <!--创建jdbc数据源 -->
- <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>
- <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 创建SqlSessionFactory。同一时候指定数据源 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 可通过注解控制事务 -->
- <tx:annotation-driven />
- <!-- Mapper接口所在包名。Spring会自己主动查找其下的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://localhost:3306/ruisystem
- username=root
- password=root
(4) 在com.geloin.spring.entity包下加入实体类。实体类相应于数据表,其属性与数据表同样或多于数据表。
- /**
- *
- * @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;
- }
- }
(5) 在com.geloin.spring.mapper下加入实体类与数据表的映射关系(com.geloin.spring.mapper与applicationContext.xml中的配置一致)。
- /**
- *
- * @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<Menu> operateReturnBeans(@Param(value = "sql") String sql);
- }
当中。@Repository表示这是一个被Spring管理的资源,资源名称为menuMapper;@Select表示operateReturnBeans方法为一个select方法;@Results表示返回结果。@Result将返回结果中的字段名与实体类关联;@Param表示String sql这个变量是用于Mybatis的一个变量。其名称为sql(value值)。该变量在@Select中调用(通过${sql}调用)。
(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<Menu> find();
- }
(7) 在com.geloin.spring.service.impl中加入MenuServiceImpl作为MenuService接口的实现
- /**
- *
- * @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<Menu> find() {
- String sql = "select * from tb_system_menu";
- return this.menuMapper.operateReturnBeans(sql);
- }
- }
当中,@Transactional表示该类被Spring作为管理事务的类,@Resource引入一个Spring定义的资源,资源名为menuMapper(name值),即为第七步定义的映射类。
(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<String, Object> map = new HashMap<String, Object>();
- List<Menu> result = this.menuService.find();
- map.put("result", result);
- return new ModelAndView("background/menu", map);
- }
- }
通过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页面
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/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) 显示结果

版权声明:本文为博主原创文章,未经博主同意不得转载。
Spring 整合Mybatis实例的更多相关文章
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- 简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
https://blog.csdn.net/RicardoDing/article/details/79899686 近期,在使用spring和mybatis框架编写代码时,sqlSession不需要 ...
- Spring整合MyBatis(三)sqlSessionFactory创建
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 目录 一.SqlSessionFactoryBean的初始化 二.获取 ...
- Spring整合MyBatis(一)MyBatis独立使用
摘要: 本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. MyBatis本是Apache的一个开源项目iBatis,2010年这 ...
- Spring学习笔记:Spring整合Mybatis(mybatis-spring.jar)(二:mybatis整合spring)
http://blog.csdn.net/qq598535550/article/details/51703190 二.Spring整合mybatis其实是在mybatis的基础上实现Spring框架 ...
- spring整合mybatis详解
在上篇螃蟹已经说明spring注解的最经典配置,接下来开始整合mybatis,这样整个项目就相对完整了. 有关本实例的源码可以到 <spring MVC注解实例及说明文档> 下载. 如需转 ...
- spring基础:什么是框架,框架优势,spring优势,耦合内聚,什么是Ioc,IOC配置,set注入,第三方资源配置,综合案例spring整合mybatis实现
知识点梳理 课堂讲义 1)Spring简介 1.1)什么是框架 源自于建筑学,隶属土木工程,后发展到软件工程领域 软件工程中框架的特点: 经过验证 具有一定功能 半成品 1.2)框架的优势 提高开发效 ...
- 深入源码理解Spring整合MyBatis原理
写在前面 聊一聊MyBatis的核心概念.Spring相关的核心内容,主要结合源码理解Spring是如何整合MyBatis的.(结合右侧目录了解吧) MyBatis相关核心概念粗略回顾 SqlSess ...
随机推荐
- 【代码笔记】iOS-屏幕根据键盘自动的变化高度
一,效果图. 二,代码. ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewContro ...
- CSS文字超出指定长度,用省略号
overflow:hidden; text-overflow:ellipsis; white-space:nowrap; display: block;
- 图像增强算法(直方图均衡化、拉普拉斯、Log、伽马变换)
一.图像增强算法原理 图像增强算法常见于对图像的亮度.对比度.饱和度.色调等进行调节,增加其清晰度,减少噪点等.图像增强往往经过多个算法的组合,完成上述功能,比如图像去燥等同于低通滤波器,增加清晰度则 ...
- IT的2017,面临数字生态系统新挑战,该怎么办?
所谓数字生态系统,就是包含一系列基于标准,规模可变的硬件.软件.数字设备和服务,可系统地实现企业信息数字化,数据流通,以帮助企业提高运营效率. 随着越来越多的中国企业加入数字生态系统,中国CIO在技术 ...
- 护网杯 task_shoppingCart 记录
前言 相关题目位于 https://gitee.com/hac425/blog_data/tree/master/hwb task_shoppingCart 漏洞位于 00BD9 用户输入 idx 然 ...
- 2018-10-27 22:44:33 c language
2018-10-27 22:44:33 c language 标准的C语言并不支持上面的二进制写法,只是有些编译器自己进行了扩展,才支持二进制数字.并不是所有的编译器都支持二进制数字,只有一部分编译 ...
- 今日面试WPS总结
1.使用正则来实现替换文件名前三位+...+后两位+后缀名 '1234.56789.jpg'.replace(/^(.{3})(.+?)(.{2})(?=\.[^\.]+)$/,"$1$3& ...
- 团队项目个人进展——Day09
一.昨天工作总结 冲刺第九天,把地图和界面整合了一下 二.遇到的问题 地图的样式和一些公共样式有冲突 三.今日工作规划 团队共同整合程序,做出第一阶段的成品.
- 70部MAYA灯光材质渲染教程合集
MAYA灯光材质渲染教程合集 教程格式:MP4和flv 两种格式 使用版本:教程不是一年出的教程,各个版本都有 (教程软件为英文版) 清晰度:可以看清软件上的文字 语言:部分中文字幕,其他英文(通过看 ...
- python的函数(二)
1,函数的变量 2,函数的返回值 1,函数的变量 1.0,函数的变量分为局部变量和全局变量. def fun(): x = 100 print x 这个x是局部变量,函数执行完后,x的变量就会销毁,只 ...