springmvc连接mysql_挺详细的spring+springmvc+mybatis配置整合|含源代码

本文详细介绍了如何配置SSM(spring、springmvc、mybatis)框架,包括新建Java Web项目、导入jar包、配置web.xml、applicationContext.xml、springmvc-servlet.xml以及mybatis配置文件。同时提供了各配置文件的代码片段,适合初学者参考。
摘要由CSDN通过智能技术生成
34fea41e0dcda1039209ca3987378b5f.png

大家好,我是雄雄,今天来带着大家来配置一下SSM(spring+springmvc+

mybatis)框架。

01

新建java web项目

直接在myeclipse中,新建一个web项目即可。

c52ac5b8e3441dcb6b219096fd309492.png

02

导入jar包

将SSM所需的jar包复制到项目的/WebRoot/WEB-INF/lib中,在这里我整理了下,大致需要34个jar文件,复制完之后,选中所有jar包,右击—Build Path-->Add to Build Path。

256b1c42a5157b3c8c9c15329d81a3b6.png

03

配置web.xml文件。

web.xml文件需要配置三部分信息,spring、springmvc以及解决springmvc传值乱码的过滤器,分别如下:

spring配置信息:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>

springmvc配置信息:

<servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
   <init-param>
     <param-name>contextConfigLocationparam-name>
     <param-value>classpath:springmvc-servlet.xmlparam-value>
   init-param>
   <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

设置编码格式的过滤器信息:


  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

04

配置Spring配置文件

applicationContext.xml文件,里面需要包含这些信息:用来连接数据库的数据源信息


  <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/schooldb">property>
    <property name="username" value="root">property>
    <property name="password" value="root">property>
  bean>

加载mybatis-config.xml文件以及sql映射文件的SqlSessionFactory:


  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource">property>
    <property name="configLocation" value="classpath:mybatis-config.xml">property>
    <property name="mapperLocations">
      <list>
        <value>classpath:org/dao/*.xmlvalue>
      list>
    property>
  bean>

Mapper注入映射器的MapperScannerConfigurer:


  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.dao">property>
  bean>

最后就是扫描注解的配置:


  <context:component-scan base-package="org.dao,org.service,org.web"/>
  <mvc:annotation-driven/>

05

配置springmvc的信息

springmvc-servlet.xml中需要包含最基本的两个部分,扫描注解和springmvc请求的前缀后缀设置:


  <context:component-scan base-package="org.web,org.dao,org.service"/>
  <mvc:annotation-driven/>
  
  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/">property>
    <property name="suffix" value=".jsp">property>
  bean>

06

配置mybatis配置文件

本文件简单点,就配置个别名和打印sql语句就行,都是可选的:

xml version="1.0" encoding="UTF-8" ?>
br mpa-from-tpl="t" />  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>
  
  <typeAliases>
    <package name="org.entity"/>
  typeAliases>
  
configuration>

附录:

spring配置文件(applicationContext.xml):

xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/p
  http://www.springframework.org/schema/p/spring-p-3.1.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd
  ">

  
  <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/schooldb">property>
    <property name="username" value="root">property>
    <property name="password" value="root">property>
  bean>
  
  
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource">property>
    <property name="configLocation" value="classpath:mybatis-config.xml">property>
    <property name="mapperLocations">
      <list>
        <value>classpath:org/dao/*.xmlvalue>
      list>
    property>
  bean>
  
  
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.dao">property>
  bean>
  
  
  <context:component-scan base-package="org.dao,org.service,org.web"/>
  <mvc:annotation-driven/>

beans>

springmvc配置文件(springmvc-servlet.xml):

xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  http://www.springframework.org/schema/p
  http://www.springframework.org/schema/p/spring-p-3.1.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.1.xsd
  ">
  
  
  <context:component-scan base-package="org.web,org.dao,org.service"/>
  <mvc:annotation-driven/>
  
  
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/">property>
    <property name="suffix" value=".jsp">property>
  bean>

beans>

web.xml文件:

xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
  <display-name>display-name>  
  <welcome-file-list>
    <welcome-file>index.jspwelcome-file>
  welcome-file-list>
  
  
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
   <init-param>
     <param-name>contextConfigLocationparam-name>
     <param-value>classpath:springmvc-servlet.xmlparam-value>
   init-param>
   <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
  
  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
    <init-param>
      <param-name>forceEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
  
web-app>

配置到此结束,明天带着大家实现一遍SSM的增删改查案例。

独家特制纯手工辣椒酱,小商店现在下单,单件商品立减1.88元,满80元减15元.

往期精彩

投资理财要趁早,基金风险是最小!

2021-01-10

cfb94793d2f13da804e1f939366bd79b.png

java中的泛型类型擦除

2021-01-12

aff17cfa038426a33c399ca3308c655e.png

一百馒头一百僧,大僧三个更无争,小僧三人分一个,大小和尚得几丁?

2021-01-09

5cd1aa026789680ed875b1a4e643382f.png

你们好好的学,回头教教我~

2021-01-08

126e264f6ee8c2f9172f92729aba49a3.png

辣椒酱中奖说明~

2021-01-07

b714ef02e18b3c83aa95fa3299ecf4be.png 295bb2a971047b1b6e9401cc9c9d3b2e.png e7eea4c7572ec1c487cb32576cac6711.png点分享 f553796c49442a8e7b7353cf5f6eede7.png点点赞 e00e087f7ceb3b2d315e9f07d2e884b5.png点在看
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值