基本说明:
说明:我们可以将MonsterMapperjava接口方法在对应的MonsterlMapper.xml文件中实现外,也可以直接组织MonsterMappere.java接口中声明的方法,直接使用注解来实现,可以作为一种补充的机制在项目中使用.
1、编写了 MonsterAnnotation.java 接口
public interface MonsterAnnotation {@Insert("INSERT INTO mybatis_monster_ (monster_id,age,birthday,email,gender,name,salary)\n" +" VALUES (#{monster_id},#{age},#{birthday},#{email},#{gender},#{name},#{salary})")public void addMonster(Monster monster);@Delete("DELETE FROM mybatis_monster_ WHERE monster_id=#{monster_id}")public void delMonster(Integer monster_id);@Update(" UPDATE mybatis_monster_ SET age=#{age},birthday=#{birthday},email=#{email},gender=#{gender},name=#{name},salary=#{salary}\n" +" WHERE monster_id=#{monster_id}")public void updateMonster(Monster monster);@Select("SELECT * FROM mybatis_monster_ WHERE monster_id=#{monster_id}")public Monster findById(Integer monster_id);@Select("SELECT * FROM mybatis_monster_")public List<Monster> findAllMonster();
}
2、将Monster.Annotation 注册到mybatis-config.xml文件中
<mappers>
<!-- 这里引入或者说注册我们的MonsterMapper.xml文件--><mapper resource="com/bin/mybatis/mapper/MonsterMapper.xml"/><mapper class="com.bin.mybatis.mapper.MonsterAnnotation"/>
<!-- <package name="com.bin.mybatis.mapper"/>--></mappers>
3、测试
@Testpublic void testAnotation() {Monster monster = new Monster();monster.setAge(200);monster.setBirthday(new Date());monster.setEmail("123@qq.com");monster.setGender(1);monster.setSalary(8928.00);monster.setName("大树精");MonsterAnnotation annotation = sqlsession.getMapper(MonsterAnnotation.class);annotation.addMonster(monster);}