https://github.com/Ethel731/WebProjectDemohtml
以前都是在已經創建好的項目基礎上開發,沒有本身創建過IDEA的maven+springMVC+Mybatis項目,今天恰好學習一下。前端
maven的本地安裝自行度娘~java
首先,打開IDEA後新建一個project:mysql
選擇maven項目和JDK版本後,勾選Create from archetype建立原型,由於咱們這是一個web項目,所以選擇maven-archetype-webapp,點擊「next」:git
此時跳出的窗口是對maven倉庫的基本配置,groupID中填寫本項目倉庫路徑(默認小寫),artifactID爲項目名稱。Version通常分兩種,SNAPSHOT爲快照版本,RELEASE爲最終發佈版本,前面的編號爲版本號:github
接下來進入maven配置階段,個人本地安裝路徑爲D:\apache-maven-3.5.0,接下來兩個分別是maven配置文件地址和本地倉庫地址:web
最後審查一下咱們的配置,點擊"finish":spring
IDEA生成的項目目錄src下並無源碼文件夾,須要在main下新建一個Java文件夾:sql
新建文件夾後,IDEA仍然不能識別源碼和resources文件地址,須要咱們手動設置一下。數據庫
右鍵新建的Java文件夾,mark directory as sources root:
對同目錄下的resources文件夾也是一樣的操做,mark directory as resources root:
完成後項目目錄是這樣的:
web.xml爲JavaWEB核心配置文件,也是程序的入口,所以咱們首要配置它。IDEA自動生成的web.xml並非規範的,是DOCTYPE規範,而咱們WEB-INF下的web.xml是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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="ssm" version="2.5"> <!-- 初始化spring容器 --> <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> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 解決jsp include html 亂碼問題 --> <jsp-config> <jsp-property-group> <description>html encoding</description> <display-name>JSPConfiguration</display-name> <url-pattern>*.html</url-pattern> <el-ignored>true</el-ignored> <page-encoding>UTF-8</page-encoding> <scripting-invalid>false</scripting-invalid> <include-prelude></include-prelude> <include-coda></include-coda> </jsp-property-group> </jsp-config> <!-- springmvc的前端控制器 --> <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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
配置完web.xml以後,緊接着就是項目根目錄下的pom文件,該文件用於管理源代碼、配置文件、開發者的信息和角色、問題追蹤系統、組織信息、項目受權、項目的url、項目的依賴關係等等。
配置pom前,首先是技術選型,也就是這個項目所須要用到哪些技術棧。若是不知道本身dependency怎麼寫,能夠直接去https://mvnrepository.com/中搜索。eg:搜索mybatis彈出mybatis相關,點擊mybatis後進入版本列表,點擊其中一個版本,出現其配置方法:
如今貼入本DEMO的pom.xml(注意MyBatis Generator的配置,下面會講到):
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zyx.freemarkerworddemo</groupId> <artifactId>FreemarkerWordDemo</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>FreemarkerWordDemo Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 集中定義依賴版本號 --> <properties> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis-generator.version>1.3.5</mybatis-generator.version> <mysql.version>5.1.32</mysql.version> <druid.version>1.0.9</druid.version> <slf4j.version>1.6.4</slf4j.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>2.5</commons-io.version> <commons-net.version>3.3</commons-net.version> <freemarker.version>2.3.23</freemarker.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- Apache工具組件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- 日誌處理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 鏈接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- freemarker --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP相關 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency> <!-- mybatis generator --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>${mybatis-generator.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>FreemarkerWordDemo</finalName> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>80</port> </configuration> </plugin> <!-- MyBatis Generator --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> </plugin> </plugins> </pluginManagement> <!-- 解決配置文件不拷貝的問題 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <excludes> <exclude>generatorConfig.xml</exclude> </excludes> <filtering>false</filtering> </resource> </resources> </build> </project>
通常resources文件夾下存放的都是咱們項目集成的配置文件,springMVC、Mybatis等文件配置都放在這裏。本DEMO中的配置文件目錄以下:
首先是spring配置文件applicationContext.xml,此處有兩個地方須要注意,一個是阿里的druid數據庫鏈接池,咱們的jdbc.properties在這裏被加載;一個是整合mybatis和spring,須要在MapperScannerConfigurer中配置DAO路徑以便建立MapperFactoryBean。
jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/demofreeword?characterEncoding=utf-8 jdbc.username=root jdbc.password=123456
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize"> <value>1</value> </property> <property name="maxActive"> <value>5</value> </property> <property name="minIdle"> <value>1</value> </property> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait"> <value>60000</value> </property> <!-- 配置監控統計攔截的filters --> <property name="filters"> <value>stat</value> </property> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis"> <value>60000</value> </property> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis"> <value>300000</value> </property> </bean> <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數據庫鏈接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zyx.dao" /> </bean> <!-- aop --> <aop:aspectj-autoproxy/> </beans>
其他配置文件大同小異,在此就不一一列舉了。詳情可下載源碼。
配置文件完成後就是創建J2EE的項目目錄了,源碼文件都在Java文件夾下,具體目錄見下圖:
到此,項目目錄已完成。
mybatis-generator是mybatis的一個插件,用於自動生成model及DAO(mapper)層的代碼,須要開發者事前在數據庫中設置好數據庫表及其字段、屬性等,以便自動映射JAVA對象。此處只寫其配置及使用。
先在pom.xml文件中配置Tomcat插件和MyBatis Generator(上文pom.xml已給出),並定義generatorConfig.xml(打包時不打入war包,只在建立DAO,model層時用到),此處直接貼入generatorConfiguration:
generatorConfig.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" > <generatorConfiguration> <classPathEntry location="D:\apache-maven-3.5.0\repo\mysql\mysql-connector-java\5.1.32\mysql-connector-java-5.1.32.jar" /> <context id="context1" targetRuntime="MyBatis3"> <!-- 序列化pojo --> <plugin type="org.mybatis.generator.plugins.SerializablePlugin" /> <commentGenerator> <property name="suppressDate" value="true" /> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/demofreeword?characterEncoding=utf-8" userId="root" password="123456" /> <javaModelGenerator targetPackage="com.zyx.model" targetProject="src\main\java" /> <sqlMapGenerator targetPackage="com.zyx.dao" targetProject="src\main\java" /> <javaClientGenerator targetPackage="com.zyx.dao" targetProject="src\main\java" type="XMLMAPPER" /> <table schema="" tableName="resume"> <!-- 實體類中的成員變量和表的列明一一對應 --> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration>
在generatorConfiguration中:
配置完成後,點擊右上角
,
點擊「+」添加一個新的configuration,選擇「maven」:
在彈出的對話框Name填寫插件的名字mybatis-generator(這裏是本身定義的,最好寫上插件的名字),command line中填寫mybatis-generator:generate -e(這裏**-e後面不能出現空格**,-e表示在控制檯顯示錯誤信息):
點擊肯定後,可看出右上角已顯示配置好的mybatis-generator,先點擊右側的maven project彈出maven相關操做按鈕,進入run configurations,選擇mybatis-generator。雙擊運行或者右鍵run:
出現下圖結果後,表示項目建成:
備註:在上述生成文件中,dao目錄下接口文件會以Mapper爲後綴,可選擇手動更改成DAO文件。當rename文件成功後,mapper.xml中namespace將自動更改,無需更多操做。
項目生成後,部署到Tomcat。窗口右上角處點擊「Edit Configurations...」,在彈出窗口左上角點擊「+」號選擇Tomcat Server——本地Tomcat:
填寫所用服務名稱(自定義),在server選項下,選擇默認開啓的瀏覽器,點擊下圖「+」號建立Artifacts,選擇war exploded:
- war模式:將WEB工程以包的形式上傳到服務器 ;
- war exploded模式:將WEB工程以當前文件夾的位置關係上傳到服務器;
詳情參見:http://blog.csdn.net/xlgen157387/article/details/56498938
建立號建立Artifacts:
選擇打包方式爲war exploded模式:
服務器設置完成以後,切換選項卡到部署(deployment),選擇Artifacts:
再次選擇war exploded:
一切作完了,點擊Apply無報錯表示部署成功,OK退出。
選擇已部署的項目,點擊右邊綠色三角即爲run: