ssm框架整合思路及代码
admin
2023-06-05 22:02:17
0

1、Dao层:

mybatis整合spring,通过spring创建数据库连接池,管理SqlSessionFactorymapper代理对象。需要mybatisspring的整合包

 

创建SqlMapConfig.xml配置文件

xml version="1.0"encoding="UTF-8"?>

DOCTYPE configuration

           PUBLIC"-//mybatis.org//DTD Config 3.0//EN"

           "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

 

configuration>

创建applicationContext-dao.xml

<beansxmlns="http://www.springframework.org/schema/beans"

      xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"

      xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd

      http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

      http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">

 

     

     

      <context:property-placeholderlocation="classpath:properties/*.properties"/>

     

      <bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"

           destroy-method="close">

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

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

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

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

           <property name="maxActive"value="10"/>

           <property name="minIdle"value="5"/>

      bean>

     

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

          

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

          

           <property name="configLocation"value="classpath:mybatis/SqlMapConfig.xml"/>

      bean>

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

           <property name="basePackage"value="mapper代理对象所在包的全路径"/>

      bean>

beans>

为了便于更改数据库,编写db.properties文件

jdbc.driver=数据库驱动

jdbc.url=jdbc:mysql://localhost:3306/数据库名称?characterEncoding=utf-8

jdbc.username=用户名

jdbc.password=密码

 

 

2、Service层:

所有的service实现类都放到spring容器中管理。由spring管理实务。

创建applicationContext-service.xml

xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"

      xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd

      http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-4.2.xsd

      http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">

 

      <context:component-scanbase-package="实现类所在的包的全路径 ">context:component-scan>

beans>

创建applicationContext-trans.xml

<beans xmlns="http://www.springframework.org/schema/beans"

      xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"

      xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd

      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.2.xsd

      http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

      http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsd">

     

      <bean id="transactionManager"

           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

          

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

      bean>

     

      <tx:advice id="txAdvice"transaction-manager="transactionManager">

           <tx:attributes>

                

                 <tx:method name="save*"propagation="REQUIRED"/>

                 <tx:method name="insert*"propagation="REQUIRED"/>

                 <tx:method name="add*"propagation="REQUIRED"/>

                 <tx:method name="create*"propagation="REQUIRED"/>

                 <tx:method name="delete*"propagation="REQUIRED"/>

                 <tx:method name="update*"propagation="REQUIRED"/>

                 <tx:method name="find*"propagation="SUPPORTS"read-only="true"/>

                 <tx:method name="select*"propagation="SUPPORTS"read-only="true"/>

                 <tx:method name="get*"propagation="SUPPORTS"read-only="true"/>

           tx:attributes>

      tx:advice>

     

      <aop:config>

           <aop:advisoradvice-ref="txAdvice"

                 pointcut="切入点表达式" />

      aop:config>

beans>

 

为了加载spring容器,在Web.Xml中编写如下

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      id="WebApp_ID" version="2.5">

      <display-name>名字display-name>

     

      <context-param>

           <param-name>contextConfigLocationparam-name>

           <param-value>classpath:spring/applicationContext*.xmlparam-value>

      context-param>

      <listener>

           <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>

      listener>

web-app>

 

3、表现层:

Springmvc框架,由springmvc管理controller

创建Springmvc.xml

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:p="http://www.springframework.org/schema/p"

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

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

      xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsd

       http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

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

 

      <context:component-scanbase-package="com.taotao.controller"/>

      <mvc:annotation-driven/>

      <bean

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

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

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

      bean>   

beans>

为了加载前端控制器(DispatcherServlet),在web.xml中编写如下

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

      id="WebApp_ID" version="2.5">

      <display-name>名字display-name>

      <welcome-file-list>

           <welcome-file>欢迎页welcome-file>

      welcome-file-list>

     

      <filter>

           <filter-name>CharacterEncodingFilterfilter-name>

           <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>

           <init-param>

                 <param-name>encodingparam-name>

                 <param-value>utf-8param-value>

           init-param>

      filter>

      <filter-mapping>

           <filter-name>CharacterEncodingFilterfilter-name>

           <url-pattern>/*url-pattern>

      filter-mapping>

 

 

     

      <servlet>

           <servlet-name>名字servlet-name>

           <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>

          

           <init-param>

                 <param-name>contextConfigLocationparam-name>

                 <param-value>classpath:spring/springmvc.xmlparam-value>

           init-param>

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

      servlet>

      <servlet-mapping>

           <servlet-name>名字(与上面的名字保持一致即可)servlet-name>

           <url-pattern>/url-pattern>

      servlet-mapping>

web-app>


相关内容

热门资讯

精密散热行业的技术跃迁:从“被... 在功率密度持续攀升、热流密度逼近物理极限的行业节点,精密散热已经从一个“辅助性功能模块”演变为决定系...
湖南衡阳发生火灾致5死1伤 5月24日0时45分,衡阳市祁东县上正社区一商铺发生火灾,造成5人死亡,1人受轻微伤,伤者正在积极救...
警惕!澳大利亚密集加码关键矿产... 5月18日,澳大利亚以“国家安全”为由,向北方矿业公司6名与中国有关联的股东发出强制出售令,要求在1...
AI行情狂热,三星电子未成年股... 近段时间,AI行情再次成为全球资本市场主线,但舞台中央的“主角”发生了变化:投资者不再只偏好云厂商和...
俄称乌无人机袭击卢甘斯克一学校... 当地时间5月23日,据俄罗斯紧急情况部通报称,遭乌方袭击的斯塔罗比尔斯克职业学院死亡人数升至21人,...
美加州故障化学品储罐持续升温,... 新华社洛杉矶5月23日电(记者高山 谭晶晶)美国加利福尼亚州南部奥兰治县官员23日说,当地21日开始...
真实感,AI时代写作的生命力所... AI时代,对于许多人来说,因为有了各种智能工具的加持,写作似乎变得容易了。通过提出命题、投喂内容,无...
一个以科技为名的节日,在一座深... 5月23日,2026年上海科技节在上海科技馆正式开幕,同日还将举办第五届上海科技传播大会,并见证上海...
原创 靠... 谁能想到,一笔眼看能让90后创始人躺赚140亿的AI公司收购案,会被商务部一记重拳紧急喊停。 江西小...
特朗普称美伊协议基本谈成 美东时间23日下午,美国总统特朗普在社交媒体发文,称美国与伊朗已经基本谈成一份协议。(央视记者 刘骁...