Mybatis 代理dao的开发 基于注解

代理Dao的开发需要遵守的规范:
A:将映射文件和接口放在同一个包中并文件的主文件名相同
B:映射文件的命名空间必须是接口的完整包名+接口名,即接口地址
C:映射文件中的mappedstatementId的值必须和接口的方法名相同
D:输入映射参数的类型必须和接口中方法的形参的数据类型相同
E:输出映射参数的类型必须和接口中方法返回值的数据类型相同(如果返回单个对象类型必须相同,如果返回集合则要求集合中元素的数据类型和输出映射的类型相同)
注意:映射文件的注册方式有两种:
A:在mybatis配置文件中逐个注册



B:批量注册


例子
PersonDao类

public interface PersonDao {

    @Insert("insert into t_person values(null,#{name},#{age},#{sex},#{birthday})")
    void add(Person person) throws Exception;

    @Delete("delete from t_person where id=#{id}")
    void delete(int id);

    @Update("update t_person set name=#{name},age=#{age},sex=#{sex},birthday=#{birthday} where id=#{id}")
    void update(Person person);

    @Select("select *from t_person where id=#{id}")
    Person getPersonById(int id)throws Exception;

    @Select("select *from t_person")
    List<Person> getPersons()throws Exception;

    @Select("select *from t_person where name like '%' #{condition} '%'")
    List<Person> getPersonsByLike(String condition)throws Exception;


    @Select("select *from t_person where id=#{id}")
    Map getMapById(int id)throws Exception;

    @Select("select *from t_person")
    List<Map<String,Object>> getMaps()throws Exception;

    @Select("select *from t_person where name like '%' #{condition} '%'")
    List<Map<String,Object>> getMapsByLike(String condition)throws Exception;

}

mybatis的配置文件


/* 在基于注解的单表操作中,不支持函数重载,否则报函数名已经存在的错误。
 *  
 * 在基于注解的操作中,如果使用SqlSession的selectOne(),selectList(),insert(),update(),delete()等
 * 方法时,需要注意的是:statement的取值必须是:接口的完整包名+接口名+当前操作方法的名称。
 */


/**
     * 当数据库表的列名和实体的属性名不匹配时则无法完成自动属性的注入,解决办法:
     * A:给select语句查询的列起别名
@Select("select p_id as id ,p_name as name,p_age as age,p_sex as  sex,p_birthday as birthday from t_person where p_id=#{id}")


     * B:手工进行映射关系的匹配
        @Results(
            {
                @Result(column="p_id",property="id"),   
                @Result(column="p_name",property="name"),   
                @Result(column="p_age",property="age"), 
                @Result(column="p_sex",property="sex"), 
                @Result(column="p_birthday",property="birthday")    
            }
    )
*/
public class PersonDaoTest {

    private PersonDao personDao;
    @Before
    public void setUp() throws Exception {
        **SqlSession sqlSession=MyBatisUtils.getSqlSession();

        personDao=sqlSession.getMapper(PersonDao.class);**
    }

    @Test
    public void testAdd() {
        try {
            this.personDao.add(new Person("小美", 18,"女", new Date()));
            System.out.println("插入成功!");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testDelete() {
        int id=5;
        this.personDao.delete(id);
        System.out.println("数据删除成功!");
    }

    @Test
    public void testUpdate() {
        int id=5;
        try {
            Person person=this.personDao.getPersonById(id);
            if(person!=null){
                person.setName("媚儿");
                person.setAge(20);

                this.personDao.update(person);
                System.out.println("修改成功!");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetPersonById() {
        int id=1;
        try {
            Person person=this.personDao.getPersonById(id);
            System.out.println(person);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetPersons() {
        try {
            List<Person> persons=this.personDao.getPersons();
            System.out.println(persons);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetPersonsByLike() {
        String condition="小";
        try {
            List<Person> persons=this.personDao.getPersonsByLike(condition);
            System.out.println(persons);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetMapById() {
        int id=1;
        try {
            Map<String,Object> map=this.personDao.getMapById(id);
            System.out.println(map);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetMaps() {
        try {
            System.out.println(this.personDao.getMaps());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void testGetMapsByLike() {
        String condition="小";
        try {
            List<Map<String,Object>> maps=this.personDao.getMapsByLike(condition);
            System.out.println(maps);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值