mybatis mapper代理学习笔记

sqlMapConfig.xml的配置



<configuration>

    
    <properties resource="db.properties">
        
    properties>

    
    <typeAliases>
        
        
        
        <package name="jack.po"/>
        <package name="jack.pojo"/>
    typeAliases>

     <environments default="development">
         <environment id="development">
         
             <transactionManager type="JDBC"/>
        
                 <dataSource type="POOLED">
                     <property name="driver" value="${jdbc.driver}"/>
                     <property name="url" value="${jdbc.url}"/>
                     <property name="username" value="${jdbc.username}"/>
                     <property name="password" value="${jdbc.password}"/>
                 dataSource>
         environment>
     environments>

     <mappers>
        
        
        
        
         

         <package name="jack.employee.mapper"/>
     mappers>

configuration>

使用mapper代理有以下要求:

1.映射文件的namespace是接口的全路径
2.方法名和配置文件的statement的id对应
3.返回值类型和resulttype对应
4.参数类型和parameterType对应

先来写EmployeeMapper.xml配置文件


 
 <mapper namespace="jack.employee.mapper.EmployeeMapper">

    
    <select id="getEmployeeById" parameterType="int" resultType="employee">
        select * from t_employee where id=#{id}
    select>
    <insert id="insertToEmployee" parameterType="jack.pojo.Employee">
        insert into t_employee(name,birthday,sex,address) values(#{name},#{birthday},#{sex},#{address})
    insert>
    <select id="getEmployeeByName" parameterType="String" resultType="employee">
        select * from t_employee where name like '%${value}%'
    select>
    <select id="getEmloyee" parameterType="employee" resultType="employee">
        select e.name name_,e.birthday bir_ from t_employee e where e.name like '%${name}%' and e.sex=#{sex}
    select>
    <select id="getEmloyee2" parameterType="employee" resultMap="employeeResMap">
        select e.name name_,e.birthday bir_ from t_employee e where e.name like '%${name}%' and e.sex=#{sex}
    select>
    <resultMap type="employee" id="employeeResMap">
        <result column="name_" property="name"/>
        <result column="bir_" property="birthday"/>
    resultMap>
 mapper>

下面是EmployeeMapper接口代码:

package jack.employee.mapper;

import java.util.List;

import jack.pojo.Employee;

public interface EmployeeMapper {

    /**
     * 按照员工id查找员工
     * */
    public Employee getEmployeeById(Integer id);

    /**
     * 插入一条员工记录
     * */
    public void insertToEmployee(Employee employee);

    /**
     * 按照名称模糊查询
     * */
    public List getEmployeeByName(String name);

    /**
     * 高级搜索
     * */
    public List getEmloyee(Employee employee);

    public List getEmloyee2(Employee employee);
}

mybatis会自动帮我们生成实现类,代理对象,可以自己打印mapper对象看一下
测试代码如下:

package jack.employee.mapper;

import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import jack.pojo.Employee;

public class EmployeeMapperTest {

    private SqlSessionFactory factory;

    @Before
    public void setUp() throws Exception {
        InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
        factory = new SqlSessionFactoryBuilder().build(stream);
    }


    @Test
    public void testGetEmployeeById() {
        /*mapper**/
        SqlSession session = factory.openSession();
        EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
        Employee e = mapper.getEmployeeById(1);
        System.out.println(e);
    }
}

你可能感兴趣的:(mybatis)