您好,登錄后才能下訂單哦!
這篇文章主要講解了java中如何搭建ssj框架的項(xiàng)目,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
1.新建一個(gè)maven項(xiàng)目
2.在pom.xml中,配置所需的jar包及其版本號(hào)
<!-- 設(shè)置自定義屬性和值 --> <properties> <spring.version>4.1.7.RELEASE</spring.version> <hibernate.version>4.3.11.Final</hibernate.version> </properties> <dependencies> <!-- spring配置 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</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-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- SpringAOP需要依賴這個(gè)jar包 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <!-- 引入servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- SpringMVC文件上傳時(shí)依賴這個(gè)jar包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <!-- 引入Jackson相關(guān)依賴 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency> <!-- 引入jstl包 --> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- spring-data-jpa --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.9.0.RELEASE</version> </dependency> <!-- hibernate 配置 4.3.11.Final --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- mysql數(shù)據(jù)庫(kù)的驅(qū)動(dòng)包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- c3p0數(shù)據(jù)庫(kù)連接池 --> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- java單元測(cè)試框架 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- spring 與Junit整合相關(guān)依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- apache工具包 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <!-- md5 加密相關(guān)依賴 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- 發(fā)送郵件 --> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <!-- 下載壓縮附件 --> <dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.9.7</version> </dependency> </dependencies>
3.建造完成后,自動(dòng)生成一個(gè)web.xml
在web.xml中:
A. 加載Spring配置文件
B. 配置Spring的監(jiān)聽(tīng)
C. 配置SpringMVC的核心控制器
D. 配置編碼過(guò)濾器,作用:防止中文亂碼
E. 配置驗(yàn)證碼相關(guān)servlet(有需要的話)
代碼如下:
<!-- 加載Spring配置文件 --> <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> <!-- 配置SpringMVC的核心控制器 攔截所有請(qǐng)求 --> <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> <!-- 配置編碼過(guò)濾器,作用:防止中文亂碼 --> <filter> <filter-name>abc</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>abc</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置驗(yàn)證碼相關(guān)servlet --> <servlet> <servlet-name>CheckCodeServlet</servlet-name> <servlet-class>cn.fh.servlet.CheckCodeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CheckCodeServlet</servlet-name> <url-pattern>/CheckCodeServlet</url-pattern> </servlet-mapping>
4.在resources文件夾下,創(chuàng)建并配置applicationContext.xml和springMVC.xml文件
applicationContext.xml中詳細(xì)配置:
A. 創(chuàng)建數(shù)據(jù)源:創(chuàng)建c3p0連接池,連接數(shù)據(jù)庫(kù)
B. 根據(jù)數(shù)據(jù)源,創(chuàng)建實(shí)體管理工廠
C. 配置事務(wù)
D. SpringDataJpa的相關(guān)配置
E. 開(kāi)啟組件的掃描
F. 定義advice,配置傳播特性、事務(wù)隔離級(jí)別、只讀事務(wù)、回滾策略
G. 切點(diǎn)配置 execution
springMVC.xml
A. 開(kāi)啟注解
B. 放開(kāi)靜態(tài)資源
C. 配置視圖解析器
D. 上傳文件相關(guān)配置
E. 注冊(cè)攔截器
5.新建model層,在實(shí)體類中,通過(guò)注解的方式來(lái)配置數(shù)據(jù)庫(kù)中表的信息
@Entity 表明這是一個(gè)實(shí)體類
@Table(name = "")在數(shù)據(jù)庫(kù)中生成對(duì)應(yīng)的表名
@Id聲明主鍵
@GeneratedValue設(shè)置主鍵生成策略
GenerationType.IDENTITY mySql主鍵生成策略
GenerationType.SEQUENCE oracle主鍵生成策略
GenerationType.AUTO 根據(jù)不同的數(shù)據(jù)庫(kù),設(shè)置對(duì)應(yīng)的主鍵生成策略
6.新建repository層,并創(chuàng)建接口,同時(shí)繼承兩個(gè)接口
其中:
分別是: JpaRepository<T,ID>和JpaSpecificationExecutor<T>
T 與model層對(duì)象相對(duì)應(yīng)
ID跟model層對(duì)象Id的類型相對(duì)應(yīng)
JpaRepository:排序,分頁(yè),一些簡(jiǎn)單的增刪改查
JpaSpecificationExecutor:復(fù)雜的動(dòng)態(tài)條件查詢
7.新建service serviceImpl controller層,并完成自動(dòng)注入(通過(guò)@Autowired注解)
其中:
serviceImpl類中注入repository
@Autowired private UserRepository userRepository;
controller層中注入service
@Autowired private UserService userService;
8.新建controller層之后,先測(cè)試一下項(xiàng)目的視圖解析器(@RequestMapping) 和springMVC攔截器(主要通過(guò)@ResponseBody注解,把返回的結(jié)果轉(zhuǎn)換為json格式,給瀏覽器),然后再進(jìn)行后續(xù)代碼的具體編寫。
// 測(cè)試攔截器 @RequestMapping("test") @ResponseBody public String test(){ return "test"; } // 測(cè)試SpringMVC視圖解析器 @RequestMapping("toIndex") public String toIndex(){ return "index"; }
9.將項(xiàng)目部署到tomcat下,并運(yùn)行此項(xiàng)目。
看完上述內(nèi)容,是不是對(duì)java中如何搭建ssj框架的項(xiàng)目有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(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)容。