------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

众所周知,框架Spring来整合别的框架,但是Mybatis出现的晚,Spring就没有给他提供支持,那怎么办呢?Mybatis说,我吃点亏,我给你提供整合的jar,所以那个整合的jar包就叫mabatis-spring。jar

由于SpringMVC和Spring天然集成,所以,Spring整合了Mabatis就证明你ssm整合就搞定了

整合并不只是jar包的堆砌,而是一个框架的部分功能要交给另外一个框架进行完成,调度

写个案例,购买添加图书的Spring+Mabatis+JavaWeb的案例

步骤开始:

  1.引入jar包,修改build节点:

    我之前案例的节点,可能这个案例用不到,但是也一块扔上来了

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <parent>
  4. <artifactId>Y2167DAWNALLDEMO</artifactId>
  5. <groupId>cn.dawn</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.</modelVersion>
  9. <artifactId>02Spring</artifactId>
  10. <packaging>war</packaging>
  11. <name>02Spring Maven Webapp</name>
  12. <url>http://maven.apache.org</url>
  13. <dependencies>
  14. <!--单元测试的依赖 ctrl+shif+/-->
  15. <dependency>
  16. <groupId>junit</groupId>
  17. <artifactId>junit</artifactId>
  18. <version>4.12</version>
  19. <scope>test</scope>
  20. </dependency>
  21.  
  22. <!--Spring-->
  23. <dependency>
  24. <groupId>org.springframework</groupId>
  25. <artifactId>spring-beans</artifactId>
  26. <version>4.2..RELEASE</version>
  27. </dependency>
  28.  
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-context</artifactId>
  32. <version>4.2..RELEASE</version>
  33. </dependency>
  34.  
  35. <!--aop使用的jar-->
  36. <dependency>
  37. <groupId> org.aspectj</groupId >
  38. <artifactId> aspectjweaver</artifactId >
  39. <version> 1.8.</version>
  40. </dependency>
  41.  
  42. <!--spring jdbc-->
  43. <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
  44. <dependency>
  45. <groupId>org.springframework</groupId>
  46. <artifactId>spring-jdbc</artifactId>
  47. <version>4.2..RELEASE</version>
  48. </dependency>
  49.  
  50. <dependency>
  51. <groupId>mysql</groupId>
  52. <artifactId>mysql-connector-java</artifactId>
  53. <version>5.1.</version>
  54. </dependency>
  55.  
  56. <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
  57. <dependency>
  58. <groupId>c3p0</groupId>
  59. <artifactId>c3p0</artifactId>
  60. <version>0.9.1.2</version>
  61. </dependency>
  62.  
  63. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
  64. <dependency>
  65. <groupId>org.apache.commons</groupId>
  66. <artifactId>commons-dbcp2</artifactId>
  67. <version>2.1.</version>
  68. </dependency>
  69.  
  70. <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
  71. <dependency>
  72. <groupId>com.alibaba</groupId>
  73. <artifactId>druid</artifactId>
  74. <version>1.1.</version>
  75. </dependency>
  76.  
  77. <!--mybatis jar包-->
  78. <dependency>
  79. <groupId>org.mybatis</groupId>
  80. <artifactId>mybatis</artifactId>
  81. <version>3.2.</version>
  82. </dependency>
  83.  
  84. <!--Mybatis+Spring整合-->
  85. <dependency>
  86. <groupId>org.mybatis</groupId>
  87. <artifactId>mybatis-spring</artifactId>
  88. <version>1.2.</version>
  89. </dependency>
  90.  
  91. <!-- Spring整合JavaWeb的包 -->
  92. <dependency>
  93. <groupId>org.springframework</groupId>
  94. <artifactId>spring-web</artifactId>
  95. <version>4.2..RELEASE</version>
  96. </dependency>
  97.  
  98. <!--javaee jar-->
  99. <dependency>
  100. <groupId>javaee</groupId>
  101. <artifactId>javaee-api</artifactId>
  102. <version></version>
  103. </dependency>
  104.  
  105. <!--jstl表达式-->
  106. <dependency>
  107. <groupId>jstl</groupId>
  108. <artifactId>jstl</artifactId>
  109. <version>1.2</version>
  110. </dependency>
  111.  
  112. </dependencies>
  113. <build>
  114. <resources>
  115. <resource>
  116. <directory>src/main/java</directory>
  117. <includes>
  118. <include>**/*.xml</include>
  119. </includes>
  120. </resource>
  121. </resources>
  122. </build>
  123. </project>

  2.准备数据库:

  3.分层开发开始:

    3.1entity层

      Book实体类

  1. package cn.dawn.day23ssm.entity;
  2.  
  3. public class Book {
  4. private Integer bookID;
  5. private String bookName;
  6. private String bookAuthor;
  7. private Integer bookPrice;
  8.  
  9. public Book() {
  10. }
  11.  
  12. public Book(String bookName, String bookAuthor, Integer bookPrice) {
  13. this.bookName = bookName;
  14. this.bookAuthor = bookAuthor;
  15. this.bookPrice = bookPrice;
  16. }
  17.  
  18. public Integer getBookID() {
  19. return this.bookID;
  20. }
  21.  
  22. public void setBookID(Integer bookID) {
  23. this.bookID = bookID;
  24. }
  25.  
  26. public String getBookName() {
  27. return this.bookName;
  28. }
  29.  
  30. public void setBookName(String bookName) {
  31. this.bookName = bookName;
  32. }
  33.  
  34. public String getBookAuthor() {
  35. return this.bookAuthor;
  36. }
  37.  
  38. public void setBookAuthor(String bookAuthor) {
  39. this.bookAuthor = bookAuthor;
  40. }
  41.  
  42. public Integer getBookPrice() {
  43. return this.bookPrice;
  44. }
  45.  
  46. public void setBookPrice(Integer bookPrice) {
  47. this.bookPrice = bookPrice;
  48. }
  49. }

    3.2dao层

      接口IBookDAO

  1. package cn.dawn.day23ssm.dao;
  2.  
  3. import cn.dawn.day23ssm.entity.Book;
  4.  
  5. /**
  6. * Created by Dawn on 2018/3/17.
  7. */
  8. public interface IBookDAO {
  9. //添加
  10. public int insertBook(Book book) throws Exception;
  11. }

      同名的IBookDAO.xml配置文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="cn.dawn.day23ssm.dao.IBookDAO">
  6.  
  7. <insert id="insertBook">
  8. INSERT INTO book(bookname,bookauthor,bookprice) VALUES (#{bookName},#{bookAuthor},#{bookPrice})
  9. </insert>
  10.  
  11. </mapper>

    3.3service层

      IBookService接口:

  1. package cn.dawn.day23ssm.service;
  2.  
  3. import cn.dawn.day23ssm.entity.Book;
  4.  
  5. /**
  6. * Created by Dawn on 2018/3/17.
  7. */
  8. public interface IBookService {
  9. //添加
  10. public int insertBook(Book book) throws Exception;
  11. }

      BookServiceImpl刚才那个接口的实现类

  1. package cn.dawn.day23ssm.service;
  2.  
  3. import cn.dawn.day23ssm.dao.IBookDAO;
  4. import cn.dawn.day23ssm.entity.Book;
  5. import org.springframework.transaction.annotation.Transactional;
  6.  
  7. /**
  8. * Created by Dawn on 2018/3/17.
  9. */
  10. public class BookServiceImpl implements IBookService {
  11. private IBookDAO dao;
  12.  
  13. //开启事务
  14. @Transactional
  15. public int insertBook(Book book) throws Exception {
  16. return dao.insertBook(book);
  17. }
  18.  
  19. public IBookDAO getDao() {
  20. return dao;
  21. }
  22.  
  23. public void setDao(IBookDAO dao) {
  24. this.dao = dao;
  25. }
  26. }

      此处我做了事务的开启

  3.我的习惯是在此处开始写配置文件,走个单测再去和javaweb打交道

    所以此处的步骤就是三个大配置文件

      3.1jdbc.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql:///s2228
  3. jdbc.username=root
  4. jdbc.password=

      3.2mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <typeAliases>
  7. <package name="cn.dawn.day23ssm.entity"></package>
  8. </typeAliases>
  9. </configuration>

      此处的mybatis只做了别名,但是这个案例中没有用,只是提一下,如果做查询的时候,别名从这儿设置,我一会也给把加入spring的地方也标出来

      它的mappers,properties都不从这儿设置,这就印证了我之前的一句话,整合并不只是jar包的堆砌,而是一个框架的部分功能要交给另外一个框架进行完成,调度

      3.3ApplicationContext-day23ssm.xml(你们可以改这个名字的,一会调用的时候改掉就可以了,不要学死,灵活应变,举一反三)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/aop
  11. http://www.springframework.org/schema/aop/spring-aop.xsd
  12. http://www.springframework.org/schema/context
  13. http://www.springframework.org/schema/context/spring-context.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <!--配置jdbc。properties-->
  18. <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
  19.  
  20. <!--阿里的Druid-->
  21. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  22. <property name="driverClassName" value="${jdbc.driver}"></property>
  23. <property name="url" value="${jdbc.url}"></property>
  24. <property name="username" value="${jdbc.username}"></property>
  25. <property name="password" value="${jdbc.password}"></property>
  26. </bean>
  27.  
  28. <!--dao层-->
  29. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <property name="dataSource" ref="dataSource"></property>
  31. <!--这儿的mybatis配置文件中只做别名,其他的都由spring整合了-->
  32. <property name="configLocation" value="classpath:mybatis-config.xml"></property>
  33. </bean>
  34. <!--映射扫描器-->
  35. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  36. <property name="basePackage" value="cn.dawn.day23ssm.dao"></property>
  37. </bean>
  38. <!--service-->
  39. <bean id="bookService" class="cn.dawn.day23ssm.service.BookServiceImpl">
  40. <property name="dao" ref="IBookDAO"></property>
  41. </bean>
  42. <!--事务管理器-->
  43. <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  44. <property name="dataSource" ref="dataSource"></property>
  45. </bean>
  46. <!--事务开启,注解版-->
  47. <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
  48.  
  49. </beans>

  4.jsp页面和servlet

    4.1jsp页面

      success.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Dawn
  4. Date: //
  5. Time: :
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
  9. <html>
  10. <head>
  11. <title>添加成功</title>
  12. </head>
  13. <body>
  14. <h2 style="text-align: center;color: red">添加${bookName}成功</h2>
  15. </body>
  16. </html>

      addBook.jsp

  1. <%--
  2. Created by IntelliJ IDEA.
  3. User: Dawn
  4. Date: //
  5. Time: :
  6. To change this template use File | Settings | File Templates.
  7. --%>
  8. <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
  9. <html>
  10. <head>
  11. <title>添加图书页面</title>
  12. </head>
  13. <body>
  14. <form method="post" action="${pageContext.request.contextPath}/BookServlet">
  15. 书名:<input type="text" name="bookName">
  16. 作者:<input type="text" name="bookAuthor">
  17. 价格:<input type="text" name="bookPrice">
  18. <input type="submit" value="添加图书">
  19. </form>
  20. </body>
  21. </html>

    4.2servlet层

      BookServlet

  1. package cn.dawn.day23ssm.servlet;
  2.  
  3. import cn.dawn.day23ssm.entity.Book;
  4. import cn.dawn.day23ssm.service.IBookService;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import org.springframework.web.context.support.WebApplicationContextUtils;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.IOException;
  14.  
  15. /**
  16. * Created by Dawn on 2018/3/17.
  17. */
  18. public class BookServlet extends HttpServlet {
  19. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  20. //解决乱码
  21. request.setCharacterEncoding("utf-8");
  22. //获取书名
  23. String bookName = request.getParameter("bookName");
  24. //获取作者
  25. String bookAuthor = request.getParameter("bookAuthor");
  26. //获取价格
  27. String bookPriceStr = request.getParameter("bookPrice");
  28. Integer bookPrice=Integer.parseInt(bookPriceStr);
  29. //创建图书对象
  30. Book book=new Book(bookName,bookAuthor,bookPrice);
  31. //创建service对象
         //采用优雅的方式:
         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  32. ApplicationContext applicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
          
  33. //ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext-day23ssm.xml");
  34. IBookService bookService = (IBookService)applicationContext.getBean("bookService");
  35. //调用方法
  36. try {
  37. int result = bookService.insertBook(book);
  38. //根据返回结果判断是否成功
  39. if (result>){
  40. //把结果传过去
  41. request.setAttribute("bookName",bookName);
  42. //成功,转发到Index页面
  43. request.getRequestDispatcher("/success.jsp").forward(request,response);
  44.  
  45. }else {
  46. //失败,重定向到刚才的页面
  47. response.sendRedirect("/ssm/addBook.jsp");
  48. }
  49.  
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53.  
  54. }
  55.  
  56. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  57. doPost(request,response);
  58. }
  59. }

    这儿有我之前埋的一个坑,我这儿用的javaee是5的版本,不是6.0,没法用注解版,所以还得到web.xml中配置一道

    4.3web。xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4.  
  5. <web-app>
  6. <display-name>Archetype Created Web Application</display-name>
  7.  
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath:ApplicationContext-day23ssm.xml</param-value>
  11. </context-param>
  12. <!--监听器-->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <servlet>
  17. <servlet-name>BookServlet</servlet-name>
  18. <servlet-class>cn.dawn.day23ssm.servlet.BookServlet</servlet-class>
  19. </servlet>
  20. <servlet-mapping>
  21. <servlet-name>BookServlet</servlet-name>
  22. <url-pattern>/BookServlet</url-pattern>
  23. </servlet-mapping>
  24. <welcome-file-list>
  25. <welcome-file>index.jsp</welcome-file>
  26. </welcome-file-list>
  27. </web-app>

  这儿有一处之前没有见到过

  servlet处的

  ApplicationContext applicationContext= WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());

  1.  web.xml中的监听器和配置
     这是什么呢?
      这是一个优雅的方式,
     总所周知,你将之前单测的那种方式你直接拖过来用,那在servlet中会造成什么后果呢?
      就是每一次访问servlet你就对spring容器进行一次初始化工作,这里面的bean因为是单例的,所以都会生成一次,相当耗损性能
     那么怎么办?
      你想servletcontext就是在web容器中从一开始到结束过程中都可以访问到的,那么我们在web容器一启动的时候就将spring容器放到servletcontext中岂不美哉,之后就不用new
      

-------笔者:晨曦Dawn-------

转载请注明出处:http://www.cnblogs.com/DawnCHENXI/p/8597436.html

SSM-Spring-22:Spring+Mybatis+JavaWeb的整合的更多相关文章

  1. SSM 三大框架系列:Spring 5 + Spring MVC 5 + MyBatis 3.5 整合(附源码)

    之前整理了一下新版本的 SSM 三大框架,这篇文章是关于它的整合过程和项目源码,版本号分别为:Spring 5.2.2.RELEASE.SpringMVC 5.2.2.RELEASE.MyBatis ...

  2. JAVA 框架 / SSM / SSM SPRING+SPING MVC + MYBATIS 三大框架整合详细步骤

    http://how2j.cn/k/ssm/ssm-tutorial/1137.html

  3. Spring boot 、mybatis 和 swagger 整合

    文件路径 添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  4. Spring Boot 与 Mybatis、Mysql整合使用的例子

    第一步: 创建一个SpringBoot的工程,在其中的Maven依赖配置中添加对JDBC.MyBatis.Mysql Driver的依赖具体如下: <!-- JDBC --> <de ...

  5. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  6. SSM(spring mvc+spring+mybatis)学习路径——2-2、spring MVC拦截器

    目录 2-2 Spring MVC拦截器 第一章 概述 第二章 Spring mvc拦截器的实现 2-1 拦截器的工作原理 2-2 拦截器的实现 2-3 拦截器的方法介绍 2-4 多个拦截器应用 2- ...

  7. 一步步教你整合SSM框架(Spring MVC+Spring+MyBatis)详细教程重要

    前言 SSM(Spring+SpringMVC+Mybatis)是目前较为主流的企业级架构方案,不知道大家有没有留意,在我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教 ...

  8. SSM(Spring MVC +Spring+Mybatis)整合——maven工程

    所谓的SSM 其实就是Spring MVC下整合mybatis. 具体的定义网络上都有,很详细. 这里只说项目的搭建步骤. 第一步 新建maven工程 工程目录如下: 配置pom.xml文件,引入所需 ...

  9. [置顶] Java Web学习总结(24)——SSM(Spring+SpringMVC+MyBatis)框架快速整合入门教程

    1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One  ...

随机推荐

  1. Shell Scripts - 条件语句,case语句,function功能

    修改之前的代码 1.判断 $1 是否为 hello,如果是的话,就显示 "Hello, how are you ?":     2.如果没有加任何参数,就提示使用者必须要使用的参数 ...

  2. 软件开发顶尖高手的杀手锏SQL语句

                  软件开发顶尖高手的杀手锏SQL语句                                                                     ...

  3. javascript语言扩展:可迭代对象(5)

    文章1-4篇说的都是js中的可迭代对象,下面让我们看看ruby中的等价物. 不可否认,ruby中对于迭代器和生成器的语法都相当简洁:ruby从一开始就有一个简洁的基因,而js后来的不断扩充使得其有些语 ...

  4. JVM学习--(六)类加载器原理

    我们知道我们编写的java代码,会经过编译器编译成字节码文件(class文件),再把字节码文件装载到JVM中,映射到各个内存区域中,我们的程序就可以在内存中运行了.那么字节码文件是怎样装载到JVM中的 ...

  5. App 被拒 -- App Store Review Guidelines (2015)中英文对照

    Introduction(简介) We're pleased that you want to invest your talents and time to develop applications ...

  6. Android 在Fragment中执行onActivityResult不被调用的简单解决方法

    在Android开发中,我们经常会用到FragmentActivity下嵌套多个Fragment,但是在开发过程中会发现在嵌套的Fragment中使用onActivityResult回调方法没有被执行 ...

  7. oracle的for和i++

    很长时间没用oracle的储存了,这次用到一次i++i++的sql语句:declarei_1 number(30) :=0;begin i_1 :=i_1+1;//i_1=1 insert into ...

  8. css区分ie8/ie9/ie10/ie11 chrome firefox的代码

    以下是几个主要浏览器的css  hack汇总: 现有css样式为: .class{ color:red; } 判断IE8以上的浏览器才执行的代码/* IE8+ */ .class{ color:red ...

  9. 修改was数据源

    本机的RAD运行的工程可以通过修改jpa中的persistence中的jni修改数据源: 对于通过was控制台部署的ear需要在was控制台:资源--jdbc 修改数据源

  10. JAVA 平台

    由Java虚拟机和Java核心类所构成.它为纯Java程序提供了统一的编程接口,而不管下层操作系统是什么. 目录 1Java术语 2移动平台 3桌面应用平台 4企业级平台 5JRE的成分     1J ...