mybatis支持oracle嗎,springmvc連接mysql_挺詳細的spring+springmvc+mybatis配置整合|含源代碼

 2023-12-06 阅读 32 评论 0

摘要:大家好,我是雄雄,今天來帶著大家來配置一下SSM(spring+springmvc+mybatis)框架。01新建java web項目直接在myeclipse中,新建一個web項目即可。02導入jar包mybatis支持oracle嗎?將SSM所需的jar包復制到項目的/WebRoot/WEB-INF/lib中,在這
34fea41e0dcda1039209ca3987378b5f.png

大家好,我是雄雄,今天來帶著大家來配置一下SSM(spring+springmvc+

mybatis)框架。

01

新建java web項目

直接在myeclipse中,新建一個web項目即可。

c52ac5b8e3441dcb6b219096fd309492.png

02

導入jar包

mybatis支持oracle嗎?將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

javaweb項目連接MySQL數據庫。配置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的信息

springboot前端用什么。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>

spring jdbc、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中的泛型類型擦除

maven java,2021-01-12

aff17cfa038426a33c399ca3308c655e.png

一百饅頭一百僧,大僧三個更無爭,小僧三人分一個,大小和尚得幾丁?

2021-01-09

5cd1aa026789680ed875b1a4e643382f.png

你們好好的學,回頭教教我~

2021-01-08

126e264f6ee8c2f9172f92729aba49a3.png

辣椒醬中獎說明~

spring ssm。2021-01-07

b714ef02e18b3c83aa95fa3299ecf4be.png 295bb2a971047b1b6e9401cc9c9d3b2e.pnge7eea4c7572ec1c487cb32576cac6711.png點分享f553796c49442a8e7b7353cf5f6eede7.png點點贊e00e087f7ceb3b2d315e9f07d2e884b5.png點在看

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/188506.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息