1.Spring与Mybatis整合

 web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  5. <display-name>SpringMvcTest</display-name>
  6. <welcome-file-list>
  7. <welcome-file>index.html</welcome-file>
  8. <welcome-file>index.htm</welcome-file>
  9. <welcome-file>index.jsp</welcome-file>
  10. <welcome-file>default.html</welcome-file>
  11. <welcome-file>default.htm</welcome-file>
  12. <welcome-file>default.jsp</welcome-file>
  13. </welcome-file-list>
  14.  
  15. <!-- 配置上下文加载监听器,根容器由此监听器创建 -->
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.  
  20. <!-- 配置前端控制器 -->
  21. <servlet>
  22. <servlet-name>ptmvc</servlet-name>
  23. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  24. <load-on-startup>1</load-on-startup> <!-- 设置该控制器加载时机(应用启动时即加载)以及加载优先级(1) -->
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>ptmvc</servlet-name>
  28. <url-pattern>*.do</url-pattern>
  29. </servlet-mapping>
  30.  
  31. </web-app>

  applicationContext.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:tx="http://www.springframework.org/schema/tx"   //添加事物
  6. xmlns:aop="http://www.springframework.org/schema/aop"  //添加事物切面(利用面向切面的技术,即代理模式)
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop.xsd">
  15.  
  16. <context:component-scan base-package="com.frank.services"></context:component-scan>
  17.  
  18. <!-- Mybatis与Spring集成配置: -->
  19. <!-- 1.配置数据源(连接池) -->
  20. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  21. destroy-method="close">  <!-- 关闭连接对象销毁方法(数据库访问完毕后再将连接放回连接池,而不是关闭连接) -->
  22. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
  23. <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
  24. <property name="username" value="x1" />
  25. <property name="password" value="x1" />
  26. </bean>
  27.  
  28. <!-- 2.配置sqlSession工厂(MyBatils文档) -->
  29. <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <property name="dataSource" ref="dataSource" /> </bean> -->
  31. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
  32. autowire="byName">
  33. </bean>
  34.  
  35. <!-- 3.配置映射器扫描配置器MapperScannerConfigurer(MyBatils文档) -->
  36. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  37. <property name="basePackage" value="com.frank.daomapper" />
  38. </bean>
  39.  
  40. <!-- Spring中事务的配置: -->
  41. <!-- 1.配置事务管理器 -->
  42. <bean id="txManager"
  43. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  44. <property name="dataSource" ref="dataSource" />
  45. </bean>
  46.  
  47. <!-- 2.配置事务通知(即事务处理规则) -->
  48. <tx:advice id="txAdvice" transaction-manager="txManager">
  49. <tx:attributes>
  50. <tx:method name="get*" read-only="true"/> <!-- 将查询事物设置为只读 -->
  51. <tx:method name="*" propagation="REQUIRED"/> <!-- propagation="REQUIRED"为默认,可不写 -->
  52. </tx:attributes>
  53. </tx:advice>
  54.  
  55. <!-- 3.配置事物切面 (即事务规则应用的“地方”)-->
  56. <aop:config>
  57. <aop:pointcut id="servicePointCut"
  58. expression="execution(* com.frank.services.*Service.*(..))" />
  59. <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
  60. </aop:config>
  61.  
  62. </beans>

  ptmvc-servlet.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
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9.  
  10. <context:component-scan base-package="com.frank.controller"></context:component-scan>
  11. </beans>

2.Spring与Hibernate整合:

  web.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>SpringTest</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. <welcome-file>index.htm</welcome-file>
  10. <welcome-file>index.jsp</welcome-file>
  11. <welcome-file>default.html</welcome-file>
  12. <welcome-file>default.htm</welcome-file>
  13. <welcome-file>default.jsp</welcome-file>
  14. </welcome-file-list>
  15.  
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.  
  20. <servlet>
  21. <servlet-name>myservlet</servlet-name>
  22. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  23. <load-on-startup>1</load-on-startup>
  24. </servlet>
  25. <servlet-mapping>
  26. <servlet-name>myservlet</servlet-name>
  27. <url-pattern>*.do</url-pattern>
  28. </servlet-mapping>
  29.  
  30. <filter>
  31. <filter-name>CharacterEncodingFilter</filter-name>
  32. <filter-class>com.frank.filter.CharacterEncodingFilter</filter-class>
  33. <init-param>
  34. <param-name>encoding</param-name>
  35. <param-value>UTF-8</param-value>
  36. </init-param>
  37. </filter>
  38. <filter-mapping>
  39. <filter-name>CharacterEncodingFilter</filter-name>
  40. <url-pattern>/*</url-pattern>
  41. </filter-mapping>
  42. </web-app>

  applicationContext.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" xmlns:context="http://www.springframework.org/schema/context"
  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/context
  8. http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx.xsd
  11. http://www.springframework.org/schema/aop
  12. http://www.springframework.org/schema/aop/spring-aop.xsd">
  13.  
  14. <context:component-scan base-package="com.frank.services,com.frank.daomapper" />
  15.  
  16. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  17. destroy-method="close">
  18. <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  19. <property name="url"
  20. value="jdbc:sqlserver://localhost:1433;databaseName=mydb2" />
  21. <property name="username" value="sa" />
  22. <property name="password" value="root" />
  23. </bean>
  24.  
  25. <!-- SessionFactory配置 -->
  26. <bean id="sessionFactory"
  27. class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  28. <property name="dataSource" ref="dataSource" />
  29. <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
  30. <property name="mappingLocations" value="classpath:com/frank/model/*.hbm.xml"></property>
  31. <property name="hibernateProperties">
  32. <value>
  33. hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
  34. hibernate.show_sql=true
  35. </value>
  36. </property>
  37. </bean>
  38.  
  39. <bean id="txManager"
  40. class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  41. <property name="sessionFactory" ref="sessionFactory" />
  42. </bean>
  43.  
  44. <tx:advice id="txAdvice" transaction-manager="txManager">
  45. <tx:attributes>
  46. <tx:method name="get*" read-only="true" />
  47. <tx:method name="*" />
  48. </tx:attributes>
  49. </tx:advice>
  50.  
  51. <aop:config>
  52. <aop:pointcut id="servicePointCut"
  53. expression="execution(* com.frank.services.*Service.*(..))" />
  54. <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointCut" />
  55. </aop:config>
  56. </beans>

  myservlet-servlet.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
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <context:component-scan base-package="com.frank.controller" />
  10. </beans>

Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用的更多相关文章

  1. 如约而至,Java 10 正式发布! Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十四)Redis缓存正确的使用姿势 努力的孩子运气不会太差,跌宕的人生定当更加精彩 优先队列详解(转载)

    如约而至,Java 10 正式发布!   3 月 20 日,Oracle 宣布 Java 10 正式发布. 官方已提供下载:http://www.oracle.com/technetwork/java ...

  2. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(二)RESTful API实战笔记(接口设计及Java后端实现)

    写在前面的话 原计划这部分代码的更新也是上传到ssm-demo仓库中,因为如下原因并没有这么做: 有些使用了该项目的朋友建议重新创建一个仓库,因为原来仓库中的项目太多,结构多少有些乱糟糟的. 而且这次 ...

  3. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(六)一定要RESTful吗?

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 写在前面的话 这个问题看起来就显得有些萌,或者说类似的问题都有些不靠 ...

  4. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  5. SSM(Spring,SpringMVC,Mybatis)框架整合项目

    快速上手SSM(Spring,SpringMVC,Mybatis)框架整合项目 环境要求: IDEA MySQL 8.0.25 Tomcat 9 Maven 3.6 数据库环境: 创建一个存放书籍数据 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM

    写在前面的话   承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...

  7. Spring+SpringMVC+MyBatis+easyUI整合优化篇(二)Log4j讲解与整合

    日常啰嗦 上一篇文章主要讲述了一下syso和Log间的一些区别与比较,重点是在项目的日志功能上,因此,承接前文<Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)Sy ...

  8. Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能

    日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合>讲了富文本编辑器UEditor的整合与使用 ...

  9. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(一)设计一套好的RESTful API

    写在前面的话 看了一下博客目录,距离上次更新这个系列的博文已经有两个多月,并不是因为不想继续写博客,由于中间这段时间更新了几篇其他系列的文章就暂时停止了,如今已经讲述的差不多,也就继续抽时间更新< ...

随机推荐

  1. INFO: Ignoring response <403 https://movie.douban.com/top250>: HTTP status code is not handled or not allowed

    爬取豆瓣电影top250,出现以下报错: 2018-08-11 22:02:16 [scrapy.core.engine] INFO: Spider opened 2018-08-11 22:02:1 ...

  2. linux初级学习笔记十:linux grep及正则表达式!(视频序号:04_4)

    本节学习的命令:grep 本节学习的技能: grep对文本的匹配 正则表达式的使用 知识点十:grep及正则表达式(4_4) grep,egrep,fgrep: grep: 根据模式搜索文本,并将符合 ...

  3. hadoop2.x安装配置

    1.首先准备hadoop2.2.0的安装包,从官网获取,略. 2.加压安装包,进行配置.假设hadoop安装到/usr/hadoop-2.2.0目录,则进行如下配置: (1)/etc/profile配 ...

  4. 安装ubuntu gnome 16.04后的一些操作

    好吧...其实安装了挺久了,记录一下以防忘记... 1.软件 chrome浏览器 唔..自己去官网下吧.. gnome tweak tool 不用多说,必备 sudo apt-get install  ...

  5. k8s-StatefulSet控制器-十四

    一.StatefulSet概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet管理所有有状态的服务, ...

  6. centos7安装xtrabackup

    1.安装percona依赖库: yum install http://www.percona.com/downloads/percona-release/redhat/0.1-4/percona-re ...

  7. hdoj3711【水】

    题意: 给你两个集合,对于每个B集合的元素,从A集合找一个数使得a^b的二进制的1个数最少. 思路: 直接搞= = #include <bits/stdc++.h> using names ...

  8. poj1724【最短路】

    题意: 给出n个城市,然后给出m条单向路,给出了每条路的距离和花费,问一个人有k coins,在不超过money的情况下从1到n最短路径路径. 思路: 我相信很多人在上面那道题的影响下,肯定会想想,在 ...

  9. (7)javascript的程序控制结构及语句------(2)循环控制语句、跳转语句、对话框

    一.循环控制语句 循环语句主要就是在满足条件的情况下反复执行某一个操作,循环控制语句主要包括while语句.do...while语句 和for语句. 1.While语句 语法: While(条件表达式 ...

  10. 如何才能优雅地书写JS代码

    第一:关于匿名函数的使用 要避免全局变量泛滥, 可以考虑使用匿名函数, 把不需要在外部访问的变量或者函数限制在一个比较小的范围内. 例如以下代码: <script> function fu ...