SSM(Sping+SpringMVC+MyBatis)搭建与配置

使用Idea新建一个简单的web项目,***项目结构***如下:
项目结构
导包
jar包

1.Spring与MyBatis集成

1.1 数据库配置文件

jdbc.properites

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=147258

1.2 Spring核心配置文件

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
">
    <!--
     db.properties -> datasource -> SqlSessionFactory ->mapper(接口,xml)->service->controller
      Spring集成MyBatis (MyBatis操作数据库的对象交给Spring管理)
         1.需要把SqlSessionFactory让Spring来管理
         2.数据源(四大金刚) 3.别名的问题  4.可以找到mapper.xml
    -->
    <context:component-scan base-package="cn.dieu.ssm.service" />

    <!--引入外部的db.properties-->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!--配置dbcp连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource" />
        <!--配置别名-->
        <property name="typeAliasesPackage" value="cn.dieu.ssm.domain" />
        <!--扫描 mapper.xml-->
        <property name="mapperLocations" value="classpath:cn/dieu/ssm/mapper/*.xml" />
    </bean>

    <!--配置对应的MapperBean:只能配置一个Mapper,太多的话就很麻烦-->
    <!--
    <bean id="departmentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
      <property name="sqlSessionFactory" ref="sessionFactory" />
      <property name="mapperInterface" value="cn.dieu.ssm.mapper.ProductDirMapper" />
    </bean>
    -->

    <!--一劳永逸的配置Mapper的方案-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描的包的配置-->
        <property name="basePackage" value="cn.dieu.ssm.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sessionFactory" />
    </bean>

    <!--配置一个事务对象-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!--事务的注解支持-->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

2.Spring与SpringMVC集成

2.1 SpringMVC核心配置文件

applicationContext-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!--扫描相应的controller-->
    <context:component-scan base-package="cn.dieu.ssm.controller" />
    <!--静态资源放行-->
    <mvc:default-servlet-handler />
    <!--springMVC的注解支持-->
    <mvc:annotation-driven />
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!--
        在SpringMVC中引入Spring的配置文件,是可以集成运行的,但是后期可能无法集成其他框架(shiro)
       -->
    <!--<import resource="classpath:applicationContext.xml" />-->
</beans>

Web项目配置文件

当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等)。首先会去读取web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常的被启动起来。
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!-- 解决编码问题 -->
    <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>

    <!--读取Spring的核心配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!--直接启动Spring-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--配置核心控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--SpringMVC的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-mvc.xml</param-value>
        </init-param>
        <!--随着tomcat启动而启动-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值