Spring+SpringMVC+Mybatis项目搭建 + gitHub 源码

本文介绍了一个基于SpringMVC+Spring+MyBatis的项目的部署配置过程,包括applicationContext.xml中对数据源、SqlSessionFactory及Mapper扫描配置,springmvc.xml中的注解驱动、组件扫描及视图解析器配置,以及web.xml中的上下文监听、DispatcherServlet配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. Springmvc + spring + mybatis的部署
    1. 配置applicationContext
    2. <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:mybatis-spring="http://mybatis.org/schema/mybatis-spring"

      xmlns:jdbc="http://www.springframework.org/schema/jdbc"

      xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd

      http://mybatis.org/schema/mybatis-springhttp://mybatis.org/schema/mybatis-spring-1.2.xsd

      http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsd

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">

       

      <context:component-scan base-package="com.hand.ssm"></context:component-scan>

       

      <context:property-placeholder location="classpath:/db.properties" />

       

      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>

      <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"></property>

      <property name="username" value="root"></property>

      <property name="password" value="root"></property>

      </bean>

       

      <!--         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

      <property name="driverClass" value="${jdbc.driverClass}"></property>

      <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

      <property name="user" value="${jdbc.user}"></property>

      <property name="password" value="${jdbc.password}"></property>

      </bean> -->

       

      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dataSource"></property>

      <property name="mapperLocations" value="classpath:com/hand/ssm/dao/*.xml"></property>

      </bean>

       

      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

      <property name="basePackage" value="com.hand.ssm.dao"></property>

      <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

      </bean>

       

      </beans>

    3.  配置springmvc.xml
    4. <?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

      http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsd

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">

       

      <mvc:annotation-driven></mvc:annotation-driven>

       

      <context:component-scan base-package="com.hand.ssm.controller"></context:component-scan>

       

      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <property name="prefix" value="/WEB-INF/"></property>

      <property name="suffix" value=".jsp"></property>

      </bean>

       

      </beans>

 

  1. 配置web.xml
  2. <?xml version="1.0" encoding="UTF-8"?>

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

      <display-name>Archetype Created Web Application</display-name>

     

    <!-- needed for ContextLoaderListener -->

    <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:conf/applicationContext.xml</param-value>

    </context-param>

     

    <!-- Bootstraps the root web application context before servlet initialization -->

    <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

     

      <!-- The front controller of this Spring Web application, responsible for handling all application requests -->

    <servlet>

    <servlet-name>springDispatcherServlet</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:conf/springmvc.xml</param-value>

    </init-param>

    <load-on-startup>1</load-on-startup>

    </servlet>

     

    <!-- Map all requests to the DispatcherServlet for handling -->

    <servlet-mapping>

    <servlet-name>springDispatcherServlet</servlet-name>

    <url-pattern>/</url-pattern>

    </servlet-mapping>

    </web-app>

 

  1. 配置文件db.properties(这里如果使用配置文件有问题的话,可以自行百度解决办法)
  2. jdbc.driverClass=com.mysql.jdbc.Driver

    jdbc.jdbcUrl=jdbc:mysql://localhost:3306/sakila?characterEncoding=utf8

    jdbc.user=root

    jdbc.password=123456

  3. 查看完整项目代码 https://github.com/zpf2012/SpringMybatisSpringMVC
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值