04_SSM框架整合(Spring+SpringMVC+MyBatis)

【SSM的系统架构】

【整合概述】

第一步:

  MyBatis和Spring整合,通过Spring管理mapper接口。

  使用mapper的扫描器自动扫描mapper接口在Spring中进行注册。

第二步:

  通过Spring管理Service接口。

  使用配置方式将Service接口配置在Spring配置文件中。

  实现事务控制。

第三步:

  由于SpringMVC是Spring的模块,无需整合这两个。

【工程截图】

【数据库的items】

[ 表结构 ]

[ 表内数据 ]

【1.整合dao】
将Mybatis和Spring进行整合

【1.1 sqlMapConfig.xml】MyBatis的配置文件

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

    <!-- 全局setting配置 ,根据需要再添加-->

    <!-- 配置别名 -->
    <typeAliases>
        <!-- 批量扫描别名 -->
        <package name="cn.Higgin.ssm.po"/>
    </typeAliases>

    <!-- 配置mapper
     由于使用spring和mybatis的整合包进行整合,这了无需配置
     但必须遵循:mapper.xml和mapper.xml文件同名且在同一目录-->
    <!--<mappers></mappers>  -->
</configuration>

【1.2 db.properties】数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=

【1.3 applicationContext-dao.xml】

需要配置:数据源、SqlSessionFactory、mapper扫描器

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置数据源,dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="5"/>
    </bean>

    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加载mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    </bean>

    <!-- mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
        <property name="basePackage" value="cn.Higgin.ssm.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

【1.4 逆向工程生成PO类以及mapper】(对应表的增删改查)

【1.5 手动定义商品查询mapper】

针对综合查询mapper,一般情况下会有关联查询,建议 自定义 mapper。

【1.5.1 ItemsMapperCustom.java】

package cn.Higgin.ssm.po;

/**
 *  商品信息的扩展
 */
public class ItemsCustom extends Items {

    //添加商品信息扩展的属性

}

【1.5.2 ItemsMapperCustom.xml】

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.Higgin.ssm.mapper.ItemsMapperCustom" >

   <!-- 定义商品查询的sql片段,就是商品查询条件 -->
   <sql id="query_items_where">
       <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 -->
       <!-- 商品查询条件通过ItemsQueryVo包装对象 中itemsCustom属性传递 -->
           <if test="itemsCustom!=null">
               <if test="itemsCustom.name!=null and itemsCustom.name!=‘‘">
                   items.name LIKE ‘%${itemsCustom.name}%‘
               </if>
           </if>
   </sql>

      <!-- 商品列表查询 -->
      <!-- parameterType:传入包装对象(包装了查询条件)
          resultType:建议使用扩展对象
       -->
      <select id="findItemsList" parameterType="cn.Higgin.ssm.po.ItemsQueryVo"
           resultType="cn.Higgin.ssm.po.ItemsCustom">
          SELECT items.* FROM items
          <where>
              <include refid="query_items_where"></include>
          </where>
      </select>

</mapper>
时间: 2024-08-05 21:05:37

04_SSM框架整合(Spring+SpringMVC+MyBatis)的相关文章

使用maven整合spring+springmvc+mybatis

使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中,引入我们需要的jar包:(按需引入jar包) <properties> <!-- 自定义的spring的版本号 --> <spring.version>4.3.18.RELEASE</spring.version> <!-- 自定义的mybaits的版本号

SSM框架 (Spring+SpringMVC+MyBatis)

SSM框架--详细整合教程(Spring+SpringMVC+MyBatis) springspringmvcmybatis整合教程ssm整合 1.基本概念  1.1.Spring          Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来.它是为了解决企业应用开发的复杂性而创建的.Spri

java高并发框架 SSM框架 详细整合(Spring+SpringMVC+MyBatis)

获取[下载地址]   [免费支持更新]三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单;freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等完整模块B 集成阿里巴巴数据库连接池druid;  数据库连接池  阿里巴巴的 druid.Drui

【企业级框架整合】Springmvc+mybatis+restful+bootstrap框架整合

1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问)2. 提供高并发JMS消息处理机制3. 所有功能模块化.所有模块服务化.所有服务原子化的方式,提供可拓展的服务模型,使程序稳定运行,永不宕机4. 提供Wink Rest.Webservice服务,故可作为独立服务平台部署 框架整合: Springmvc + Mybatis + Shiro(权限) + REST(服务) + W

【javaWeb框架整合】springmvc+mybatis+shiro+restful+Webservice+bootstrap+html5

开发报捷:增加Lucene搜索引擎功能 1. 创建.初始化索引.统一搜索入口.搜索结果展现--内容.标题高亮.关键词搜索 2. 高级搜索:高级搜索增加多入口查询(精确查询.模糊查询.前缀查询等),每页显示条数自定义.索引结果数据设置.选择索引文档类型等) 集成lucene搜索引擎: 1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问) 2. 提供高并发JMS消息处理机制 3.

【高并发 大数据框架整合】Springmvc+mybatis+shiro+lucene+rest+webservice+maven

1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问) 2. 提供高并发JMS消息处理机制 3. 所有功能模块化.所有模块服务化.所有服务原子化的方式,提供可拓展的服务模型,使程序稳定运行,永不宕机 4. 提供Wink Rest.Webservice服务,故可作为独立服务平台部署 框架整合: Springmvc + Mybatis + Shiro(权限) + REST(服务)

【java高并发 大数据企业架构框架整合】Springmvc+mybatis+shiro+lucene+rest+webservice+maven

1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问) 2. 提供高并发JMS消息处理机制 3. 所有功能模块化.所有模块服务化.所有服务原子化的方式,提供可拓展的服务模型,使程序稳定运行,永不宕机 4. 提供Wink Rest.Webservice服务,故可作为独立服务平台部署 框架整合: Springmvc + Mybatis + Shiro(权限) + REST(服务)

【ssm框架整合】springmvc+mybatis+shiro+rest+webservice+lucene+bootstrap html5

1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控数据库访问性能.支持Common-Logging.Log4j和JdkLog,监控数据库访问)2. 提供高并发JMS消息处理机制3. 所有功能模块化.所有模块服务化.所有服务原子化的方式,提供可拓展的服务模型,使程序稳定运行,永不宕机4. 提供Wink Rest.Webservice服务,故可作为独立服务平台部署 框架整合: Springmvc + Mybatis + Shiro(权限) + REST(服务) + W

很棒!【企业级框架整合】Springmvc+mybatis+restful+bootstrap框架整合

1.  创建.初始化索引.统一搜索入口.搜索结果展现--内容.标题高亮.关键词搜索 2.  高级搜索:高级搜索增加多入口查询(精确查询.模糊查询.前缀查询等),每页显示条数自定义.索引结果数据设置.选择索引文档类型等 3. 通过A系统调用B系统的Rest服务,生成相关的二维码,可以直接用户手机app --------------------------------------------------------- 1. 使用阿里巴巴Druid连接池(高效.功能强大.可扩展性好的数据库连接池.监控

2018用IDEA搭建SSM框架(Spring+SpringMVC+Mybatis)

使用IDEA搭建ssm框架 工具:IDEA 2018.1 jdk版本:jdk1.8.0_171 Maven版本:apache-maven-3.5.3 Tomcat版本:apache-tomcat-8.5.30 新建maven项目 项目基本展示 编写pom.xml文件,导入相关jar包 <dependencies> <!--添加测试类--> <dependency> <groupId>junit</groupId> <artifactId&g