大家好,我是雄雄,今天來帶著大家來配置一下SSM(spring+springmvc+
mybatis)框架。
新建java web項目
直接在myeclipse中,新建一個web項目即可。
導入jar包
mybatis支持oracle嗎?將SSM所需的jar包復制到項目的/WebRoot/WEB-INF/lib中,在這里我整理了下,大致需要34個jar文件,復制完之后,選中所有jar包,右擊—Build Path-->Add to Build Path。
配置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>
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/>
配置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>
配置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
java中的泛型類型擦除
maven java,2021-01-12
一百饅頭一百僧,大僧三個更無爭,小僧三人分一個,大小和尚得幾丁?
2021-01-09
你們好好的學,回頭教教我~
2021-01-08
辣椒醬中獎說明~
spring ssm。2021-01-07
點分享點點贊點在看版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态