微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SSM初级整合Spring,SpringMVC,Mybatis

SSM初级整合Spring,SpringMVC,Mybatis

1、创建JavaWeb项目&导入jar包

在这里插入图片描述

在这里插入图片描述

这里只是整合相关的包,实际中还需要JavaWeb所需的一些包:
servlet-api(可以通过添加Tomcat的包来添加
jsp-api(可以通过添加Tomcat的包来添加
Jstl

2、整合配置SpringMVC
2.1、创建配置文件springMVC-servlet.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:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd”>
<mvc:annotation-driven />

<context:component-scan base-package=“com.gx.controller”/>

<!--配置视图解析器 使用JSP-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--视图路径前缀 /WEB-INF/jsp/目录-->
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <!--视图的后缀 JSP的后缀为.jsp-->
    <property name="suffix" value=".jsp"/>
</bean>

<!--配置静态只有映射-->
<mvc:resources location="/static/" mapping="/static/**"/>

<!--文件上传配置 注意id必须是 multipartResolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--文件上传总大小的最大值,单位字节   100M=100*1024*1024 -->
    <property name="maxInMemorySize" value="104857600"/>
    <!-- 单个上传文件大小,单位字节 10M=10*1024*1024-->
    <property name="maxUploadSizePerFile" value="10485760"/>
    <!--编码方式-->
    <property name="defaultEncoding" value="UTF-8"/>
    <!--当 resolveLazily为false(认)时,会立即调用 parseRequest() 方法对请求数据进行解析,
    然后将解析结果封装到 DefaultMultipartHttpServletRequest中;
    而当resolveLazily为 true时,会在DefaultMultipartHttpServletRequest的initializeMultipart()方法调用parseRequest()方法对请求数据进行解析,
    而initializeMultipart()方法又是被getmultipartfiles()方法调用,
    即当需要获取文件信息时才会去解析请求数据,这种方式用了懒加载的思想。-->
    <property name="resolveLazily" value="true"/>
</bean>
2.2、在web/WEB-INF/web.xml中添加SpringMVC相关配置: <?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.jsp</welcome-file>
</welcome-file-list>

<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>

<filter>
    <filter-name>SpringMvcCharacterEncodingFilter</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>SpringMvcCharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<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-servlet.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>
3、整合配置Spring

3.1、在src下添加applicationContext.xml配置文件,并添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
context:component-scanbase-package=“com.gx.dao,com.gx.service”/

3.2、在项目启动时加载Sping的配置文件INF/web.xml中添加配置:
< context-param>
< param-name>contextConfigLocation
< param-value>classpath:applicationContext.xml
< /context-param>

< listener>
< listener-class>org.springframework.web.context.ContextLoaderListener< /listener-class>
< /listener>
4、整合配置MyBatis
4.1、在src下添加jdbc.properties配置文件,配置数据库的连接信息:
jdbc.driver=com.MysqL.cj.jdbc.Driver
jdbc.url=jdbc:MysqL://localhost:3306/base_admin?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
jdbc.username=root
jdbc.password=root
4.2、在src下添加mybatis-conf.xml配置文件,配置需要单独配置的MyBatis的设置:

<?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>

< setting name=“logImpl” value=“STDOUT_LOGGING”/>
< /settings>
< /configuration>
4.3、在Spring的配置文件applicationContext.xml中整合Spring和MyBatis:完整代码

<?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:aop=“http://www.springframework.org/schema/aop”
xmlns:tx=“http://www.springframework.org/schema/tx”
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<!--1、开启注解扫描 扫描dao层和service层-->
<context:component-scan base-package="com.gx.dao,com.gx.service">
    <context:include-filter expression="org.springframework.stereotype.Service" type="annotation"/>
    <context:include-filter expression="org.springframework.stereotype.Repository" type="annotation"/>
</context:component-scan>

<!--加载jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--2、配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!--数据库驱动类名-->
    <property name="driverClassName" value="${jdbc.driver}"/>
    <!--数据库连接字符串 如果直接写在xml中,&符号需要使用&amp;来转义
    jdbc:MysqL://localhost:3306/base_admin?characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;useSSL=false
    -->
    <property name="url" value="${jdbc.url}"/>
    <!--数据库用户名和密码  ${username} 获得到的是系统的用户名-->
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<!--3、MyBatis配置-->
<!--sqlSessionFactory   mybatis-spring-2.x.x.jar-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.sqlSessionfactorybean">
    <!--指定MyBatis配置文件-->
    <property name="configLocation" value="classpath:mybatis-conf.xml"/>
    <!--数据源-->
    <property name="dataSource" ref="dataSource"/>
    <!--配置Mapper.xml的路径-->
    <property name="mapperLocations" value="classpath:com/gx/mappers/*.xml"/>
</bean>

<!--指定 dao接口所在的包,Spring会自动查找类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!--指定sqlSessionFactory bean的id-->
    <property name="sqlSessionfactorybeanName" value="sqlSessionFactory"/>
    <!--指定dao接口的包名-->
    <property name="basePackage" value="com.gx.dao"/>
</bean>

<!--4、事务管理配置-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!--事务管理器管理的数据源-->
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--配置事务通知属性-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
< tx:attributes>< !--propagation:事务传播属性 指定当前方法必需在事务环境中运行,如果当前有事务环境就加入当前正在执行的事务环境,如果当前没有事务,就新建一个事务。这是认值。-->
        <tx:method name="insert*" propagation="required"/>
        <tx:method name="update*" propagation="required"/>
        <tx:method name="delete*" propagation="required"/>
        <tx:method name="authorize*" propagation="required"/><!--授权方法-->
        <tx:method name="select*" propagation="required" read-only="true"/>
        <tx:method name="get*" propagation="required" read-only="true"/>
        <tx:method name="search*" propagation="required" read-only="true"/>
        <tx:method name="find*" propagation="required" read-only="true"/>
        <tx:method name="load*" propagation="required" read-only="true"/>
        <tx:method name="*" propagation="required" read-only="true"/>
    </tx:attributes>
</tx:advice>

<!--配置事务AOP-->
<aop:config>
    <!--配置切入点-->
    <aop:pointcut id="mypointcut" expression="(execution(* com.gx.service..*Impl.*(..)))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
</aop:config>

5、添加log4j配置
在src目录下添加log4j.properties:
#配置根 Logger
log4j.rootLogger=INFO , appender1
#配置日志信息输出目的地(appender)
#appender1 输出到控制台
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender1.Threshold=INFO
log4j.appender.appender1.Target=System.out
log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1}:%L %5p - %m%n

原文地址:https://www.jb51.cc/wenti/3285379.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐