springmvc连接mysql_挺详细的spring+springmvc+mybatis配置整合|含源代码
大家好,我是雄雄,今天来带着大家来配置一下SSM(spring+springmvc+
mybatis)框架。
01
新建java web项目
直接在myeclipse中,新建一个web项目即可。
02
导入jar包
将SSM所需的jar包复制到项目的/WebRoot/WEB-INF/lib中,在这里我整理了下,大致需要34个jar文件,复制完之后,选中所有jar包,右击—Build Path-->Add to Build Path。
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
java中的泛型类型擦除
2021-01-12
一百馒头一百僧,大僧三个更无争,小僧三人分一个,大小和尚得几丁?
2021-01-09
你们好好的学,回头教教我~
2021-01-08
辣椒酱中奖说明~
2021-01-07
点分享点点赞点在看
springmvc连接mysql_挺详细的spring+springmvc+mybatis配置整合|含源代码相关推荐
- Spring系列(七)、Spring与MyBatis框架整合
7 搭建Spring与MyBatis的集成环境 要实现Spring与MyBatis的整合,很明显需要这两个框架各自的jar包,以及整合两个框架的中间包mybatis-spring.jar: 我们使用m ...
- 最详细的Spring+SpringMVC+Mybatis框架整合及mybatis分页讲解,适合初级者
最详细的关于idea整合ssm框架讲解 一个关于brand(品牌)的项目 [ssm框架搭建源代码及mysql数据库数据]链接:https://pan.baidu.com/s/1eBogklK0rFLj ...
- 餐饮收银管理系统如何连接mysql_基于jsp+mysql+Spring+mybatis的SSM餐厅点餐收银管理系统...
运行环境: 最好是java jdk 1.8,我们在这个平台上运行的.其他版本理论上也可以. IDE环境: Eclipse,Myeclipse,IDEA都可以 tomcat环境: 最好是Tomcat 7 ...
- Spring Boot + Mybatis 快速整合
引言 最近在工作结束后抽时间学习了一下mybatis的知识,因为之前有学习过,但是经久不用,也未曾踏实地整理,因此有所淡忘. super meeting会议管理系统是我厂最近开发的一套会议预约平台.持 ...
- Spring和MyBatis环境整合
2019独角兽企业重金招聘Python工程师标准>>> Spring: Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. 两个重要模块:Spring 面向 ...
- Spring Boot MyBatis配置多种数据库
mybatis-config.xml是支持配置多种数据库的,本文将介绍在Spring Boot中使用配置类来配置. 1. 配置application.yml # mybatis配置 mybatis:c ...
- spring boot mybatis 整合_两大热门框架 Spring 与 Mybatis 如何整合呢?
整合的方式 新建 maven 项目 引入依赖包 配置资源文件 案例实操 新建 maven 项目 新建 maven 项目 spring_mybatis 目录结构如下: 主目录包: com.xxx.d ...
- Spring Boot——MyBatis配置带下划线命名的字段自动转换驼峰命名解决方案
问题描述 MyBatis无法查询出属性名和数据库字段名不完全相同的数据. 即:属性名和数据库字段名分别为驼峰命名和下划线命名时查出的数据为NULL. 问题分析 MyBatis默认是属性名和数据库字段名 ...
- SpringMVC、Spring、Mybatis框架整合及使用
首先需要创建一个动态web工程 引入所需的jar包 创建所需要的数据库表,并插入数据 1 CREATE DATABASE how2java; 2 USE how2java; 3 4 CREATE TA ...
最新文章
- nacos项目搭建(服务提供者,服务消费者)
- 记录一下自己在区块链领域创业的经过
- tomcat7 https 拒绝连接_Ubuntu上运行Docker提示权限拒绝,如何处理?
- opengl加载显示3D模型blend类型文件
- 微信公众号文章中图片加载时,占位图宽高大小的确定
- [转]【高并发】高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!
- 安装codeblocks和wxwidgets及opencv
- win10+cuda10.0.130+cudnn7.5.1+tensorflow-gpu 1.13.1+anaconda3+keras+pycharm2018
- 在 vue/cli 中使用 Module Federation
- 认识XP下的NetBEUI
- Solaris系统cron服务异常解决记录
- 宫颈癌预测--随机森林
- 服务器网卡相关知识点
- Git(七)——删除历史版本,保留当前状态
- 第2关:比较、掩码和布尔逻辑
- 专访李亚锋:“大数据+”趋势下的电信实践之路
- 2020年中高级Android面试秘籍(Android高级篇-3)
- 与其说项羽败给刘邦,还不如说他输给了人情
- linux应用开发大杂烩(你不一定都知道的细枝末节)
- 英勇的战士——斯巴达
热门文章
- c 获取char*的长度_C/C++编程笔记:C语言字符串比较函数,超详细,值得收藏!...
- 情人节表白(持续更新,欢迎收藏)
- C++之默认参数顺序(从右到左)和调用顺序(从左到右)
- 解决: 您目前无法访问 因为此网站使用了 HSTS。网络错误和攻击通常是暂时的,因此,此网页稍后可能会恢复正常。
- Fuchsia之GN与Ninja构建hello world
- Android串口控制台改为root
- Android Binder 分析——匿名共享内存(好文)
- 这世上最快的捷径就是脚踏实地
- SpringBoot之idea打包以及启动jar包
- html怎么用div从左到右,单独使用CSS,你怎么能有一个从右到左的边框底部渐变?...