1. 1.导入相关的jar
    druid
    mybatis
    mybatis-spring
    pageHelper
    mysql驱动包
    spring-context-support
    spring-aspect
    spring-jdbc 事务切面
    spring-tx 事务建议
    spring-web
    servlet-api
    jsp-api
    jstl
    standard
    lombok
    log4j
    2.相关的配置文件
    jdbc.propertites
    applicationContext.xml
    *mapper.xml
    log4j.properties
    3.记住一些核心类
    DruidDataSource 德鲁伊的核心类
    SqlSessionFactoryBean mybatis-spring变为mybatis-config.xml
    DataSourceTransactionManager 事务切面
    做建议 read-only
    4.步骤
    1.先导入jar
    2.编写核心配置文件
    2.1 数据源处理
    2.2 SqlSessionFactoryBean
    2.3 SqlSessionTemplate
    2.4 切面 DataSourceTransactionManager
    2.5 AOP 关注点 execution(* com.blb.service..*(..)) 关注的都是业务层,要么都执行,要么都不执行
    2.6 为当前事务设置一些建议,哪些方法要事务,哪些方法不要事务
    增删改 一定要事务
    查询 可加可不加
    2.7 构建项目
    分层设计 遵循三层的设计原则 BeanUtils用于进行多层实体类之间的数据转换
    控制层 controller/action vo
    业务层 biz/service bo
    持久层 dao/repository
    实体类 domain/pojo/dto/model/bean
    3.具体实现
    3.1 编写映射文件 SQL语句 mybatis讲究的是代码和SQL分离
    3.2 DAO中注入sqlSession
    3.3 编写持久层代码
    3.4 编写业务层代码
    3.5 编写控制层代码
    4.和我们的web项目做一个整合
    我们必须引入spring-webjar 主要的用途是用来当前项目初始化就加载配置文件并生成IOC容器的
  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:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  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/context
  11. http://www.springframework.org/schema/context/spring-context-4.1.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  16. ">
  17.  
  18. <!--启用注解-->
  19. <context:annotation-config></context:annotation-config>
  20. <!--引入外部的属性文件jdbc.properties-->
  21. <context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder>
  22. <!--配置扫描路径-->
  23. <context:component-scan base-package="com.blb"></context:component-scan>
  24. <!--将druid连接池加入到IOC容器中-->
  25. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  26. <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  27. <property name="url" value="jdbc:mysql://localhost:3306/rbac2?useUnicode=true&amp;characterEncoding=utf8&amp;characterSetResults=utf8"></property>
  28. <property name="username" value="root"></property>
  29. <property name="password" value="root"></property>
  30.  
  31. </bean>
  32.  
  33. <!--分页插件-->
  34. <bean id="pageHelper" class="com.github.pagehelper.PageInterceptor">
  35. <property name="properties">
  36. <props>
  37. <prop key="helperDialect">mysql</prop>
  38. </props>
  39. </property>
  40. </bean>
  41.  
  42. <!--sqlSessionFactoryBean加入到容器中-->
  43. <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  44. <property name="dataSource" ref="dataSource"></property>
  45. <property name="plugins">
  46. <array>
  47. <ref bean="pageHelper"></ref>
  48. </array>
  49. </property>
  50. <property name="typeAliasesPackage" value="com.blb.dto"></property>
  51. <property name="mapperLocations" value="classpath*:*com/blb/mapper/*.xml"></property>
  52. </bean>
  53. <!--sqlSessionTemplate-->
  54. <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
  55. <constructor-arg index="0" ref="sqlSessionFactoryBean"></constructor-arg>
  56. </bean>
  57. <!--配置jdbc包中切面-->
  58. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  59. <property name="dataSource" ref="dataSource"></property>
  60. </bean>
  61. <!--配置建议规则-->
  62. <tx:advice id="tx" transaction-manager="transactionManager">
  63. <tx:attributes>
  64. <tx:method name="get*" read-only="true"/>
  65. <tx:method name="select*" read-only="true"></tx:method>
  66. <tx:method name="query*" read-only="true"></tx:method>
  67. <tx:method name="insert*" propagation="REQUIRED"></tx:method>
  68. <tx:method name="save*" propagation="REQUIRED"></tx:method>
  69. <tx:method name="update*" propagation="REQUIRED"></tx:method>
  70. <tx:method name="modify*" propagation="REQUIRED"></tx:method>
  71. <tx:method name="remove*" propagation="REQUIRED"></tx:method>
  72. <tx:method name="delete*" propagation="REQUIRED"></tx:method>
  73. </tx:attributes>
  74. </tx:advice>
  75.  
  76. <!--开启切面代理-->
  77. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  78. <!--先来实现关注点-->
  79. <aop:config>
  80. <aop:pointcut id="service" expression="execution(* com.blb.service..*(..))"/>
  81. <aop:advisor advice-ref="tx" pointcut-ref="service"></aop:advisor>
  82. </aop:config>
  83.  
  84. </beans>
  1. @Log4j
  2. @WebServlet("/user")
  3. public class UserController extends HttpServlet {
  4.  
  5. private UserService userService;
  6.  
  7. @Override
  8. public void init() throws ServletException {
  9. WebApplicationContext webcontainer = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  10. this.userService= (UserService)webcontainer.getBean("userServiceImpl");
  11.       //与IOC容器交互,从IOC容器里拿到userService单列
  12. }
  13.  
  14. @Override
  15. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  16. String m = req.getParameter("m");
  17. if(m.equals("save"))
  18. {
  19. save(req,resp);
  20. }
  21. else if(m.equals("update"))
  22. {
  23. update(req,resp);
  24. }
  25. else if(m.equals("delete"))
  26. {
  27. delete(req,resp);
  28. }
  29. else if(m.equals("select"))
  30. {
  31. select(req,resp);
  32. }
  33. else if(m.equals("selectAll"))
  34. {
  35. selectAll(req,resp);
  36. }
  37.  
  38. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  5. http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  6. version="3.1">
  7. <!--1.告诉spring的核心配置文件叫什么名字-->
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>classpath*:applicationContext.xml</param-value>
  11. </context-param>
  12. <!--2.我给你一个监听器 监听器用来使用上面的配置,加载配置文件-->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. </web-app>
  1.  

Servlet+Spring+Mybatis初试的更多相关文章

  1. Idea SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  2. SpringMVC+Spring+MyBatis+Maven调整【转】

    Idea SpringMVC+Spring+MyBatis+Maven整合   创建项目 File-New Project 选中左侧的Maven,选中右侧上方的Create from archetyp ...

  3. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

  4. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  5. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  6. 2.springMVC+spring+Mybatis整合

    前面已经说了,springMVC+spring+Mybatis的整合思路了,现在就照着这个思路来做一下: 在开始之前先来看一下工程的目录结构: config这个目录底下放的都是配置文件: mybati ...

  7. Spring+Mybatis+SpringMVC+Maven+MySql搭建实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了如何使用Maven来搭建Spring+Mybatis+SpringMVC+M ...

  8. SpringMVC+Spring+MyBatis整合完整版Web实例(附数据)

    最近段时间正在学习Spring MVC和MyBatis的一些知识.自己也在网络上面找了一些例子来练习.但是都不是很完整.所以,今天,自己也抽空写了个完成的关于Spring MVC + Spring + ...

  9. 【整理】JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系

    #[整理]JavaEE基本框架(Struts2+Spring+MyBatis三层,Struts MVC)之间的关系 ![关系图解](http://images.cnitblog.com/blog/84 ...

随机推荐

  1. 每天进步一点点------Verilog 测试平台(Testbench) (一)

    每天进步一点点------Verilog 测试平台(Testbench) (一)

  2. 红帽RHCE培训-课程2笔记目录

    目录 1 kickstart自动安装 DHCP+TFTP(syslinux) +FTP +KICKSTART ~/anaconda-ks.cfg system-config-kickstart 2 g ...

  3. linux默认的目录结构

    /: 根目录/root: root账户的home目录/home: 用户的目录,每个用户有一个home/bin: 可执行文件和命令/lib: 库文件/etc: 配置文件存放地/usr: 用户的应用程序和 ...

  4. pipreqs (找当前项目依赖的包)

    pipreqs pipreqs可以帮你找到当前项目的所有组件及其版本.就是当别人给你一个程序的时候,你要在自己电脑上运行起来,就需要安装程序所依赖的组件,总不能自己一个一个找吧. # 安装 pip3 ...

  5. Educational Codeforces Round 76 (Rated for Div. 2)E(最长上升子序列)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[200007],b[200007] ...

  6. Java方法的定义和使用

    /* 定义一个方法的格式: public static void 方法名称() { 方法体 } 方法名称的命名规则和变量一样,使用小驼峰. 方法体:也就是大括号当中可以包含任意条语句. 注意事项: 1 ...

  7. flex布局构建大屏框架并支持翻页动画、滚动表格功能

      本文将利用flex属性构建大屏可视化界面.界面主要分标题栏.工具栏.数据可视化窗口.其中,翻页动画以及滚动表格功能分别分布在数据可视化界面两侧. 鼠标点击标题,可看到左侧窗口翻转动画: 整体布局效 ...

  8. SpringMVC Controller 接收页面传递的中文参数出现乱码

    在Controller中接收到的POST参数如果是中文的话,显示为乱码.已知客户端传过来时编码为UTF-8. 问题产生分析: spring MVC中默认的编码格式为“ISO-8859-1”,因此造成乱 ...

  9. BFS(广度优先搜索遍历保存全局状态,华容道翻版做法)--08--DFS--蓝桥杯青蛙跳杯子

    题目描述 X星球的流行宠物是青蛙,一般有两种颜色:白色和黑色. X星球的居民喜欢把它们放在一排茶杯里,这样可以观察它们跳来跳去. 如下图,有一排杯子,左边的一个是空着的,右边的杯子,每个里边有一只青蛙 ...

  10. 「HNOI2012」永无乡

    传送门 Luogu 解题思路 很容易想到平衡树,然后还可以顺便维护一下连通性,但是如何合并两棵平衡树? 我们采用一种类似于启发式合并的思想,将根节点siz较小的那颗平衡树暴力的合并到另一颗上去. 那么 ...