ssm框架整合配置,用maven配置依赖jar包

时间:2024-12-02 09:05:25

1.创建maven project

首先在pom.xml中指定工程所依赖的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xk</groupId>
<artifactId>XXXXX</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>xyspgl Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.3.7.RELEASE</spring.version>
</properties>
<dependencies>
<!-- 单元测试所需jar包 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<!-- 日志jar包 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 4.Spring -->
<!-- 1)Spring核心 -->
<!-- 1)包含spring框架的核心工具类,spring其他组件都要使用到这个包里的类,是其他组件的基本核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 2)这个jar包是所有文件都要用到的,它包含访问配置文件、创建和管理bean以及进行(ioc/Di)
操作相关的所有类,如果应用只需要基本的(ioc/Di)操作,引入core与bean的jar包就足够了 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 3)这个jae包为spring核心提供了大量的扩展,可以找到使用spring ApplicationContext特性时所需的全部标签及所需要
的全部的类,instrumentation组件及校验Validation方面的相关类。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 二)Spring DAO层 -->
<!-- 4)这个jar文件包含 Spring 对JDBC 数据访问进行封装的所有类 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 5)为JDBC、Hibernat、JDO、JPA 等提供的一致的声明式和编程式 事务管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 三)Spring web -->
<!-- 6)Spring web包含web应用开发时,用到spring框架时所需的核心类,包括自动载入WebApplicationContext -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 7)包含SpringMVC框架相关的所有类。 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 四)Spring test -->
<!-- 8)Spring test 对JUNIT等测试框架的简单封装 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency> <!-- Servlet web -->
<!-- servlet-api主要用来提供servlet服务的 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- json解析 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<!-- Map工具类 对标准java Collection的扩展spring-core.jar需commons-collections.jar -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency> <!-- DAO: MyBatis所有基础类库 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!-- mybatis-spring整合包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency> <!-- 2.数据库 -->
<!-- mysql-connector-java用来支持JDBC与mysql的交互 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<!-- 连接池所需jar包 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
<build>
<finalName>xyspgl</finalName>
<!-- 配置编译依赖工具 -->
<plugins>
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

2.接着在jdbc.properties文件中指定了数据库的连接方式

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

3.接着创建mybatis的核心配置文件mybaits-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性 -->
<settings>
<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值 -->
<setting name="useGeneratedKeys" value="true"/> <!-- 使用列标签(数据库列名)替换列别名(查询语句中给查询的字段自定义的名称) 默认:true -->
<setting name="useColumnLabel" value="true"/> <!-- 开启驼峰命名转换:Table(creatr_time) -> Entity(creatTime) -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.xk.XXXX.entity" />
</typeAliases> </configuration>

4.接着创建spring-dao.xml文件,将jdbc.properties和mybaits-config.xml载入,创建dataSource连接池、配置 通过用mybatis与数据库交互 的方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 配置整合mybatis过程 -->
<!-- 1.配置数据相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 2.C3P0数据库连接池 -->
<bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 引入属性文件的值 -->
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property> <!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="10"/>
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false"/>
<!-- 获取连接超时时间 -->
<property name="checkoutTimeout" value="1000"/>
<!-- 当获取连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2"></property>
</bean> <!-- 3.配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:mybaits-config.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean> <!-- 4.配置扫描Dao接口,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.xk.XXXX.dao"></property>
</bean>
</beans>

5.接着创建spring-service.xml这个配置文件,这里其作用主要是做事务管理的,将在spring-dao中配置的datesource注入到事务管理器中 对我们的service层进行操作

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" 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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 扫描service包下所有使用注解的类型 -->
<context:component-scan base-package="com.xk.XXXX.service"/> <!-- 配置事务管理器=============================== -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
</bean> <!-- 开启注解事务=============== -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

6.创建spring-web.xml,这个文件主要是定义DispatcherServlet如何去响应URL请求,也就是定义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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 配置spring MVC -->
<!-- 1.开启springmvc注解模式 -->
<mvc:annotation-driven/> <!-- 2.静态资源默认servlet配置
(1)加入对静态资源的处理:js/gif/png
(2)允许使用"/"做整体映射
-->
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler/> <!-- 3.定义视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置逻辑视图前后缀 -->
<property name="prefix" value="/WEB-INF/html/"></property>
<property name="suffix" value=".html"></property>
</bean> <!-- 4.扫描web相关的bean -->
<context:component-scan base-package="top.lyfan.web"/>
</beans>

7.最后在web.xml文件里将我们的springmvc-dispatcher给当做一个servlet注册到里面去,来响应前端请求,同时将三个spring打头的三个xml配置文件注册进来,整合到一起,

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true">
<display-name>Archetype Created Web Application</display-name> <welcome-file-list>
<!-- 设置默认访问的页面 aaa找不到或不存在就会去找index.jsp -->
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 指定Spring配置文件 -->
<!-- Spring的配置文件的默认路径是/WEB-INF/${servlet-name}-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 将三个Spring配置文件整合到一起,这样三个spring配置文件也能相互读取到彼此内的东西 -->
<param-value>classpath:spring/spring-*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc-dispatcher</servlet-name>
<!-- 指定匹配规则,默认将所有url请求都会转入到DispatcherServlet中去处理, 在spring-web中已经过滤掉一些请求了,因此这里可以直接这样配(默认匹配所有请求) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

到这,整合完毕。