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>

最新文章

  1. awk匹配
  2. Windows动态库学习心得
  3. jQuery中时间戳和日期的相互转换
  4. [原创.数据可视化系列之一]使用openlayers 3 显示聚合数据
  5. CentOS6.5安装tomcat7
  6. hadoop2.0中无法启动datanode的问题
  7. WIND2003 安装Zend studio 报错
  8. 标准初始化css样式表
  9. 【转】Xcode7.1环境下上架iOS App到AppStore 流程 (Part 三)
  10. Devexpress XtraReports 交叉报表
  11. Angular - - ngRoute Angular自带的路由
  12. 11.巨坑,注意了,关于显示不正常的问题,localstorage的存储问题
  13. NI笔试——大数加法
  14. go语言基础知识笔记(二)之数组和切片
  15. quartz 每天0点5分开始,以后每隔15分钟启动一次,23:50停止
  16. [JS] Topic - define &quot;class&quot; by tricky methods
  17. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
  18. STL基础--仿函数(函数对象)
  19. Linux入门基础教程之Linux下软件安装
  20. linux编码问题小节

热门文章

  1. 安装python包时遇到&quot;error: Microsoft Visual C++ 9.0 is required&quot;的简答(Python2.7)
  2. 基于alpine的php-fpm扩展swoole和pdo_mysql
  3. [Luogu]中位数
  4. (转载)设置虚拟机桥接模式以及解决桥接模式上不了网以及ping不通主机的问题
  5. 第一篇,VScode插架以及配置项
  6. Quartus ii 初学遇到的问题以及解决
  7. 百度地图使用http ,https
  8. 1.EntityManaget的persist和merge方法的区别
  9. django 模型增加字段后迁移失败
  10. 微信或QQ屏蔽了我的域名,已经被微信屏蔽的域名如何在微信打开,如何进行微信域名防封?