前言

spring、mybatis、springmvc的整合。

创建maven项目

  1. main
  2. ├─java
  3. └─com
  4. └─alvin
  5. ├─controller
  6. ├─mapper
  7. └─service
  8. ├─resources
  9. ├─mybatis
  10. └─spring
  11. └─webapp
  12. └─WEB-INF

添加依赖

代码
  1. <!--配置SpringMVC的依赖,包括了Spring的相关依赖(IOC,AOP)-->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-webmvc</artifactId>
  5. <version>5.0.2.RELEASE</version>
  6. </dependency>
  7. <!--配置SpringJdbc依赖,包括JdbcTemplate,和内置的数据源-->
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-jdbc</artifactId>
  11. <version>5.0.2.RELEASE</version>
  12. </dependency>
  13. <!--配置SpringAOP的扩展支持,可以支持纯注解的AOP(@Aspect,五个通知的注解)-->
  14. <dependency>
  15. <groupId>org.aspectj</groupId>
  16. <artifactId>aspectjweaver</artifactId>
  17. <version>1.8.7</version>
  18. </dependency>
  19. <!-- 数据源 -->
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>druid</artifactId>
  23. <version>1.1.9</version>
  24. </dependency>
  25. <!--MySql数据库驱动-->
  26. <dependency>
  27. <groupId>mysql</groupId>
  28. <artifactId>mysql-connector-java</artifactId>
  29. <version>5.1.6</version>
  30. </dependency>
  31. <!--MyBatis依赖包-->
  32. <dependency>
  33. <groupId>org.mybatis</groupId>
  34. <artifactId>mybatis</artifactId>
  35. <version>3.4.5</version>
  36. </dependency>
  37. <!--MyBatis和Spring的整合包-->
  38. <dependency>
  39. <groupId>org.mybatis</groupId>
  40. <artifactId>mybatis-spring</artifactId>
  41. <version>1.3.0</version>
  42. </dependency>
  43. <!--junit的测试-->
  44. <dependency>
  45. <groupId>junit</groupId>
  46. <artifactId>junit</artifactId>
  47. <version>4.12</version>
  48. <scope>test</scope>
  49. </dependency>
  50. <!--Spring的测试组件-->
  51. <dependency>
  52. <groupId>org.springframework</groupId>
  53. <artifactId>spring-test</artifactId>
  54. <version>5.0.2.RELEASE</version>
  55. <scope>test</scope>
  56. </dependency>
  57. <!--对JSP的支持-->
  58. <dependency>
  59. <groupId>javax.servlet</groupId>
  60. <artifactId>servlet-api</artifactId>
  61. <version>2.5</version>
  62. <scope>provided</scope>
  63. </dependency>
  64. <dependency>
  65. <groupId>javax.servlet.jsp</groupId>
  66. <artifactId>jsp-api</artifactId>
  67. <version>2.0</version>
  68. <scope>provided</scope>
  69. </dependency>
  70. <!--jsp的JSTL标准标签库的支持-->
  71. <dependency>
  72. <groupId>jstl</groupId>
  73. <artifactId>jstl</artifactId>
  74. <version>1.2</version>
  75. </dependency>

配置文件

总览

tree ./src/main /F > treeAll.txt

  1. touch jdbc.properties
  2. touch mybatis/SqlMapConfig.xml
  3. touch spring/applicationContext-dao.xml
  4. touch spring/applicationContext-service.xml
  5. touch spring/applicationContext-trans.xml
  6. touch spring/springmvc.xml
  1. main
  2. ├─java
  3. └─com
  4. └─alvin
  5. ├─controller
  6. ├─mapper
  7. └─service
  8. ├─resources
  9. jdbc.properties

  10. ├─mybatis
  11. SqlMapConfig.xml

  12. └─spring
  13. applicationContext-dao.xml
  14. applicationContext-service.xml
  15. applicationContext-trans.xml
  16. springmvc.xml

  17. └─webapp
  18. index.jsp

  19. └─WEB-INF
  20. web.xml

jdbc配置

jdbc.properties

  1. jdbc.driver = com.mysql.jdbc.Driver
  2. jdbc.url = jdbc:mysql://127.0.0.1:3306/db_test?characterEncoding=utf8
  3. jdbc.username = root
  4. jdbc.password = root
  5. mappers.package = com.alvin.mapper

mybatis配置

SqlMapConfig.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. </configuration>

dao层配置

applicationContext-dao.xml

  • 加载jdbc文件
  • 数据源
  • SQLSessionFactory
  • 包扫描
  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:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--加载jdbc.properties配置文件-->
  7. <context:property-placeholder location="classpath:jdbc.properties"/>
  8. <!-- 3. 配置数据源 -->
  9. <!-- 数据源配置, 使用 Druid 数据库连接池 -->
  10. <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  11. <!-- 四大属性 -->
  12. <property name="driverClassName" value="${jdbc.driver}" />
  13. <property name="url" value="${jdbc.url}" />
  14. <property name="username" value="${jdbc.username}" />
  15. <property name="password" value="${jdbc.password}" />
  16. <!-- 配置初始化连接个数 -->
  17. <property name="initialSize" value="0" />
  18. <!-- 配置最小空闲连接 -->
  19. <property name="minIdle" value="0" />
  20. <!-- 配置最大的活动连接 -->
  21. <property name="maxActive" value="15" />
  22. <!-- 配置获取连接等待超时的时间 -->
  23. <property name="maxWait" value="60000" />
  24. <!-- 获取链接的时候,不校验是否可用,开启会有损性能
  25. 这里建议配置为TRUE,防止取到的连接不可用。-->
  26. <property name="testOnBorrow" value="true" />
  27. <!-- 归还链接到连接池的时候校验链接是否可用 -->
  28. <property name="testOnReturn" value="false" />
  29. <!-- 此项配置为true即可,不影响性能,并且保证安全性。
  30. 意义为:申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,
  31. 执行validationQuery检测连接是否有效 -->
  32. <property name="testWhileIdle" value="true" />
  33. <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒 -->
  34. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  35. <!-- 配置一个连接在池中最小生存的时间,单位毫秒 -->
  36. <property name="minEvictableIdleTimeMillis" value="300000" />
  37. <!-- 链接使用超过时间限制是否回收 -->
  38. <property name="removeAbandoned" value="true" />
  39. <!-- 超过时间限制时间(单位秒),目前为5分钟,如果有业务处理时间超过5分钟,可以适当调整。 -->
  40. <property name="removeAbandonedTimeout" value="300" />
  41. <!-- #链接回收的时候控制台打印信息,测试环境可以加上true,线上环境false。会影响性能。 -->
  42. <property name="logAbandoned" value="false" />
  43. <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
  44. <property name="validationQuery" value="select 1 " />
  45. </bean>
  46. <!--配置SqlSessionFactory,需要使用mybatis-spring整合包中的类-->
  47. <bean class="org.mybatis.spring.SqlSessionFactoryBean">
  48. <!--配置数据源-->
  49. <property name="dataSource" ref="dataSource"/>
  50. <!--加载MyBatis的配置文件,SqlMapConfig.xml.现在为空文件-->
  51. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
  52. </bean>
  53. <!--配置Mapper扫描,需要使用mybatis-spring整合包中的类-->
  54. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  55. <!--配置Mapper扫描的包-->
  56. <property name="basePackage" value="com.alvin.mapper"/>
  57. </bean>
  58. </beans>

service层配置

applicationContext-service.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:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--扫描service的注解,让注解生效-->
  7. <context:component-scan base-package="com.alvin.service"/>
  8. </beans>

事务配置

applicationContext-trans.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  9. <!--配置事务管理器-->
  10. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  11. <!--这里报错,原因是IDEA在这个xml配置中没有找到dataSource-->
  12. <!--不用管这个错误,Spring启动的时候,会把所有的配置全部加在,包括数据源的配置(在dao里)-->
  13. <property name="dataSource" ref="dataSource"/>
  14. </bean>
  15. <!--配置在哪个方法上执行-->
  16. <tx:advice id="tx" transaction-manager="transactionManager" >
  17. <tx:attributes>
  18. <!--所有以trans,save,update,delete开头的方法都会进行事务管理,
  19. 因为默认的配置就是要进行事务管理-->
  20. <tx:method name="trans*"/>
  21. <tx:method name="save*"/>
  22. <tx:method name="update*"/>
  23. <tx:method name="delete*"/>
  24. <!--以下配置都不进行事务管理-->
  25. <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
  26. <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
  27. <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
  28. </tx:attributes>
  29. </tx:advice>
  30. <!--配置在什么地方执行-->
  31. <aop:config>
  32. <aop:advisor advice-ref="tx"
  33. pointcut="execution(* com.alvin.service.impl.*.*(..))"/>
  34. </aop:config>
  35. </beans>

controller配置

  • 包扫描
  • 注解驱动
  • 视图解析器

springmvc.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:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  7. <!--配置Controller的包扫描-->
  8. <context:component-scan base-package="com.alvin.controller"/>
  9. <!--配置注解驱动-->
  10. <mvc:annotation-driven/>
  11. <!--配置视图解析器-->
  12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <property name="prefix" value="/WEB-INF/jsp/"/>
  14. <property name="suffix" value=".jsp"/>
  15. </bean>
  16. </beans>

web.xml

项目首先加载的配置文件

  • 加载所有配置文件
  • 启动spring容器
  • 解决post乱码
  • 配置前端控制器
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. id="WebApp_ID" version="2.5">
  7. <display-name>Archetype Created Web Application</display-name>
  8. <!--配置Spring随着项目的启动创建,就是Spring和web项目的整合-->
  9. <!--配置创建Spring容器需要加载的配置文件-->
  10. <context-param>
  11. <!--contextConfigLocation:是固定写法,是spring中的一个属性-->
  12. <param-name>contextConfigLocation</param-name>
  13. <!--因为Spring配置有很多,我们需要启动的时候全部加在,所以使用*-->
  14. <param-value>classpath:spring/applicationContext-*.xml</param-value>
  15. </context-param>
  16. <!--配置监听器,让Spring容器随着ServletContext的创建而创建-->
  17. <!--(Tomcat启动,web应用就启动,Spring容器就创建了)-->
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. <!--配置POST乱码解决的过滤器-->
  22. <filter>
  23. <filter-name>encoding</filter-name>
  24. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  25. <init-param>
  26. <param-name>encoding</param-name>
  27. <param-value>UTF-8</param-value>
  28. </init-param>
  29. <init-param>
  30. <param-name>forceEncoding</param-name>
  31. <param-value>true</param-value>
  32. </init-param>
  33. </filter>
  34. <filter-mapping>
  35. <filter-name>encoding</filter-name>
  36. <url-pattern>/*</url-pattern>
  37. </filter-mapping>
  38. <!--配置SpringMVC的前端控制器-->
  39. <servlet>
  40. <servlet-name>dispatcherServlet</servlet-name>
  41. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  42. <!--加载SpringMVC核心配置文件-->
  43. <init-param>
  44. <param-name>contextConfigLocation</param-name>
  45. <param-value>classpath:spring/springmvc.xml</param-value>
  46. </init-param>
  47. <load-on-startup>1</load-on-startup>
  48. </servlet>
  49. <!--配置所有的请求都进入SpringMVC-->
  50. <servlet-mapping>
  51. <servlet-name>dispatcherServlet</servlet-name>
  52. <url-pattern>/</url-pattern>
  53. </servlet-mapping>
  54. </web-app>

使用

  • 编写pojo
  • 编写mapper接口
  • 编写Mapper.xml映射文件
  • 编写Service接口
  • 编写Service实现
  • 编写Controller
  • 编写前端页面

ssm单项目整合的更多相关文章

  1. SSM 框架 ---项目整合

    一.SSM框架理解 Spring(业务层) Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象. Spring的核心思想是IoC(控 ...

  2. IdentityServer4实战 - 与API单项目整合

    一.前言 我们在实际使用 IdentityServer4 的时候,可能会在使用 IdentityServer4 项目添加一些API,比如 找回密码.用户注册.修改用户资料等,这些API与Identit ...

  3. IdentityServer4与API单项目整合(net core 3.X)

    一.创建一个空的api项目 添加identityserver4的nuget包 配置config文件 public static IEnumerable<IdentityResource> ...

  4. SSM项目整合第一步 注册登陆实现

    SSM项目整合第一步  注册: 项目目录: 一.数据库建表: 源码: ; -- ---------------------------- -- Table structure for t_user - ...

  5. SSM项目整合基本步骤

    SSM项目整合 1.基本概念 1.1.Spring Spring 是一个开源框架, Spring 是于 2003  年兴起的一个轻量级的 Java  开发框架,由 Rod Johnson  在其著作  ...

  6. (转)shiro权限框架详解06-shiro与web项目整合(下)

    http://blog.csdn.net/facekbook/article/details/54962975 shiro和web项目整合,实现类似真实项目的应用 web项目中认证 web项目中授权 ...

  7. (转) shiro权限框架详解06-shiro与web项目整合(上)

    http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...

  8. Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)

    Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...

  9. 【JAVA - SSM】之SSM入门项目的搭建

    最近学习了一下SSM.SSM是 Spring + SpringMVC + MyBatis 整合框架,非常适合WEB后台开发,也是当前很多人的不二选择.这篇博客带大家来创建一个学习SSM的入门程序,从搭 ...

随机推荐

  1. java数据结构之递归算法

    概述程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用.递归有直接递归和间接递归•直接递归:函数在执行过程中调用本身.•间接递归:函数在执行过程中调用其它 ...

  2. arm处理器启动流程分析

    2440: 启动方式:nor , nand 地址布局: 启动流程: 开发板在上电后,会从0x0地址处运行. 如果从nor flash启动,则代码要放在nor 的0地址处: 如果从nand flash启 ...

  3. 关系型数据库MySQL主从同步-读写分离

    1.环境准备 我的数据库版本是MySQL 5.6 MySQL主机至少两个实例,可以是多实例,可以是多台主机 关闭selinux,关闭防火墙等基础优化 2.安装 yum -y install make ...

  4. test11

    -Xms512m-Xmx512m-XX:PermSize=512-XX:MaxPermSize=512

  5. Cookie文件说明及IE的Cookie文件格式

    1.Cookie文件的实质 Cookie实际上是Web服务端与客户端(典型的是浏览器)交互时彼此传递的一部分内容,内容可以是任意的,但要在允许的长度范围之内.客户端会将它保存在本地机器上(如IE便会保 ...

  6. 在Eclipse中,用XFire发布web服务

    配置及相关说明见http://www.cnblogs.com/xshy3412/archive/2007/09/29/910848.html 仅确定发布xfire需要的最基本的jar activati ...

  7. 关于Mysql+EF6本地运行和发布没有问题,发布到服务器上出现问题的解决方案

    这个问题折磨了我差不多两天,网上各种方法也找了个遍,但是都没有解决我的问题,后面通过自己仔细分析问题和排查,终于把问题解决了,以下是我的解决问题的步骤,希望能帮到各位,不要再被这些问题坑了 1,项目实 ...

  8. Android 控件:使用下拉列表框--Spinner

    ---恢复内容开始--- 一.前段代码 <Spinner android:id="@+id/spin" android:paddingTop="10px" ...

  9. DOM-访问元素

    id:元素在文档中唯一标识符. title:有关元素的附加说明信息,一般通过工具提示条显示出来. lang:元素内容的语言编码,很少使用 dir:语言方向,职位“Ltr”(从左至右).Rtl(从右至左 ...

  10. 针对Eclipse闪退的两种解决方案

    闪退情况是:双击Eclipse登陆按钮,显示图标后,紧接着关闭: 1. 到eclipse文件夹中的eclipse.ini打开编辑在最后加入下面代码保存即可 -Dorg.eclipse.swt.brow ...