歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring MVC整合Mybatis實例

Spring MVC整合Mybatis實例

日期:2017/3/1 10:21:15   编辑:Linux編程

本文基於Spring 注解,讓Spring跑起來。本文使用Mysql數據庫。

(1) 導入相關包,包結構如下圖所示:


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

  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

  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. * @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. }
Copyright © Linux教程網 All Rights Reserved