您好,登錄后才能下訂單哦!
簡(jiǎn)單談一下maven搭建 ssm 項(xiàng)目 (使用數(shù)據(jù)庫(kù)oracle,比 mysql 麻煩一點(diǎn),所以這里談一下)
在創(chuàng)建maven 的web項(xiàng)目時(shí),常常會(huì)缺了main/java , main/test 兩個(gè)文件夾。
解決方法:
① : 在項(xiàng)目上右鍵選擇properties,然后點(diǎn)擊java build path,在Librarys下,編輯JRE System Library,選擇workspace default jre就可以了。 (推薦使用這種)
② :手動(dòng)創(chuàng)建 目錄。切換視圖采用Navigator視圖,直接在src/main目錄下建立 Java目錄。
項(xiàng)目目錄結(jié)構(gòu):
重要的配置文件:
對(duì)象模型配置文件: pom.xml
Spring的配置文件:applicationContext.xml
spring MVC配置文件: springmvc.xml
數(shù)據(jù)庫(kù)配置文件: jdbc.properties
日志配置文件: log4j.properties
mybatis配置文件: mybatis-config.xml
網(wǎng)絡(luò)程序配置文件:web.xml
首先配置pom.xml
pom.xml 主要描述了項(xiàng)目的maven坐標(biāo),依賴關(guān)系,自動(dòng)引入jar包
<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.krry</groupId> <artifactId>maven_SSM</artifactId> <version>0.0.1-SNAPSHOT</version> <name>maven_SSM</name> <url>http://maven.apache.org</url> <dependencies> <!--引入junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!--引入servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> <scope>provided</scope> </dependency> <!--引入jstl的包 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <!--引入jsp的編譯依賴 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!--引入log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!--spring springmvc mybatis --> <!-- spring和springmvc相關(guān)的構(gòu)建 jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <!-- springmvc相關(guān) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.2.1.RELEASE</version> </dependency> <!--springmvc需要用到j(luò)son的轉(zhuǎn)換包 jackson --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency> <!--JSR303 后臺(tái)校驗(yàn) hibernate validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.1.1.Final</version> </dependency> <!--上傳文件相關(guān)的jar包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2</version> </dependency> <!--跟加密算法相關(guān)的codeC --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> <!--orm或者jdbc組件需要用到的jar包 mybatis --> <!--oracle數(shù)據(jù)庫(kù)驅(qū)動(dòng) --> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>12.1.0.2.0</version> </dependency> <!--mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng) (這里不用,用的是上面的oracle驅(qū)動(dòng)) --> <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.8</version> <scope>runtime</scope> </dependency> --> <!-- proxool連接池 --> <dependency> <groupId>com.cloudhopper.proxool</groupId> <artifactId>proxool</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>com.cloudhopper.proxool</groupId> <artifactId>proxool-cglib</artifactId> <version>0.9.1</version> </dependency> <!--引入mybatis需要的jar包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.4</version> </dependency> <!-- 分頁(yè)管理需要的jar包,這里沒(méi)用到 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <version>3.0</version> </configuration> </plugin> </plugins> <finalName>maven_SSM</finalName> </build> </project>
這里說(shuō)一下maven工程利用pom.xml導(dǎo)入oracle驅(qū)動(dòng)包的問(wèn)題:
由于Oracle授權(quán)問(wèn)題,Maven不提供Oracle JDBC driver,為了在Maven項(xiàng)目中應(yīng)用Oracle JDBC driver,必須手動(dòng)添加到本地倉(cāng)庫(kù)。
如果電腦中已經(jīng)裝有Oracle數(shù)據(jù)庫(kù),則在安裝路徑下有數(shù)據(jù)庫(kù)的驅(qū)動(dòng)程序,可以直接用。D:\Oracle\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib
也可以直接到Oracle官網(wǎng)上下載Oracle數(shù)據(jù)庫(kù)驅(qū)動(dòng), 使用SQL語(yǔ)句查詢數(shù)據(jù)庫(kù)驅(qū)動(dòng)的版本: SELECT * FROM v$instance
然后確定版本下載:http://www.oracle.com/technetwork/database/features/jdbc/default-2280470.html
打開windows的命令行界面,進(jìn)入驅(qū)動(dòng)包ojdbc6的目錄,然后運(yùn)行:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=12.1.0.2.0 -Dpackaging=jar -Dfile=ojdbc6.jar
顯示"BUILD SUCCESS" 成功,就會(huì)自動(dòng)導(dǎo)入你的maven本地倉(cāng)庫(kù)。
然后就可以在maven項(xiàng)目里添加dependency,各坐標(biāo)對(duì)應(yīng)上面這個(gè)命令的個(gè)元素,如下:
<dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>12.1.0.2.0</version> </dependency>
Spring的配置文件:applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" 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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd "> <!-- 配置包掃描 --> <context:component-scan base-package="com.krry"></context:component-scan> <!-- 導(dǎo)入外部資源文件 --> <!-- <context:property-placeholder location="classpath:jdbc.properties" /> --> <bean class="com.krry.core.des.EncryptPropertyPlaceholderConfigurer" p:location="classpath:jdbc.properties" p:fileEncoding="utf-8" /> <!-- proxool連接池 --> <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource"> <!-- 驅(qū)動(dòng)的名字,mysql --> <property name="driver" value="${db.driver}"></property> <!--proxool 的 url連接串,這個(gè)必須確定用戶名和密碼 --> <property name="driverUrl" value="${db.url}"></property> <!-- 用戶名(proxool沒(méi)有使用,但是不能沒(méi)有) --> <property name="user" value="${db.username}"></property> <!-- 密碼(proxool沒(méi)有使用,但是不能沒(méi)有) --> <property name="password" value="${db.password}"></property> <!-- proxool自動(dòng)偵察各個(gè)連接狀態(tài)的時(shí)間間隔(毫秒),偵察到空閑的連接就馬上回收,超時(shí)的銷毀 現(xiàn)在設(shè)置為4秒) --> <property name="houseKeepingSleepTime" value="3000"></property><!-- 自動(dòng)檢查連接是否斷掉開關(guān) --> <property name="testBeforeUse" value="true"></property> <!-- 如果發(fā)現(xiàn)了空閑的數(shù)據(jù)庫(kù)連接.house keeper 將會(huì)用這個(gè)語(yǔ)句來(lái)測(cè)試.這個(gè)語(yǔ)句最好非??斓谋粓?zhí)行.如果沒(méi)有定義,測(cè)試過(guò)程將會(huì)被忽略 --> <property name="houseKeepingTestSql" value="SELECT count(1) from dual"></property> <!-- 如果housekeeper 檢測(cè)到某個(gè)線程的活動(dòng)時(shí)間大于這個(gè)數(shù)值.它將會(huì)殺掉這個(gè)線程.所以確認(rèn)一下你的服務(wù)器的帶寬.然后定一個(gè)合適的值.默認(rèn)是5分鐘. 現(xiàn)在設(shè)置 10 秒--> <property name="maximumActiveTime" value="10000"></property> <!-- 最少保持的空閑連接數(shù) (現(xiàn)在設(shè)置20個(gè)) --> <property name="prototypeCount" value="20"></property> <!-- 最大連接數(shù) (現(xiàn)在設(shè)置100個(gè)) --> <property name="maximumConnectionCount" value="200"></property> <!-- 最小連接數(shù) (現(xiàn)在設(shè)置50個(gè)) --> <property name="minimumConnectionCount" value="50"></property> <!-- 如果為true,那么每個(gè)被執(zhí)行的SQL語(yǔ)句將會(huì)在執(zhí)行期被log記錄(DEBUG LEVEL).你也可以注冊(cè)一個(gè)ConnectionListener (參看ProxoolFacade)得到這些信息. --> <property name="trace" value="false"></property> <property name="verbose" value="true"></property> </bean> <!-- 注冊(cè)事務(wù)管理器 --> <bean id="txMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 開啟事務(wù)注解驅(qū)動(dòng) --> <tx:annotation-driven transaction-manager="txMgr" /> <!-- 配置mybatis的sqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 配置可以整體掃描Mapper的一個(gè)掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--如果有多個(gè)報(bào)路徑,用逗號(hào)分開即可 --> <property name="basePackage" value="com.krry.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
spring MVC配置文件: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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 開啟注解模式驅(qū)動(dòng) --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <!-- 開啟mvc的注解模式 user 還會(huì)注冊(cè)一個(gè)ConversionService 子類FormattingConversionServiceFactoryBean--> <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.krry.core.UTF8StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter"/> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="prefixJson" value="false" /> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <!-- 處理responseBody 里面日期類型 --> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" /> </bean> </property> <!-- 為null字段時(shí)不顯示 --> <property name="serializationInclusion"> <value type="com.fasterxml.jackson.annotation.JsonInclude.Include">NON_NULL</value> </property> </bean> </property> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/x-www-form-urlencoded;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 掃包 --> <context:component-scan base-package="com.krry.controller"></context:component-scan> <!--對(duì)靜態(tài)資源文件的訪問(wèn) 必須要設(shè)置,因?yàn)樵趕pringmvc的配置中配置了/匹配所有請(qǐng)求, 此工程所有的請(qǐng)求(.do ,addUser,js/image/css)都會(huì)被springmvc解析, 必須對(duì)所有的靜態(tài)資源文件進(jìn)行過(guò)濾放行 --> <!-- 靜態(tài)資源過(guò)濾 下面二選一--> <!--<mvc:default-servlet-handler/> --> <mvc:resources mapping="/resourse/**" location="/resourse/" /> <!-- 攔截器定義 --> <mvc:interceptors> <mvc:interceptor> <!-- 個(gè)人中心也需要登陸 以admin開頭的配置都會(huì)進(jìn)行攔截--> <mvc:mapping path="/admin/**"></mvc:mapping> <!-- 這個(gè)是設(shè)置不會(huì)進(jìn)入攔截器的路徑 --> <mvc:exclude-mapping path="/resourse/**"/> <!-- 攔截器進(jìn)入的類,返回false表示不會(huì)進(jìn)入輸入的路徑 --> <bean class="com.krry.core.filter.LoginInterceptor" /> </mvc:interceptor> </mvc:interceptors> <!-- 配置文件解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8"> <property name="uploadTempDir" value="/temp"></property> <property name="maxUploadSize"> <value>209715200</value><!-- 200MB --> </property> <property name="maxInMemorySize"> <value>4096</value><!-- 4KB大小讀寫 --> </property> </bean> <!-- 視圖渲染 jsp/freemaker/velocity--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 制定頁(yè)面存放的路徑 --> <property name="prefix" value="/WEB-INF/pages/"></property> <!-- 文件的后綴 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
數(shù)據(jù)庫(kù)配置文件: jdbc.properties
db.driver=oracle.jdbc.OracleDriver db.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl db.username=4m+la23KCA4= db.password=WWijcIyMPaU\=
我這里使用了加密算法
日志配置文件: log4j.properties
log4j.rootLogger=DEBUG, CONSOLE, FILE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %10l - %m%n log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=D:/logs/log4j.log log4j.appender.FILE.MaxFileSize=1MB log4j.appender.FILE.Append = true log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy/MM/dd/HH:mm:ss} %-5p [%t] %10l - %m%n
mybatis配置文件: mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 全局映射器啟用緩存 --> <setting name="cacheEnabled" value="true" /> <!-- 查詢時(shí),關(guān)閉關(guān)聯(lián)對(duì)象即時(shí)加載以提高性能 --> <setting name="lazyLoadingEnabled" value="true" /> <!-- 設(shè)置關(guān)聯(lián)對(duì)象加載的形態(tài),此處為按需加載字段(加載字段由SQL指 定),不會(huì)加載關(guān)聯(lián)表的所有字段,以提高性能 --> <setting name="aggressiveLazyLoading" value="false" /> <!-- 對(duì)于未知的SQL查詢,允許返回不同的結(jié)果集以達(dá)到通用的效果 --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- 允許使用列標(biāo)簽代替列名 --> <setting name="useColumnLabel" value="true" /> <!-- 允許使用自定義的主鍵值(比如由程序生成的UUID 32位編碼作為鍵值),數(shù)據(jù)表的PK生成策略將被覆蓋 --> <setting name="useGeneratedKeys" value="true" /> <!-- 給予被嵌套的resultMap以字段-屬性的映射支持 --> <setting name="autoMappingBehavior" value="FULL" /> <!-- 對(duì)于批量更新操作緩存SQL以提高性能 --> <setting name="defaultExecutorType" value="BATCH" /> <!-- 數(shù)據(jù)庫(kù)超過(guò)25000秒仍未響應(yīng)則超時(shí) --> <setting name="defaultStatementTimeout" value="25" /> <setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/> </settings> <typeAliases> <!--自定義user對(duì)象的別名 --> <!-- <typeAlias type="com.krry.mybatis.sysmanage.entity.User" alias="user"/> --> <!-- 批量定義別名 --> <package name="com.krry.entity" /> </typeAliases> </configuration>
網(wǎng)絡(luò)程序配置文件:web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>maven_SSM</display-name> <welcome-file-list> <welcome-file>index</welcome-file> </welcome-file-list> <!-- 加載Spring IOC容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- spring上下文監(jiān)聽(tīng)器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Introspector緩存清除監(jiān)聽(tīng)器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>maven_SSM</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置springMVC的配置文件 --> <!-- 如果不配置下面選項(xiàng),系統(tǒng)默認(rèn)加載classpath下面名為[servlet-name]-servlet.xml的文件 springmvc01-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>maven_SSM</servlet-name> <url-pattern>/index</url-pattern> </servlet-mapping> <!-- 可以配*.do, *.action(了解) / (重點(diǎn)): 所有的請(qǐng)求都會(huì)被spring mvc解析,但必須對(duì)靜態(tài)資源文件進(jìn)行過(guò)濾放行,建議大家使用這種方式 /* : 不建議大家使用 --> <servlet-mapping> <servlet-name>maven_SSM</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
到這里,基本配置全部完成,jar包也自動(dòng)依賴。就進(jìn)行測(cè)試和編寫后續(xù) java 的代碼
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。