java中ssj框架的项⽬搭建流程
1.新建⼀个maven项⽬
2.在l中,配置所需的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.</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.1</version>
</dependency>
<!-- 引⼊jstl包 -->
<dependency>
java源代码加密<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数据库连接池 -->
<!-- mvnrepository/hange/c3p0 -->  <dependency>
<groupId&hange</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.apachemons</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.建造完成后,⾃动⽣成⼀个l
在l中:
A. 加载Spring配置⽂件
B. 配置Spring的监听
C. 配置SpringMVC的核⼼控制器
D. 配置编码过滤器,作⽤:防⽌中⽂乱码
E. 配置验证码相关servlet(有需要的话)
代码如下:
<!-- 加载Spring配置⽂件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>l</param-value>
</context-param>
<!--配置Spring的监听-->
<listener>
<listener-class>org.t.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>l</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⽂件夹下,创建并配置l和l⽂件l中详细配置:
A. 创建数据源:创建c3p0连接池,连接数据库
B. 根据数据源,创建实体管理⼯⼚
C. 配置事务
D. SpringDataJpa的相关配置
E. 开启组件的扫描
F. 定义advice,配置传播特性、事务隔离级别、只读事务、回滚策略
G. 切点配置 execution
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框架搭建内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。