1. 导入相关包:Spring包:Spring架包 MyBatis包:MyBatis架包 整合包:Mybatis-Spring整合包

  2. 编写实体类User,实体类的sql映射文件,映射内容如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <typeAliases>
    <package name="com.lxy.entity"/>
    </typeAliases> <mapper namespace="com.lxy.entity.UserMapper">
    <select id="selectUsers" resultType="com.lxy.entity.User">
    select * from users
    </select>
    </mapper>
  3. 创建实体类的dao接口UserDao和接口的实现类UserDaoImp,在实现类里创建一个sqlSessionTemplate类变量sqlSession,设置sqlSession的set方法。在实现接口的方法中调用sqlSession的selectList方法并返回代码如下:

    public class UserDaoImp implements UserDao {
    
        private SqlSessionTemplate sqlSession;
    @Override
    public List<User> selectUser() {
    //参数为映射文件的namespace+查询语句的id
    return sqlSession.selectList("com.lxy.entity.UserMapper.selectUsers");
    } public void setSqlSession(SqlSessionTemplate sqlSession) {
    this.sqlSession = sqlSession;
    } }

    Mybatis工具类要交由spring容器来处理和实例化,不再需要另外编写。此时的项目结构如图:

  4. 在Spring的xml配置下编写数据库连接配置,例如:

    <?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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/springExercise"/>
    <property name="username" value="root"/>
    <property name="password" value="myPassword"/>
    </bean> <!--配置sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean> <!--sqlSessionFactory利用构造器引用了Factory-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean> <bean id="userDao" class="com.lxy.dao.imp.UserDaoImp">
    <property name="sqlSession" ref="sqlSessionTemplate" />
    </bean>
  5. 编写测试主方法,如:

    public class test {
    public static void main(String[] args) throws IOException {
    ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
    UserDao userDao= (UserDao) context.getBean("userDao");
    System.out.println(userDao.selectUser().size());
    }
    }
  6. 配置声明式事务,为了使用事务,先修改mapper文件的映射,添加不同的操作:

    <mapper namespace="com.lxy.entity.UserMapper">
    <select id="selectUsers" resultType="com.lxy.entity.User">
    select * from users
    </select>
    <insert id="add" parameterType="User" useGeneratedKeys="true">
    insert into users(name,pwd) values(#{name},#{pwd})
    </insert>
    <delete id="remove">
    deletes from users where id=#{id}
    </delete>
    </mapper>

    需要修改Beans.xml的头部beans配置:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

    再添加事务的和aop的配置:

    <!--声明式事务-->
    <!--配置事务管理器-->
    <bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务的通知-->
    <tx:advice id="txAdvice" transaction-manager="txManger">
    <tx:attributes>
    <!--配置哪些方法使用什么样的事务,配置事务的传播特性-->
    <!--REQUIRED表示如果没有当前事务就开启一个事务-->
    <tx:method name="add" propagation="REQUIRED"/>
    <!--带*号匹配所有以remove为开头的方法名字-->
    <tx:method name="remove*" propagation="REQUIRED"/>
    <!--使用read-only为true,只读就不会提交事务-->
    <tx:method name="selectUsers" read-only="true"/>
    <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice> <aop:config>
    <!--这里的表达式中实际运用时应该配置在service层,这里是因为没有写出service所以就使用dao层-->
    <aop:pointcut id="pointCut" expression="execution(* com.lxy.dao.imp.*.*(..))"/>
    <aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/>
    </aop:config>

MyBatis-Spring整合之方式1的更多相关文章

  1. springMVC+mybatis+spring整合案例

    1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...

  2. springMVC+MyBatis+Spring 整合(3)

    spring mvc 与mybatis 的整合. 加入配置文件: spring-mybaits.xml <?xml version="1.0" encoding=" ...

  3. SpringMVC+Mybatis+Spring整合

    Maven引入需要的JAR包 pom.xml <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEA ...

  4. MyBatis Spring整合配置映射接口类与映射xml文件

    本文转自http://blog.csdn.net/zht666/article/details/38706083 Spring整合MyBatis使用到了mybatis-spring,在配置mybati ...

  5. Mybatis——Spring整合

    一.引入依赖 Spring的相关jar包 mybatis-3.4.1.jar mybatis-spring-1.3.0.jar mysql-connector-java-5.1.37-bin.jar ...

  6. springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题

    解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...

  7. Mybatis+Spring整合后Mapper测试类编写

    public class UserMapperTest { private ApplicationContext applicationContext; @Before public void ini ...

  8. springmvc+mybatis+spring 整合源码项目

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类,service等 ...

  9. springmvc+mybatis+spring 整合

    获取[下载地址]   [免费支持更新]三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] ...

  10. springmvc+mybatis+spring 整合 bootstrap

    获取[下载地址]   [免费支持更新]三大数据库 mysql  oracle  sqlsever   更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] ...

随机推荐

  1. 使用 C++11 编写可复用多线程任务池

    类的功能 Task (任务基类) 该类主要实现一个任务类 virtual int doWork() = 0; TaskQueue (任务队列) 该类主要针对任务的存储.删除.撤回等状态做管理 Thre ...

  2. [Java IO]05_JSON操作

    目录 6.1 JSON 知识背景  6.1.1 JSON 简介  6.1.2 JSON 语法  6.1.3 JSON 的数据结构6.2 Java 中操作 JSON 数据  6.2.1 Jar包下载   ...

  3. poj1321棋盘问题(dfs+摆放问题)

    在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C. ...

  4. 测度论--长度是怎样炼成的[zz]

    http://www.58pic.com/newpic/27882296.html http://www.58pic.com/newpic/27893137.html http://699pic.co ...

  5. Linux - Shell - 免密码登录

    概述 简述 linux ssh 无密码登录 无能狂怒 最近真是不知道写啥了 环境 os centos7 1. 场景 场景 主机A 需要经常访问 主机B 每次访问, 都要输入一次 密码 问题 每次都输密 ...

  6. VScode 编辑器快捷键被占用

    在家办公这个开发环境实在是让人受不鸟 .mysql .vscode 对连QQ都没有运行还是不行.排查最终发现了这个家伙—搜狗输入法.微软输入法 这两个把系统快捷键占得那个全 我这个全局搜索死活用不了, ...

  7. C++ 深拷贝实例-改变原生数组

    深拷贝 main.cpp #include <stdio.h> #include "IntArray.h" int main() { IntArray a(); ; i ...

  8. laravel中如何区分get数据和post数据

    可能有时候会碰到这样的场景: 有一个post提交请求,请求地址为:xxx?id=10,提交的post数据为:id=20而在控制器中,我要同时得到get和post中数据get中的id为10,而post中 ...

  9. Unity中常用的数据结构总结

    本篇博文对U3D经常用到的数据结构和各种数据结构的应用场景总结下. 1.几种常见的数据结构 这里主要总结下在工作中常碰到的几种数据结构:Array,ArrayList,List<T>,Li ...

  10. 整理收集的一些常用java工具类

    1.json转换工具 package com.taotao.utils; import java.util.List; import com.fasterxml.jackson.core.JsonPr ...