广告
返回顶部
首页 > 资讯 > 数据库 >ssm框架整合思路及代码
  • 734
分享到

ssm框架整合思路及代码

2024-04-02 19:04:59 734人浏览 安东尼
摘要

1、Dao层:mybatis整合spring,通过spring创建数据库连接池,管理sqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包 创建SqlM

1、Dao层:

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

 

创建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>

      <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->

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

           <!-- 数据库连接池 -->

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

           <!-- 加载mybatis的全局配置文件 -->

           <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>

      <!-- 加载spring容器 -->

      <context-param>

           <param-name>contextConfigLocation</param-name>

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

      </context-param>

      <listener>

           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-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>

      <!-- 解决post乱码 -->

      <filter>

           <filter-name>CharacterEncodingFilter</filter-name>

           <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

           <init-param>

                 <param-name>encoding</param-name>

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

           </init-param>

      </filter>

      <filter-mapping>

           <filter-name>CharacterEncodingFilter</filter-name>

           <url-pattern>/*</url-pattern>

      </filter-mapping>

 

 

      <!-- springmvc的前端控制器 -->

      <servlet>

           <servlet-name>名字</servlet-name>

           <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

           <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml"-->

           <init-param>

                 <param-name>contextConfigLocation</param-name>

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

           </init-param>

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

      </servlet>

      <servlet-mapping>

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

           <url-pattern>/</url-pattern>

      </servlet-mapping>

</web-app>


您可能感兴趣的文档:

--结束END--

本文标题: ssm框架整合思路及代码

本文链接: https://www.lsjlt.com/news/38147.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作