ssm初始化环境搭建
Posted 会玩代码的摄影师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm初始化环境搭建相关的知识,希望对你有一定的参考价值。
一、创建一个工程
选择Maven,勾选create from archetype,选中maven-archetype-webapp
二、编辑Maven
1、Spring框架必须
2、Spring整合ORM框架(如调用事务管理器)
3、Spring对web的支持(不管Struts2还是SpringMVC都需要,它不是SpringMVC)
4、SpringMVC
5、Spring依赖的jackson(SpringMVC使用@ResponseBody时输出JSON要用到)
6、Mybatis
7、Mybatis-spring (1、SqlSessionFactory 2、完成Dao的实现类)
8、Mybatis需要依赖的数据库的驱动
9、Mybatis需要依赖的C3P0
10、JSTL
POM.XML参考:
<properties>
<spring-version>5.0.5.RELEASE</spring-version>
<jackson-version>2.9.5</jackson-version>
<mybatis-version>3.4.6</mybatis-version>
<mybatis-spring-version>1.3.2</mybatis-spring-version>
<c3p0-version>0.9.5.2</c3p0-version>
<!--oracle的使用私有库(Maven本身不提供Oracle的驱动)-->
<mysql-version>8.0.11</mysql-version>
<jstl-version>1.2</jstl-version>
</properties>
<!--1、Spring框架必须-->
<!--Spring框架必须:Spring Core、Spring Context、Spring Beans、Spring AOP-->
<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-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<!--2、Spring整合ORM框架(如调用事务管理器)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<!--3、Spring对web的支持(不管Struts2还是SpringMVC都需要,它不是SpringMVC)-->
<!--spring对web的支持 1.新建一个applicationContext.xml 2.把applicationContext.xml写到web.xml(整个web项目的启动文件)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!--4.SpringMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--5.Spring依赖的jackson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<!--6、Mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!--7、Mybatis 和Spring的整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring-version}</version>
</dependency>
<!--8、Mybatis需要依赖的数据库的驱动,Oracle,mysql-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-version}</version>
</dependency>
<!--9.c3p0是一个非常稳定的数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0-version}</version>
</dependency>
<!--10.jsp使用JSTL-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
三、5份配置文件
- web.xml
- 先保证web.xml为3.0以上的版本
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
</web-app>
加入Spring的支持
<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>
加入SpringMVC(分别是中央控制器和编码过滤器)
<!--SpringMVC的中央控制器-->
<servlet>
<servlet-name>springmvc-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符集编码过滤器,设置为UTF8-->
<!--相当于request.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2. db.properties
jdbc_driver=oracle.jdbc.driver.OracleDriver
jdbc_url=jdbc:oracle:thin:@192.168.88.88:1521:orcl
jdbc_username=oa
jdbc_password=123456
#oracle参考字符串
#oracle.jdbc.driver.OracleDriver
#jdbc:oracle:thin:@192.168.88.88:1521:orcl
#mysql参考字符串(注意6.0以上的jar包发生了变化)
#jdbc:mysql://localhost:3306/pyq?serverTimezone=UTC
#com.mysql.cj.jdbc.Driver
3.applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<context:component-scan base-package="com.nf"></context:component-scan>
<!--以下配置数据库相关的信息-->
<!-- 使用c3p0连接池注册数据源 -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基础配置 -->
<property name="jdbcUrl" value="${jdbc_url}"></property>
<property name="driverClass" value="${jdbc_driver}"></property>
<property name="user" value="${jdbc_username}"></property>
<property name="password" value="${jdbc_password}"></property>
<!-- 关键配置 -->
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"></property>
<!--连接池中保留的最小连接数。Default: 2 -->
<property name="minPoolSize" value="2"></property>
<!--连接池中保留的最大连接数。Default: 15 -->
<property name="maxPoolSize" value="15"></property>
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="1"></property>
<!-- 性能配置 -->
<!-- 控制数据源内加载的PreparedStatements数量。如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0 -->
<property name="maxStatements" value="8"></property>
<!-- maxStatementsPerConnection定义了连接池内单个连接所拥有的最大缓存statements数。Default: 0 -->ssm环境搭建