Spring MVC整合Mybatis实例


本文基于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.       
  14.     <!-- 引入jdbc配置文件 -->  
  15.     <context:property-placeholder location="classpath:jdbc.properties" />  
  16.   
  17.     <!--创建jdbc数据源 -->  
  18.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  19.         destroy-method="close">  
  20.         <property name="driverClassName" value="${driver}" />  
  21.         <property name="url" value="${url}" />  
  22.         <property name="username" value="${username}" />  
  23.         <property name="password" value="${password}" />  
  24.     </bean>  
  25.   
  26.     <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
  27.     <bean id="transactionManager"  
  28.         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  29.         <property name="dataSource" ref="dataSource" />  
  30.     </bean>  
  31.   
  32.     <!-- 创建SqlSessionFactory,同时指定数据源 -->  
  33.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  34.         <property name="dataSource" ref="dataSource" />  
  35.     </bean>  
  36.       
  37.     <!-- 可通过注解控制事务 -->  
  38.     <tx:annotation-driven />  
  39.   
  40.     <!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
  41.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  42.         <property name="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. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容