首页 > 代码库 > spring日记------部署环境、写demo

spring日记------部署环境、写demo

一、安装jdk1.7

祥见http://zhinan.sogou.com/guide/detail/?id=1610006590

二、创建web项目

三、配置ssm环境

3.1添加spring、mybatis、springMvc的jar包及他们之间的整合包

技术分享

技术分享

 

3.2添加三个配置文件并配置相关参数

web.xml文件

 <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.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>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

 

applicationContext.xml文件

 <!-- 读取properties文件org.springframework.beans.factory.config.PlaceholderConfigurerSupport  -->
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                 <value>classpath:dbconfig.properties</value>  
            </list>  
        </property>  
    </bean>
        
        
        <!-- 配置数据库 -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="http://www.mamicode.com/${jdbc.driver}"/>
            <property name="url" value="http://www.mamicode.com/${jdbc.url}"/>
            <property name="username" value="http://www.mamicode.com/${jdbc.username}"/>
            <property name="password" value="http://www.mamicode.com/${jdbc.password}"/>
        </bean>
        
        
        <!-- 配置会话工厂org.mybatis.spring.SqlSessionFactoryBean -->
        <!-- 配置mybatis -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis-config.xml"></property>
        <!-- mapper扫描 -->
        <property name="mapperLocations" value="http://www.mamicode.com/classpath:mybatis/*/*.xml"></property>
    </bean>
 
     <!-- 启用注解 -->
    <context:annotation-config />
    <!-- aop激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    
    <!-- 事务处理器 -->
    <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

 

 

applicationContext-mvc.xml文件

<context:component-scan base-package="com.diancan.controller"/>
    <!-- 配置SpringMVC的视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="http://www.mamicode.com/WEB-INF/jsp/"/>
        <property name="suffix" value="http://www.mamicode.com/.jsp"/>
    </bean>

 

 

mybatis-config.xml文件

<settings> 
        <setting name="cacheEnabled" value="http://www.mamicode.com/true" /><!-- 全局映射器启用缓存 -->   
        <setting name="useGeneratedKeys" value="http://www.mamicode.com/true" /> 
        <setting name="defaultExecutorType" value="http://www.mamicode.com/REUSE" /> 
    </settings>

    <!-- 别名 -->
    <typeAliases>
        <typeAlias type="com.diancan.util.PageData" alias="pd"/>
        <!-- 分页 -->
        <typeAlias type="com.diancan.entity.Page" alias="Page"/>
    </typeAliases>

 

 

dbconfig.properties文件

jdbc.driver:com.mysql.jdbc.Driver
jdbc.url:jdbc:mysql://localhost:3306/diancan
jdbc.username:root
jdbc.password:root

 

四、创建需要的各种包

技术分享技术分享

 

 

let‘s do it!

 

spring日记------部署环境、写demo