SpringMVC + mybatis + spring 통합 강좌

27752 단어 springmvcmybatis
                ,            ,          。     ,      ,        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   )

             :
       ![](http://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       ,
   ![](http://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 ,      !   ,            ,                  ,    !

좋은 웹페이지 즐겨찾기