0
点赞
收藏
分享

微信扫一扫

Spring MVC整合Mybatis实例


示例下载地址:

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

        (1) 导入相关包,包结构如下图所示:

Spring MVC整合Mybatis实例_Mybatis

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

 


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



 

        (3) 在src下添加jdbc.properties

 




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



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

 

 



 


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



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

 

 




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



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

     

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

     



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



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

     

     



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



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

       

              (8) 修改控制器LoginController

       


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



              通过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页面

       


      1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
      2. "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. "${result }" var="item">  
      12.         ${item.id }--${item.name }--${item.parentId }--${item.url }--${item.isShowLeft }<br />  
      13.     </c:forEach>  
      14. </body>  
      15. </html>



              (10) 显示结果

       

       

      举报

      相关推荐

      0 条评论