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-web的jar包 主要的用途是用来当前项目初始化就加载配置文件并生成IOC容器的
 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
"> <!--启用注解-->
<context:annotation-config></context:annotation-config>
<!--引入外部的属性文件jdbc.properties-->
<context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder>
<!--配置扫描路径-->
<context:component-scan base-package="com.blb"></context:component-scan>
<!--将druid连接池加入到IOC容器中-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/rbac2?useUnicode=true&amp;characterEncoding=utf8&amp;characterSetResults=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property> </bean> <!--分页插件-->
<bean id="pageHelper" class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean> <!--sqlSessionFactoryBean加入到容器中-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="plugins">
<array>
<ref bean="pageHelper"></ref>
</array>
</property>
<property name="typeAliasesPackage" value="com.blb.dto"></property>
<property name="mapperLocations" value="classpath*:*com/blb/mapper/*.xml"></property>
</bean>
<!--sqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactoryBean"></constructor-arg>
</bean>
<!--配置jdbc包中切面-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置建议规则-->
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="select*" read-only="true"></tx:method>
<tx:method name="query*" read-only="true"></tx:method>
<tx:method name="insert*" propagation="REQUIRED"></tx:method>
<tx:method name="save*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="modify*" propagation="REQUIRED"></tx:method>
<tx:method name="remove*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice> <!--开启切面代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--先来实现关注点-->
<aop:config>
<aop:pointcut id="service" expression="execution(* com.blb.service..*(..))"/>
<aop:advisor advice-ref="tx" pointcut-ref="service"></aop:advisor>
</aop:config> </beans>
 @Log4j
@WebServlet("/user")
public class UserController extends HttpServlet { private UserService userService; @Override
public void init() throws ServletException {
WebApplicationContext webcontainer = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
this.userService= (UserService)webcontainer.getBean("userServiceImpl");
      //与IOC容器交互,从IOC容器里拿到userService单列
} @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String m = req.getParameter("m");
if(m.equals("save"))
{
save(req,resp);
}
else if(m.equals("update"))
{
update(req,resp);
}
else if(m.equals("delete"))
{
delete(req,resp);
}
else if(m.equals("select"))
{
select(req,resp);
}
else if(m.equals("selectAll"))
{
selectAll(req,resp);
} }
 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--1.告诉spring的核心配置文件叫什么名字-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<!--2.我给你一个监听器 监听器用来使用上面的配置,加载配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
												

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. 使用node.js实现多人聊天室(socket.io、B/S)

    通过B/S架构实现多人聊天,客户端连接服务器,发送信息,服务器接收信息之后返回给客户端. 主要是通过socket.io实现浏览器和服务器之间进行实时,双向和基于事件的通信. socket.io官方文档 ...

  2. EditPlus 注册码在线生成

    虽然editplus现在不常用,但是它轻便,我还是很喜欢的,推荐一个注册码生成器,真是好好用 http://www.jb51.net/tools/editplus/ 点击链接输入自己想要的用户名,就能 ...

  3. 201771010135 杨蓉庆《面对对象程序设计(java)》第十六周学习总结

    1.实验目的与要求 (1) 掌握线程概念: (2) 掌握线程创建的两种技术: (3) 理解和掌握线程的优先级属性及调度方法: (4) 掌握线程同步的概念及实现技术: 一.理论知识 ⚫ 线程的概念 (1 ...

  4. Go之NSQ

    文章引用自 NSQ NSQ是目前比较流行的一个分布式的消息队列,本文主要介绍了NSQ及Go语言如何操作NSQ. NSQ介绍 NSQ是Go语言编写的一个开源的实时分布式内存消息队列,其性能十分优异. N ...

  5. 【C语言】(数组方式)求n名同学的平均成绩

    原理就不说了 代码: #include <stdio.h> int main() { ], sum = ; int i; printf("请输入5名童鞋的成绩:\n") ...

  6. 运维数据同步工具:rsync,serync,csync,drbd,info(基于文件系统)

     Rsync官方站点:http://rsync.samba.org 1. Rsync rsync是一款开源的实现数据全量与增量同步备份的工具 生产环境中使用的场景: 1:一般用于数据异地备份 2:用于 ...

  7. C语言:计算并输出给定10个数的方差。

    //计算并输出给定10个数的方差. #include<math.h> #include<stdio.h> ]) { double p = 0.0,f=0.0,g=0.0; ; ...

  8. 2.2.FastDFS-单机拆分版-存储器安装配置

    Centos610系列配置 我们在Centos610FastDFS单机模式-FastDFS安装 中已经完成了FastDFS的安装,接下来我们进行FastDFS存储器的安装. 1.找到FastDFS配置 ...

  9. 「ZJOI2008」树的统计

    树剖模板题啊! 这道题的话,最通(jian)俗(dan)易(cu)懂(bao)的解法应该就是树剖了. 加上线段树维护树上路径的最大权值(\(Max\))和路径和(\(sum\)). 至于\(LCT\) ...

  10. Django框架之模板路径及静态文件路径配置

    内容: (1)模板文件路径的配置 (2)静态文件路径的配置 一.模板文件路径的配置 模板文件主要通过jinja2模板进行渲染html页面,实现动态页面. 步骤一:创建一个template的文件夹,用于 ...