java中ssj框架的專案搭建流程
1.新建一個maven專案
2.在pom.xml中,配置所需的jar包及其版本號
<!-- 設定自定義屬性和值 --> <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需要依賴這個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檔案上傳時依賴這個jar包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <!-- 引入Jackson相關依賴 --> <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資料庫的驅動包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!-- c3p0資料庫連線池 --> <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <!-- java單元測試框架 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- spring 與Junit整合相關依賴 --> <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 加密相關依賴 --> <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> <!-- 傳送郵件 --> <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.建造完成後,自動生成一個web.xml
在web.xml中:
A. 載入Spring配置檔案
B. 配置Spring的監聽
C. 配置SpringMVC的核心控制器
D. 配置編碼過濾器,作用:防止中文亂碼
E. 配置驗證碼相關servlet(有需要的話)
程式碼如下:
<!-- 載入Spring配置檔案 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--配置Spring的監聽--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置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> <!-- 配置編碼過濾器,作用:防止中文亂碼 --> <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> <!-- 配置驗證碼相關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資料夾下,建立並配置applicationContext.xml和springMVC.xml檔案
applicationContext.xml中詳細配置:
A. 建立資料來源:建立c3p0連線池,連線資料庫
B. 根據資料來源,建立實體管理工廠
C. 配置事務
D. SpringDataJpa的相關配置
E. 開啟元件的掃描
F. 定義advice,配置傳播特性、事務隔離級別、只讀事務、回滾策略
G. 切點配置 execution
springMVC.xml
A. 開啟註解
B. 放開靜態資源
C. 配置檢視解析器
D. 上傳檔案相關配置
E. 註冊攔截器
5.新建model層,在實體類中,通過註解的方式來配置資料庫中表的資訊
@Entity 表明這是一個實體類
@Table(name = "")在資料庫中生成對應的表名
@Id宣告主鍵
@GeneratedValue設定主鍵生成策略
GenerationType.IDENTITYmySql主鍵生成策略
GenerationType.SEQUENCEoracle主鍵生成策略
GenerationType.AUTO根據不同的資料庫,設定對應的主鍵生成策略
6.新建repository層,並建立介面,同時繼承兩個介面
其中:
分別是: JpaRepository<T,ID>和JpaSpecificationExecutor<T>
T 與model層物件相對應
ID跟model層物件Id的型別相對應
JpaRepository:排序,分頁,一些簡單的增刪改查
JpaSpecificationExecutor:複雜的動態條件查詢
7.新建service serviceImpl controller層,並完成自動注入(通過@Autowired註解)
其中:
serviceImpl類中注入repository
@Autowired private UserRepository userRepository;
controller層中注入service
@Autowired private UserService userService;
8.新建controller層之後,先測試一下專案的檢視解析器(@RequestMapping) 和springMVC攔截器(主要通過@ResponseBody註解,把返回的結果轉換為json格式,給瀏覽器),然後再進行後續程式碼的具體編寫。
// 測試攔截器 @RequestMapping("test") @ResponseBody public String test(){ return "test"; } // 測試SpringMVC檢視解析器 @RequestMapping("toIndex") public String toIndex(){ return "index"; }
9.將專案部署到tomcat下,並執行此專案。
到此這篇關於java中ssj框架的專案搭建流程的文章就介紹到這了,更多相關java ssj框架搭建內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!