SSM(Spring、SpringMVC、Mybatis)整合详细配置

创建maven项目,导入web支持。

Spring官网:配置文件中需导入的相关约束
maven项目:需要导入的依赖

项目基本框架:

  • com
    • yt
      • bookstore
        • controller
        • dao
        • pojo
        • service
  • classpath:
    • mappers
      • BookMapper.xml
    • applicationContext-dao.xml
    • applicationContext-service.xml
    • database.properties
    • mybatis-config.xml
    • spring-mvc.xml
  1. 导入相关依赖
 <dependencies>

        <!--Junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        
	 <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

        <!-- 数据库连接池  c3p0   dbcp-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

        <!--Servlet - JSP -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
          <!--JSP标准标签库 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!--Mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.1</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

<!--静态资源导出问题-->
<!--    Maven资源过滤设置-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
   </build>
  1. mybatis-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>

</configuration>
  1. 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

</beans>
  1. spring-mvc.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: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
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解驱动 -->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置-->
    <mvc:default-servlet-handler/>

    <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.yt.bookstore.controller" />

</beans>
  1. 编写数据库配置文件database.properties
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&serverTimezone=GMT%2B8 &useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

mysql关于时区的设置:

上海时间:serverTimezone=Asia/HongKong
北京时间:serverTimezone=GMT%2B8 
如果设定serverTimezone=UTC,会比实际时间早8个小时
如果在中国,可以选择东8区的Hongkong、Asia/Shanghai或者Asia/Hongkong
  1. 编写mybatis核心配置文件:mybatis-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>
    <!--    配置别名-->
    <typeAliases>
        <package name="com.yt.bookstore.pojo"/>
    </typeAliases>
   <!-- 若*Mapper.xml文件较少,可用以下方式一个一个引入,告诉Mabitis到哪去找映射文件-->
<!--    <mappers>
        <mapper resource="mapper/BookMapper.xml"/>
    </mappers>
-->

</configuration>

关于mapper映射文件的配置:
* 使用相对于类路径的资源引用

<mappers>  
	<!-- BookMapper.xml可以和接口放在一起 -->
	<mapper resource="com/yt/bookstore/dao/BookMapper.xml"/>
	<!-- 也可放在classpath下,及项目的resources目录下 -->
	<mapper resource="mappers/BookMapper.xml"/>  
</mappers>

* 使用完全限定资源定位符(URL)

<mappers>
	<mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>

* 使用映射器接口实现类的完全限定类名
通过class属性指定mapper接口名称,此时对应的映射文件必须与接口位于同一路径下,并且名称相同

<mappers>
	<mapper class="com.yt.bookstore.dao.BookMapper"/>
</mappers>

* 通过name属性指定mapper接口所在的包名 ,此时对应的映射文件必须与接口位于同一路径下,并且名称相同,将包内的映射器接口实现全部注册为映射器但是需要配置文件名称和接口名称一致,并且位于同一目录下

<mappers>
	<package name="com.yt.bookstore.dao"/>
</mappers>

若要批量注册classpath:mappers内所有的Mapper.xml,则在spring-dao.xml配置文件的相应位置加入以下代码(在配置sqlSessionFactory的位置加入)

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
  1. 编写与接口对应的 Mapper.xml 文件
    约束为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<mapper namespace="com.yt.bookstore.dao.BookMapper">

</mapper>
  1. 编写Spring整合Mybatis的相关的配置文件: applicationContext-dao.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:context="http://www.springframework.org/schema/context"
       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">

    <!-- 配置整合mybatis -->
    <!-- 1.关联数据库文件 -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- 2.数据库连接池 -->
    <!--数据库连接池
dbcp  半自动化操作  不能自动连接
c3p0  自动化操作(自动的加载配置文件 并且设置到对象里面)
    -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<!-- 配置连接池属性 -->
	<property name="driverClass" value="${jdbc.driver}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>
	
	<!-- c3p0连接池的私有属性 -->
	<property name="maxPoolSize" value="30"/>
	<property name="minPoolSize" value="10"/>
	<!-- 关闭连接后不自动commit -->
	<property name="autoCommitOnClose" value="false"/>
	<!-- 获取连接超时时间 -->
	<property name="checkoutTimeout" value="10000"/>
	<!-- 当获取连接失败重试次数 -->
	<property name="acquireRetryAttempts" value="2"/>
    </bean>
	
    <!-- 3.配置SqlSessionFactory对象 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<!-- 注入数据库连接池 -->
	<property name="dataSource" ref="dataSource"/>
	<!-- 注册所有的Mapper.xml-->
	<property name="mapperLocations" value="classpath:/mappers/*.xml"/>
	<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
	<property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>

    <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
    <!--解释 : https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 注入sqlSessionFactory -->
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	<!-- 给出需要扫描的Dao接口包 -->
	<property name="basePackage" value="com.yt.bookstore.dao"/>
    </bean>

</beans>
  1. spring整合service层:applicationContext-sevice.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:context="http://www.springframework.org/schema/context"
       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">

    <!-- 扫描service相关的bean -->
    <context:component-scan base-package="com.yt.service" />

   <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 注入数据库连接池 -->
	<property name="dataSource" ref="dataSource" />
    </bean>
    
    <!--    配置事务的通知-->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
	<!-- 配置事务的属性-->
	<tx:attributes>
       <!-- name:方法名-->    
           <tx:method name="*" propagation="REQUIRED" read-only="false"/>
           <!--优先级别更高-->    
           <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
       </tx:attributes>
   </tx:advice>

<!--配置AOP-->
   <aop:config>
	<!-- 配置切入点表达式-->
	<aop:pointcut id="txPointCut" expression="execution(* com.yt.bookstore.service.impl.*.*(..))"/>
	<!--    建立切入点表达式和事务通知的对应关系-->
	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor>

   </aop:config>

</beans>
  1. 编写web.xml文件
<?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">
<!--    设置首页-->
    <welcome-file-list>
        <welcome-file>/index.do</welcome-file>
    </welcome-file-list>
    <!-- 配置应用监听器 :
             做一些初始化的内容添加工作、设置一些基本的内容、比如一些参数或者是一些固定的对象等等
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 <!--配置spring配置文件-->
   <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/applicationContext-*.xml</param-value>
   </context-param>
 <!--注册Servlet-->
    <!--DispatcherServlet :作用是将请求分发到不同的处理器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--encodingFilter:乱码过滤-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!--Session过期时间
    <session-config>
        <session-timeout>15</session-timeout>
    </session-config>
    -->
</web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值