ssj项目搭建流程
1.新建一个maven项目
2.建造完成后,生成一个web.xml同时配置spring的监听,springMvc的请求分发器,以及字符集编码
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.在pom.xml中配置所需的jar包及其版本号。
<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>
<!-- 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>
ringDataJpa 配置开始 -->
<!-- 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>
<!-- springDataJpa 配置结束 -->
<!-- 单元测试 配置开始 -->
<!-- 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>
</dependencies>
4.新建包 model,controller,repository,service,model创建实体类,在实体类中使用@Entity注解声明这是
一个实体类,使用@table注解生成表名,使用@id生成主键,使用@GeneratedValue来设置主键生成策略
5.创建repository接口,继承JpaRepository<User, Integer>,此接口可以做简单的增删改查了,JpaSpecificationExecutor这个接口是
动态的条件查询
6.创建service接口,并实现implements此接口,使用@service注解表明这是一个service类,同时生成一个私有的repository,使用@Autowired注解实现。
7.在controller层使用@Controller注解表明这是控制层,使用@RequestMapping生成注解映射请求路径,然后生成一个私有的service,使用@Autowired实现
8.将applicationContext.xml和springMVC.xml引入到项目中在applicationContext.xml中配置数据源,创建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&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="cn.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>
<!-- 4.SpringDataJpa的相关配置
base-package:指定基包
entity-manager-factory-ref:引用实体类管理器工厂
transaction-manager-ref: 引用平台事务管理器 -->
<!-- ************* -->
<jpa:repositories base-package="cn.fh.repository"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"
5.创建jsp页面引入相关依赖
6.在springMVC.xml中放开静态资源,要不然的话jsp页面的功能没法实现
<!--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>