• MyBatic与Spring的整合,Mapper接口方式的开发


    本文摘自:Java EE企业级应用开发教程,有部分修改

    在MyBatis+Spring项目中,虽然使用传统DAO的开发可以实现所需功能,但是采用这种方式在实现类中会出现大量重复代码,在方法中也需要指定映射文件中执行语句的Id,并且不能保证编写时的Id的正确性(运行时才能知道)。为此,我们可以使用MyBatis提供的另一种编程方式,Mapper接口编程。

    本文假定你的MyBatis与Spring环境已经搭建完毕,数据结构及测试数据也已经导入数据库,实体类(Customer.java)也已经创建,参考:MyBatic与Spring的整合,传统DAO方式的开发

    三、Mapper接口方式的开发

    1、创建接口类

    package com.itheima.mapper;
    import com.itheima.po.Customer;
    public interface CustomerMapper {
        // 通过id查询客户
        public Customer findCustomerById(Integer id);
        
        // 添加客户
        public void addCustomer(Customer customer);
    }

    2、创建映射文件

    <?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">
    <mapper namespace="com.itheima.mapper.CustomerMapper">
        <!--根据id查询客户信息 -->
        <select id="findCustomerById" parameterType="Integer"
                 resultType="customer">
            select * from t_customer where id = #{id}
        </select>
        
        <!--添加客户信息 -->
        <insert id="addCustomer" parameterType="customer">
            insert into t_customer(username,jobs,phone)
            values(#{username},#{jobs},#{phone})
        </insert>
    </mapper>

    注意:此文件与接口文件位于同一个目录。

    3、在mybatis-config-spring.xml配置文件中,增加如下代码:

        <mappers> 
           <mapper resource="com/itheima/mapper/CustomerMapper.xml" />
        </mappers>

    4、在applicationContext-mybatis.xml配置文件中,增加如下代码:

        <!-- Mapper代理开发(基于MapperFactoryBean) -->
        <bean id="customerMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="mapperInterface" value="com.itheima.mapper.CustomerMapper" />
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />  
        </bean>

    上述配置代码为MapperFactoryBean指定了接口以及SqlSessionFactory。

    5、创建测试类

    package com.itheima.test;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import 
         org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.itheima.mapper.CustomerMapper;
    import com.itheima.po.Customer;
    
    /**
     * DAO测试类
     */
    public class DaoTest {
    //    @Test
        public void findCustomerByIdMapperTest(){    
            ApplicationContext act = 
                    new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
            CustomerMapper customerMapper = act.getBean(CustomerMapper.class);   
            Customer customer = customerMapper.findCustomerById(1);
            System.out.println(customer);
        }
        
        @Test
        public void addCustomerTest(){    
            ApplicationContext act = 
                    new ClassPathXmlApplicationContext("applicationContext-mybatis.xml");
            CustomerMapper customerMapper = act.getBean(CustomerMapper.class);   
            Customer customer = new Customer();
            customer.setUsername("李咏");
            customer.setJobs("测试哦");
            customerMapper.addCustomer(customer);
        }
    }

    addCustomerTest方法输出:

    DEBUG [main] - ==>  Preparing: insert into t_customer(username,jobs,phone) values(?,?,?) 
    DEBUG [main] - ==> Parameters: 李咏(String), 测试哦(String), null
    DEBUG [main] - <==    Updates: 1

    findCustomerByIdMapperTest输出:

    DEBUG [main] - ==>  Preparing: select * from t_customer where id = ? 
    DEBUG [main] - ==> Parameters: 1(Integer)
    DEBUG [main] - <==      Total: 1
    Customer(id=1, username=张三, jobs=测试工程师, phone=13099992222)

    几条建议:

    1、Mapper接口的名称和对应的Mapper.xml映射文件的名称必须一致;

    2、Mapper.xml文件中的namespace与Mapper接口的类路径相同(放在同一个包中);

    3、Mapper接口中的方法名和Mapper.xml中定义的每个执行语句的id相同;

    4、Mapper接口中方法的输入参数类型要与Mapper.xml中定义的每个SQL的parameterType的类型相同;

    5、Mapper接口方法的输出参数类型要和Mapper.xml中定义的每个SQL的resultType的类型相同。

  • 相关阅读:
    js控制滚动条滑动
    js二维码插件总结
    wer
    验证插件——jquery.validate.js
    .py小程序总结
    Linux中xargs命令的使用
    Cacti、解决cacti的snmp error
    Cacti安装使用
    用Python发一个高逼格的朋友圈
    windows安装zabbix-agent
  • 原文地址:https://www.cnblogs.com/nayitian/p/15267035.html
Copyright © 2020-2023  润新知