设为首页 收藏本站
查看: 793|回复: 0

[经验分享] Spring MVC整合Mybatis实例

[复制链接]

尚未签到

发表于 2016-11-26 06:37:44 | 显示全部楼层 |阅读模式
示例下载地址:http://download.csdn.net/detail/geloin/4506640
        本文基于Spring 注解,让Spring跑起来。本文使用Mysql数据库。
        (1) 导入相关包,包结构如下图所示:
DSC0000.jpg

        (2) 修改src/applicationContext.xml文件,结果如下所示:
 
[java] view plaincopy 



  • <?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
 
[java] view plaincopy 



  • driver=com.mysql.jdbc.Driver  
  • url=jdbc:mysql://localhost:3306/ruisystem  
  • username=root  
  • password=root  

  
        (4) 在com.geloin.spring.entity包下添加实体类,实体类对应于数据表,其属性与数据表相同或多于数据表。
 
 
[java] view plaincopy 



  • /** 
  •  * 
  •  * @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中的配置一致)。
 
 
[java] view plaincopy 



  • /** 
  •  * 
  •  * @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接口
 
[java] view plaincopy 



  • /** 
  •  * 
  •  * @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接口的实现
 
 
[java] view plaincopy 



  • /** 
  •  * 
  •  * @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
 
[java] view plaincopy 



  • /** 
  •  * 
  •  * @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页面
 
[java] view plaincopy 



  • <%@ 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) 显示结果
   
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305566-1-1.html 上篇帖子: mybatis中单表的CRUD 下篇帖子: Mybatis之自动生成java代码和sql语句
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表