Mybatis注解方式实现CURD
2016-07-19 17:20
483 查看
上一篇演示了如何使用XML来操作Mybatis实现CRUD,但是大量的XML配置文件的编写是非常烦人的。因此
Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。
首先是创建一个接口。
[java] view
plain copy
print?
package com.bird.mybatis.bean;
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 UserMapper {
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int add(Users user);
@Delete("delete from users where id = #{id}")
public int deleteById(int id);
@Update("update users set name = #{name}, age = #{age} where id = #{id}")
public int update(Users user);
@Select("select * from users where id = #{id}")
public Users getUserById(int id);
@Select("select * from users")
public List<Users> getAllUsers();
}
然后一定不要忘了在conf.xml配置文件中,注册这个类
[java] view
plain copy
print?
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
<mapper class="com.bird.mybatis.bean.UserMapper"/>
</mappers>
下面就是使用这个类了
[java] view
plain copy
print?
@Test
public void testAdd2() {
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.add(new Users(-1,"娃娃",99));
openSession.commit();
openSession.close();
}
Mybatis也提供了基于注解的配置方式,下面我们来演示一下使用接口加注解来实现CRUD的的例子。
首先是创建一个接口。
[java] view
plain copy
print?
package com.bird.mybatis.bean;
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 UserMapper {
@Insert("insert into users(name, age) values(#{name}, #{age})")
public int add(Users user);
@Delete("delete from users where id = #{id}")
public int deleteById(int id);
@Update("update users set name = #{name}, age = #{age} where id = #{id}")
public int update(Users user);
@Select("select * from users where id = #{id}")
public Users getUserById(int id);
@Select("select * from users")
public List<Users> getAllUsers();
}
然后一定不要忘了在conf.xml配置文件中,注册这个类
[java] view
plain copy
print?
<mappers>
<mapper resource="com/bird/mybatis/bean/userMapper.xml" />
<mapper class="com.bird.mybatis.bean.UserMapper"/>
</mappers>
下面就是使用这个类了
[java] view
plain copy
print?
@Test
public void testAdd2() {
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
mapper.add(new Users(-1,"娃娃",99));
openSession.commit();
openSession.close();
}
相关文章推荐
- C++开发者都应该使用的10个C++11特性
- WheelView 实现 日期和时间同时选择
- js方法不识别参数问题
- localStorage的高级操作写法
- 使用Spring JDBCTemplate简化JDBC操作
- apache搭载多个tomcat
- 【Eclipse】部署新项目的几个出错问题与解决方案
- 【转】使用graphviz dot来画图表
- Hibernate中get和load的区别
- 【转】使用graphviz dot来画图表
- 2016 Multi-University Training Contest 1 1001 hdu 5723 DFS+并查集最小生树
- linux raw socket
- 关于 Android 进程保活,你所需要知道的一切
- jquery中filter与grep的区别【笔记】
- 32.怎样在Swift中实现TabBar和导航视图结合的项目?
- MySQL 5.7 免安装版 access denied 解决办法
- django(四)
- 1092 - Lighted Panels
- 在线程序员 计算器 (中文)
- 关于函数的多态,与继承,以及相关的名词