SSM框架整合——环境搭建

1.文件组织结构

 

 

2.所需jar包:

链接:链接:https://share.weiyun.com/51uCJBc

 

3.配置文件

3.1数据库常量配置文件db.properties:

jdbc.driver = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/
jdbc.username =
jdbc.password =
jdbc.maxTotal =
jdbc.maxIdle =
jdbc.initialSize =

3.2Spring配置文件applicationContext-db.xml:

  首先定义了读取db.properties文件的配置和数据源配置,然后配置了事务管理器并开启了事务注解。接下来配置了用于整合MyBatis框架的MyBatis工厂信息,最后定义了mapper扫描器DAO层以及Service层的配置。

<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 读取db.properties-->
<context:property-placeholder location="classpath:../config/db.properties"/>
<!-- 配置数据源-->
<bean id="dateSource" class="org.apache.commons.dbcp2.BasicDataSource">
<!-- 数据库驱动-->
<property name="driverClassName" value="${jdbc.driver}"/>
<!-- 连接数据库的url-->
<property name="url" value="${jdbc.url}"/>
<!-- 连接数据库的用户名-->
<property name="username" value="${jdbc.username}"/>
<!-- 连接数据库的密码-->
<property name="password" value="${jdbc.password}"/>
<!-- 最大连接数-->
<property name="maxTotal" value="${jdbc.maxTotal}"/>
<!-- 最大空闲数-->
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!-- 初始化连接数-->
<property name="initialSize" value="${jdbc.initialSize}"/>
</bean>
<!-- 事务管理器,依赖于数据源-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"/>
</bean>
<!-- 开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置MyBatis工厂sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源-->
<property name="dataSource" ref="dateSource"/>
<!-- 指定MyBatis核心配置文件位置-->
<property name="configLocation" value="classpath:../config/mybatis-config.xml"/>
</bean>
<!-- 配置mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"/>
</bean>
<!-- 扫描Service-->
<context:component-scan base-package="service"/>
</beans>

3.3MyBatis配置文件mybatis-config.xml:

  由于在Spring中已经配置了数据源信息以及mapper接口文件扫描器,所以在MyBatis的配置文件中只需要根据POJO类路径进行别名配置即可。

<?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>
<!-- 定义别名-->
<typeAliases>
<package name="po"/>
</typeAliases>
</configuration>

3.4Spring配置文件springmvc-config.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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 配置包扫描器,扫描@Controller注解的类-->
<context:component-scan base-package="web.controller"/>
<!-- 加载注解驱动-->
<mvc:annotation-driven/>
<!-- 配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

3.5web.xml:

  配置Spring的文件监听器、编码过滤器以及Spring MVC的前端控制等信息。

<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
version="4.0">
<!-- 配置加载Spring文件的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:../config/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 编码过滤器-->
<filter>
<filter-name>encoding</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>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器-->
<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:../config/applicationContext.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /:拦截所有请求(除了jsp)-->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

转载于:https://www.cnblogs.com/-StarrySky-/p/11601879.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值