前面说了基于 PersonMappe.xml 配置文件的操作, 现在咱们说一下基于注解的配置如何操作.
一般我们不用基于注解的配置.
大体了解一下吧,
首先, 要想使用基于注解的配置, 首先得把之前建立的 PersonMapper.xml 文件删掉, 然后在 cong.xml 文件中接触注册.
一: 定义 SQL 映射的接口
package com.mybatis.entities; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; public interface PersonMapper { @Insert("INSERT INTO person(name, age) VALUES(#{name}, #{age})") public int insertPerson(Person Person); @Delete("DELETE FROM person WHERE id=#{id}") public int deletePersonById(int id); @Update("UPDATE person SET name=#{name},age=#{age} WHERE id=#{id}") public int updatePerson(Person Person); @Select("SELECT id, name, age FROM person WHERE id=#{id}") public Person getPersonById(int id); @Select("SELECT id, name, age FROM Person") public List<Person> getAllPerson(); }
二: 在conf.xml中注册
<mappers> <mapper class="com.mybatis.entities.PersonMapper"/> </mappers>
三: 调用
package com.mybatis.test; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.mybatis.entities.Person; import com.mybatis.entities.PersonMapper; import com.mybatis.util.MyBatisUtil; public class TestPersonAnnotation { @Test public void testGetPersonById(){ SqlSession session = MyBatisUtil.getSqlSession(); PersonMapper mapper = session.getMapper(PersonMapper.class); Person person = mapper.getPersonById(2); System.out.println(person); } }
就这么点东西.
本篇博文源代码点击 http://pan.baidu.com/s/1YVgce 下载.
本文出自 “专注Java,linux技术” 博客,请务必保留此出处http://wuqinglong.blog.51cto.com/9087037/1723196
原文:http://wuqinglong.blog.51cto.com/9087037/1723196