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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM