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.IDENTITY mySql主键生成策略
GenerationType.SEQUENCE oracle主键生成策略
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框架搭建内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!