MVC 项目创建的流程

代码理解
读取数据库
1、搞定实体类
2、配置文件,写在哪里都可以,尽量全部放在同一个resources目录下,方便查找

mapper 这个文件是用于: 大多配置在dao层
描述这个接口是怎么访问的,访问了什么内容

 项目创建过程

一、前期一构建各个module,并实现各个modulel的关联

1、构建各个 5个 module层 

1.1创建项目

 

1.2把 src删掉,建立各个模块,也可以留着,但是这样方便查找,简洁

1.3、建各个模型的依赖

 

 1.4、配置全部moven依赖

写在项目  根目录 下的pom.xml

pom.xml

1.5、配置web-module 层

1、添加 response 

 1.1、配置applicationContext.xml (用于配置Spring

<?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: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.nf"/>

    <!-- 配置数据库文件,引入jdbc.properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--配置 C3P0 的 DataSource--> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClassName}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <!-- 连接池中保留的最小连接数,默认为3-->
    <property name="minPoolSize" value="3"/>
    <!-- 连接池中保留的最大连接数。默认值:15   -->
    <property name="maxPoolSize" value="15"/>
    <!-- 初始值连接池中的连接数,取值应在minPoolSize 与 maxPoolSize之间,默认为3-->
    <property name="initialPoolSize" value="3"/>
</bean>


    <!--配置 sqlSessionFactory sql会话工厂 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"></property>
        <property name="mapperLocations" value="classpath*:mapper/*.xml"></property>
    </bean>

    <!-- 5、配置 MapperScannerConfigurer  映射器扫描配置-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描dao层的接口 interface-->
        <property name="basePackage" value="com.nf.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- 6、配置 transactionManager 声明事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="myDataSource"></property>
    </bean>
    <!-- 7、配置 tx:annotation-driven  实现使用来执行事务-->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"></tx:annotation-driven>


</beans>
applicationContext

1.2、配置与数据库相连的 jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.88.88:1521:orcl
jdbc.username=oa
jdbc.password=12345678
jdbc.properties

1.3、配置springmvc.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:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 自动扫描-->
    <!-- 自动扫描包-->
    <context:component-scan base-package="com.nf">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>


    <!--mvc:annotation-driven 是告诉springmvc需要解析:@ResponseBody-->
    <!--如果使用jackson,可以不写里面的bean,因为是默认-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <mvc:resources mapping="/img/**" location="/img/"></mvc:resources>

    <mvc:resources mapping="/page/**" location="/page/"></mvc:resources>


</beans>
springmvc

2、添加webapp

3、添加配置web-module的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>Test_MVC_190219</artifactId>
        <groupId>com.nf</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <packaging>war</packaging>
    <artifactId>web-module</artifactId>

    <build>
        <resources>
            <resource>
                <directory>resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>webapp</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>com.nf</groupId>
            <artifactId>entity-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.nf</groupId>
            <artifactId>dao-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.nf</groupId>
            <artifactId>service-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.nf</groupId>
            <artifactId>action-module</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
 
</project>

4、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 配置web容器一启动,就启动SpringMVC -->
    <!-- 这是spring MVC 的配置 -->
    <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:springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 这是spring 的配置 context Config Location 上下文配置位置 -->
    <context-param>
        <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>


</web-app>

猜你喜欢

转载自www.cnblogs.com/Wjjxj/p/10415481.html