三、WEB项目开发之SpringMVC+Mybatis搭建网站框架

(一)使用IDEA创建一个新项目
(二)添加整个项目所需的依赖(pom文件)

/* 以下是SpringMVC需要用到的依赖,无需版本号,由“spring-framework-bom”统一管理 */

//为了防止用Maven管理Spring项目时,不同的项目依赖了不同版本的Spring,可以使用Maven BOM来解决者一问题, 即可统一版本,而且,在引入BOM之后,在引入其他Spring依赖时,都无需指定版本
spring-framework-bom4.3.17.RELEASE
spring-core
spring-web
spring-webmvc
//这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean 以及进行Inversion of Control/ Dependency Injection(IoC/DI)操作相关的所有类。
spring-beans
spring-context
//spring表达式语言
spring-expression
//为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理
spring-tx
spring-jdbc

/* 以上是SpringMVC需要用到的依赖 */

/* Spring配置上传文件解析器需要用到的依赖 */
commons-fileupload

/* 以下是和数据库交互需要用到的依赖 */
mysql-connector-java

//下面两个是Spring中使用Mybatis需要的依赖
mybatis
mybatis-spring

//数据库连接池
druid
/* 以上是和数据库交互需要用到的依赖 */

/* 以下是日志需要用到的依赖 start*/

/*logback-classic中就包含了logback-core、slf4j-apiJAR包。作用就是打印日志提供方法*/
logback-classic
/* logback-ext-spring是Spring提供的,它用于提供listener监听器。 */
logback-ext-spring
/* cl-over-slf4j用来把Spring中大量使用common-logging替换成slf4j。只有有个这个包,才能打印出Spring框架本身的信息数据。*/
jcl-over-slf4j

/* 以上是日志需要用到的依赖 end*/

/* 以下是WEB三贱客 */
javax.servlet-api
jsp-api
jstl
/* 以上是WEB三贱客 */

/* 以下是JSON需要的依赖 */
jackson-core
jackson-annotations
jackson-databind
/* 以上是JSON需要的依赖 */

/* 以下是常用工具需要的依赖 */
commons-lang3
commons-collections4
/* 以上是常用工具需要的依赖 */

/* 以下是测试工具需要的依赖 */
junit
/* 以上是测试工具需要的依赖 */

(四)创建相关配置文件
1、创建与数据库交互需要的属性文件“dao.properties”

#jdbc配置
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/order?autoReconnect\=true&useUnicode\=true&characterEncoding\=utf-8&allowMultiQueries\=true
jdbc.username=root
jdbc.password=root
#druid配置
druid.initialSize=5
druid.maxActive=30
druid.minIdle=1
druid.maxWait=60000
druid.testBetweenEvictionRunMillis=6000
druid.minEvictableIdleTimeMillis=300000
druid.testWhileIdle=true
druid.validationQuery=SELECT 'x'
druid.testOnBorrow=true
druid.testOnReturn=false
druid.poolPreparedStatements=true
druid.maxOpenPreparedStatements=20
druid.filters=wall,stat

2、Mybatis核心配置文件(“mybatis.xml”)

<configuration>
<settings>
  
<setting name="useGeneratedKeys" value="true"/>
 
<setting name="mapUnderscoreToCamelCase" value="true"/>

settings>
configuration>

3、Spring核心配置文件(“applicationContext.xml”)


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    

    
    
    <context:property-placeholder location="classpath:properties/*.properties"/>
    
    <import resource="classpath:spring/applicationContext-*.xml"/>
beans>

4、Spring容器配置文件之数据库(“applicationContext-dao.xml”)


<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">

    

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="initialSize" value="${druid.initialSize}"/>
        <property name="maxActive" value="${druid.maxActive}"/>
        <property name="minIdle" value="${druid.minIdle}"/>
        <property name="maxWait" value="${druid.maxWait}"/>
        <property name="timeBetweenEvictionRunsMillis" value="${druid.testBetweenEvictionRunMillis}"/>
        <property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/>
        <property name="testWhileIdle" value="${druid.testWhileIdle}"/>
        <property name="validationQuery" value="${druid.validationQuery}"/>
        <property name="testOnBorrow" value="${druid.testOnBorrow}"/>
        <property name="testOnReturn" value="${druid.testOnReturn}"/>
        <property name="poolPreparedStatements" value="${druid.poolPreparedStatements}"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${druid.maxOpenPreparedStatements}"/>
        <property name="filters" value="${druid.filters}"/>
        <property name="proxyFilters">
            <list>
                <ref bean="logFilter"/>
            list>
        property>
    bean>
    <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
        <property name="statementExecutableSqlLogEnable" value="false"/>
    bean>

    

    
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        <property name="configLocation" value="classpath:spring/mybatis.xml"/>
        
        <property name="typeAliasesPackage" value="com.imooc.bean"/>
        
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory"/>
        <property name="basePackage" value="com.imooc.dao"/>
    bean>
    
beans>

4、Spring容器配置文件之服务层(“applicationContext-service.xml”)


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    <context:component-scan base-package="com.imooc.service"/>
    
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    bean>
    
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
beans>

5、Spring容器配置文件之控制层(“application-web.xml”)


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    <mvc:annotation-driven/>
    
    <mvc:default-servlet-handler/>
    
    <bean id="viewResolver" 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>
    
    <context:component-scan base-package="com.imooc.controller"/>
beans>

(三)配置web.xml,注册相关对象


<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"
         version="3.0">
    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        
        <param-value>classpath:spring/root-context.xmlparam-value>
    context-param>
    <listener>
        
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    
    
    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <servlet>
        <servlet-name>dispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring/applicationContext.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServletservlet-name>
        
        <url-pattern>/url-pattern>
    servlet-mapping>
    

    
    <servlet>
        <servlet-name>druidStatViewservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
        <init-param>
            
            <param-name>resetEnableparam-name>
            <param-value>falseparam-value>
        init-param>
        <init-param>
            
            <param-name>loginUsernameparam-name>
            <param-value>adminparam-value>
        init-param>
        <init-param>
            
            <param-name>loginPasswordparam-name>
            <param-value>1234param-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>druidStatViewservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>
    <filter>
        
        <filter-name>druidWebStatFilterfilter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
        <init-param>
            
            <param-name>exclusionsparam-name>
            <param-value>/public/*,*.js,*.css,/druid*,*.jsp,*.swfparam-value>
        init-param>
        <init-param>
            
            <param-name>principalSessionNameparam-name>
            <param-value>sessionInfoparam-value>
        init-param>
        <init-param>
            
            <param-name>profileEnableparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>druidWebStatFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    

web-app>

(五)规划项目目录结构
三、WEB项目开发之SpringMVC+Mybatis搭建网站框架_第1张图片

    注意:以上只是初步搭建框架,后面根据实际需要,再更改上述文件!

你可能感兴趣的:(web项目开发)