Mybatis之使用注解开发CRUD

来源:互联网 发布:js正则表达式函数 编辑:程序博客网 时间:2024/06/05 18:50

Mybatis之使用注解开发CRUD

1、创建接口

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();  }

2、在配置文件中注册

<mappers>      <mapper resource="com/bird/mybatis/bean/userMapper.xml" />      <mapper class="com.bird.mybatis.bean.UserMapper"/>  </mappers> 

3、使用

    @Test      public void testAdd2() {          SqlSession openSession = factory.openSession();          UserMapper mapper = openSession.getMapper(UserMapper.class);          mapper.add(new Users(-1,"娃娃",99));          openSession.commit();          openSession.close();      } 
转载地址:http://blog.csdn.net/a352193394/article/details/39940259

原创粉丝点击