SpringMVC+mybatis+spring整合教程

     今天终于决定要写博客了,好久以前都纠结博客这个事,终于下定决心开始行动。仔细想了想,没什么可写的,正好现在正在学习SSM(sping+springMVC+mybatis)这个框架,于是决定把这三个框架的整合教程整理成一篇文章进行发布出来,希望对大家能够有所帮助。说了好多废话,现在正式进入主题:
     IDE:  Eclipse Mars Release (4.5.0)
     DB: MySQL 5.1.73
     服务器: Tomcat 7.0

     1,首先在Eclipse中新建一个Dynamic Web Project,暂且起名为SSM01;
     2,新建包名:
        com.juyuan238.ssm.mapper  
        com.juyuan238.ssm.service
        com.juyuan238.ssm.pojo
        com.juyuan238.ssm.controller
     3, 导入jar包:
        http://download.csdn.net/detail/samile6899/9100335(jar包资源)

     此时的的项目结构:
       ![](https://img-blog.csdn.net/20150912105840814)

     4, 新建一个Source Folder,命名为config;(将资源文件都放在config文件夹中)
        在config文件夹中新建db.properties文件,定义数据库连接参数,
            db.read.database=数据库名
            db.read.host=数据库连接地址
            db.read.port=端口号
            db.read.userName=数据库连接的账号
            db.read.password=数据库连接的密码
     5, 将log4j.properties文件复制粘贴到config文件夹中;
     6,  新建sqlMapConfig.xml文件:
      <?xml version="1.0" encoding="UTF-8" ?>  
       <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
            "http://mybatis.org/dtd/mybatis-3-config.dtd">  
       <configuration>  
       <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->  
       <settings>  
          <!-- 全局映射器启用缓存 -->  
          <setting name="cacheEnabled" value="true" />  
          <!-- 查询时,关闭关联对象即时加载以提高性能 -->  
          <setting name="lazyLoadingEnabled" value="true" />  
          <!-- 设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能 -->  
         <setting name="aggressiveLazyLoading" value="false" />  
         <!-- 对于未知的SQL查询,允许返回不同的结果集以达到通用的效果 -->  
         <setting name="multipleResultSetsEnabled" value="true" />  
         <!-- 允许使用列标签代替列名 -->  
         <setting name="useColumnLabel" value="true" />  
         <!-- 允许使用自定义的主键值(比如由程序生成的UUID 32位编码作为键值),数据表的PK生成策略将被覆盖 -->  
        <!-- <setting name="useGeneratedKeys" value="true" /> -->  
        <!-- 给予被嵌套的resultMap以字段-属性的映射支持 -->  
        <setting name="autoMappingBehavior" value="FULL" />  
        <!-- 对于批量更新操作缓存SQL以提高性能 -->  
        <setting name="defaultExecutorType" value="BATCH" />  
        <!-- 数据库超过25000秒仍未响应则超时 -->  
        <setting name="defaultStatementTimeout" value="25000" />  
    </settings>  
    <typeAliases>
         //......
    </typeAliases>
    <mappers>
         //......
    </mappers>
   </configuration>
     7,   新建spring.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:jee="http://www.springframework.org/schema/jee"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd 
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.juyuan238.ssm"/>

    <!-- 加载配置文件 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:db.properties</value>
            </list>
        </property>
    </bean>


    <!-- 配置数据源 -->
    <bean id="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl">
            <value><![CDATA[jdbc:mysql://${db.read.host}:${db.read.port}/${db.read.database}?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=true]]></value>
        </property>
        <property name="user"               value="${db.read.userName}" />
        <property name="password"           value="${db.read.password}" />
        <property name="maxPoolSize"        value="12" />
        <property name="minPoolSize"        value="0" />
        <property name="maxStatements"      value="100" />
        <property name="initialPoolSize"    value="3" />
        <property name="maxIdleTime"        value="10"/>
        <property name="idleConnectionTestPeriod"   value="10" />
        <property name="testConnectionOnCheckin"    value="true" />
        <property name="testConnectionOnCheckout"   value="false" />
        <property name="preferredTestQuery"         value="SELECT 1 FROM DUAL" />
    </bean>

    <!-- 配置读的 ibatis (从库)-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
    </bean>


    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>


     <!-- 事务控制 (主库)-->
    <bean id="transactionManager"         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

</beans>
  8,  在WEB-INF下面新建springMVC-servlet.xml:
   <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:p="http://www.springframework.org/schema/p"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/mvc
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >  
    <property name="messageConverters">   
             <list>   
                 <bean class = "org.springframework.http.converter.StringHttpMessageConverter">   
                    <property name = "supportedMediaTypes">
                          <list>
                              <value>text/html;charset=UTF-8</value>   
                         </list>   
                    </property>   
                 </bean>   
             </list>   
       </property>  
    </bean> 

      <context:component-scan base-package="com.juyuan238.ssm.controller"/>
      <mvc:annotation-driven />

     <!-- 对模型视图名称的解析,在请求时模型视图名称添加前后缀 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
     </bean>
</beans> 
 9, 编辑web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"            xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
        <display-name>Web</display-name>
          <welcome-file-list>
                <welcome-file>index.html</welcome-file>
                <welcome-file>index.htm</welcome-file>
                <welcome-file>index.jsp</welcome-file>
                <welcome-file>default.html</welcome-file>
                <welcome-file>default.htm</welcome-file>
                <welcome-file>default.jsp</welcome-file>
          </welcome-file-list>

  <servlet-mapping>
       <servlet-name>default</servlet-name>
       <url-pattern>/assets/*</url-pattern>
  </servlet-mapping>
  <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring*.xml</param-value>
  </context-param>
  <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
       <servlet-name>springMVC</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
       <servlet-name>springMVC</servlet-name>
       <url-pattern>/</url-pattern>
  </servlet-mapping>
  <distributable/>

</web-app>
 10, 新建一个Controller类,命名为:  ShowController         
    @Controller
    public class ShowController {
        @RequestMapping("/")
        public String index(){
            return "login";
        }
    }
11,到WEB-INF/jsp下面新建“ login.jsp ”页面
<body>
     <h1>欢迎来到我的页面!</h1>
</body>
12,新建jsp页面可能会报错,
   ![](https://img-blog.csdn.net/20150912113817309)
  解决办法: 
    选中项目--->右键"Build Path"--->"Configure Build Path"--->Add Library--->Server Runtime--->Next--->选中一个创建的tomcat( 如果没有的话,就是没有创建tomcat服务器,自己去创建一个tomcat服务器 )--->Finish--->OK

13, 点击tomcat添加并运行该项目
14,OK,成功运行该项目,进入到login.jsp中,框架搭建成功!接下来,会继续发布增删改查的帖子,希望发布的帖子能够对大家能够有所帮助,有所启迪!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值