重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

ssm框架整合思路及代码-创新互联

1、Dao层:

创新互联专注于企业全网整合营销推广、网站重做改版、绥宁网站定制设计、自适应品牌网站建设、HTML5建站商城网站开发、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为绥宁等各大城市提供网站开发制作服务。

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

创建SqlMapConfig.xml配置文件

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

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

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

创建applicationContext-dao.xml

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

     

     

     "classpath:properties/*.properties"/>

     

     "dataSource"class="com.alibaba.druid.pool.DruidDataSource"

          destroy-method="close">

          "url"value="${jdbc.url}"/>

          "username"value="${jdbc.username}"/>

          "password"value="${jdbc.password}"/>

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

          "maxActive"value="10"/>

          "minIdle"value="5"/>

     

     

     "sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

          

          "dataSource"ref="dataSource"/>

          

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

     

     "org.mybatis.spring.mapper.MapperScannerConfigurer">

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

     

为了便于更改数据库,编写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

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

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

     "实现类所在的包的全路径 ">

创建applicationContext-trans.xml

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

     

     "transactionManager"

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

          

          "dataSource"ref="dataSource"/>

     

     

     "txAdvice"transaction-manager="transactionManager">

          

                

                "save*"propagation="REQUIRED"/>

                "insert*"propagation="REQUIRED"/>

                "add*"propagation="REQUIRED"/>

                "create*"propagation="REQUIRED"/>

                "delete*"propagation="REQUIRED"/>

                "update*"propagation="REQUIRED"/>

                "find*"propagation="SUPPORTS"read-only="true"/>

                "select*"propagation="SUPPORTS"read-only="true"/>

                "get*"propagation="SUPPORTS"read-only="true"/>

          

     

     

     

          "txAdvice"

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

     

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

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

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

     名字

     

     

          contextConfigLocation

          classpath:spring/applicationContext*.xml

     

     

          org.springframework.web.context.ContextLoaderListener

     

3、表现层:

Springmvc框架,由springmvc管理controller

创建Springmvc.xml

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

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

     "com.taotao.controller"/>

     

     

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

          "prefix"value="/WEB-INF/jsp/"/>

          "suffix"value=".jsp"/>

     

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

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

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

     名字

     

          欢迎页

     

     

     

          CharacterEncodingFilter

          org.springframework.web.filter.CharacterEncodingFilter

          

                encoding

                utf-8

          

     

     

          CharacterEncodingFilter

          /*

     

     

     

          名字

          org.springframework.web.servlet.DispatcherServlet

          

          

                contextConfigLocation

                classpath:spring/springmvc.xml

          

          1

     

     

          名字(与上面的名字保持一致即可)

          /

     

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:ssm框架整合思路及代码-创新互联
网页URL:http://cqcxhl.cn/article/docdjj.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP