ssj(spring+springMVC+jpa)项目速建

一:搭建基本框架

1:导入css和bootstrap文件夹到webapp目录下。
在这里插入图片描述
2:在src/main/java目录下创建包结构
在这里插入图片描述
controller+model+repository+service+util 文件夹。
resource目录下放入applicationContext.xml(spring的配置文件)和springMVC.xml(springMVC的配置文件) 文件。
3:在webapp的WEB-INF下新建view/friend(对象)/friend-list.jsp文件。
在这里插入图片描述
这时候ssj框架已经搭建好了,接下来就是完善各个文件夹内容。

二:框架的完善

1:配置pom.xml的相关依赖 ps:maven创建项目时能很好的管理jar包,对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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.fh</groupId>
  <artifactId>ljy_friend</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
<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>
</project>

2:web.xml配置(配置spring,springMVC,过滤器等)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>ljy_friend</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 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过滤器,设置字符编码格式为utf-8 -->
  <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>
  
</web-app>

3: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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
 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.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

 
  <!-- 1、创建数据源:创建c3p0连接池,连接数据库 -->
	 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	  <property name="driverClass" value="com.mysql.jdbc.Driver" />
	  <!-- ************ -->
	  <property name="jdbcUrl"
	   value="jdbc:mysql://localhost:3306/1908a?useUnicode=true&amp;characterEncoding=UTF-8" />
	  <property name="user" value="root" />
	  <property name="password" value="root" />
	 </bean>
	 
	 
	 <!-- 2.根据数据源,创建实体管理工厂 -->
	<bean id="entityManagerFactory"
	  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	  <!-- 数据源 -->
	  <property name="dataSource" ref="dataSource" />
	  <!-- ********扫描实体类的包  com.fh.model-->
	  <property name="packagesToScan" value="com.fh.model" />
	  <!-- 指定jpa的提供商 -->
	  <property name="persistenceProvider">
	   <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
	  </property>
	  <!--JPA的供应商适配器 -->
	  <property name="jpaVendorAdapter">
	   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
	    <!-- true: HBM2DDL_AUTO为"update" ,有表就维护表,没有表就创建表 false: 默认为false -->
	    <property name="generateDdl" value="true" />
	    <property name="database" value="MYSQL" />
	    <!-- 数据库方言:根据这个方言生成不同数据库的sql -->
	    <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
	    <property name="showSql" value="true" />
	   </bean>
	  </property>
	  <!-- 可选:Jpa的方言 -->
	  <property name="jpaDialect">
	   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
	  </property>
	 </bean>
	 
	  <!-- 3.配置事务 -->
	 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	  <property name="entityManagerFactory" ref="entityManagerFactory" />
	 </bean> 
 
	<!--  4.SpringDataJpa的相关配置 
		 base-package:指定基包 
		 entity-manager-factory-ref:引用实体类管理器工厂
	   	 transaction-manager-ref: 引用平台事务管理器 -->
	   	 <!-- ************* -->
  <jpa:repositories base-package="com.fh.repository"
   entity-manager-factory-ref="entityManagerFactory"
   transaction-manager-ref="transactionManager"
    />
 
 
 	<!-- *********5、开启组件的扫描 -->
	<context:component-scan base-package="com.fh" />
 
 
 <!-- 定义advice,配置传播特性、事务隔离级别、只读事务、回滚策略 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="change*" propagation="REQUIRED"
			rollback-for="java.lang.Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="batchDelete*" propagation="REQUIRED"
			rollback-for="java.lang.Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="java.lang.Exception" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- 切点配置 execution(* com.fh.service.impl.*.*(..)) 
		第一个*:任意返回值 
		第二个*:包下任意的类 
		第三个*:类中的所有方法 
		(..):任意参数 -->
		<!-- ******************** -->
	<aop:config>
		<aop:pointcut expression="execution(* com.fh.service.impl.*.*(..))"
			id="servicePointcut" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
	</aop:config>
</beans> 

4:springMVC.xml配置(开启注解扫描controller层,视图解析器,文件上传,登录拦截器等)

<?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:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


 	   <!-- 开启注解 补充springmvc注解 -->
   <mvc:annotation-driven></mvc:annotation-driven>
    <!-- base-package 扫描项目中 com.fh.controller下的java类-->
 <context:component-scan base-package="com.fh.controller"></context:component-scan>
 
 
	<!-- 放开静态资源 -->
	<!--location 配置静态资源的路径    mapping  /** 代表/bootstrap/路径下所有的请求都放开-->
	<!-- 为什么配置两颗星? 
	第一个*:bootstrap文件夹下的所有文件  
	第二个* :bootstrap文件夹下的所有文件和文件夹
	
	-->
	<mvc:resources location="/bootstrap/" mapping="/bootstrap/**"></mvc:resources>
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
	
 
 
	<!-- 配置视图解析器 把handler 方法返回值解析为实际的物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<!-- prefix前缀  suffix后缀-->
	        <property name = "prefix" value="/WEB-INF/view/"></property>
	        <property name = "suffix" value = ".jsp"></property>
	</bean>
    
    
    <!-- 上传文件相关配置 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	  <!-- 上传文件大小上限,单位为字节(10MB) -->
	  <property name="maxUploadSize">
	   <value>10485760</value>
	  </property>
	  <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
	  <property name="defaultEncoding">
	   <value>UTF-8</value>
	  </property>
	 </bean>
    </beans> 

5:java包下完善信息
在这里插入图片描述
到此ssj项目搭建完成!!!

三:测试项目

1:在controller层新建方法toFriendList返回jsp页面
在这里插入图片描述
将项目放入tomcat中debug后
在这里插入图片描述
小编在friend-list.jsp中写的这句话正常显示,表明项目搭建成功。

2:下面将 controller,service,model,repository层都相互引用,简单的查询一条数据
在controller层新建方法如下:

@RequestMapping("findFriendList")
	@ResponseBody
	public DataTableResult findFriendList(FriendVo friendVo) {
		return friendService.findFriendList(friendVo);
	}

service层
在这里插入图片描述
service的实现类
在这里插入图片描述
repository层
在这里插入图片描述
在mysql随意添加一条数据,使用DataTables查看数据。
在这里插入图片描述
数据显示正常,图像是因为随意给的路径没有显示。到此我们ssj项目测试成功!!!

四:写在最后

本文是小编个人理解所作,如有错误之处请联系小编修改或删除。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值