02_mybatis的CRUD操作

1、XML 的实现

1.1 定义SQL映射xml文件

  1. <insert id="insertUser" parameterType="cn.imentors.mybatis.test1.User">
  2. insert into users(name, age) values(#{name}, #{age});
  3. </insert>
  4. <delete id="deleteUser" parameterType="int">
  5. delete from users where id=#{id}
  6. </delete>
  7. <update id="updateUser" parameterType="cn.imentors.mybatis.test1.User">
  8. update users set name=#{name},age=#{age} where id=#{id}
  9. </update>
  • parameterType="cn.imentors.mybatis.test1.User",这个参数类型是一个对象,所以占位符必须是对象的属性#{name}

1.2 在config.xml 中注册这个映射文件

  1. <mappers>
  2. <mapper resource="cn/imentors/mybatis/test1/UserMapper.xml"/>
  3. </mappers>
  4. </configuration>

1.3 MyBatisUtils

为了后面测试的方便,将conf.xml文件的加载,写成一个工具类
  1. package cn.imentors.mybatis.util;
  2. public class MybatisUtils {
  3. public static SqlSessionFactory getFactory() {
  4. String resource = "conf.xml";
  5. InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
  6. SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
  7. return factory;
  8. }
  9. }
1.4 测试CRUD操作的XML实现
  • add
  1. @Test
  2. public void testAdd() {
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. //默认是手动提交的
  5. SqlSession session = factory.openSession();
  6. String statement = "cn.imentors.mybatis.test2.userMapper.insertUser";
  7. int insert = session.insert(statement , new User(3, "KK", 23));
  8. //提交
  9. session.commit();
  10. session.close();
  11. System.out.println(insert);
  12. }
  • update
  1. @Test
  2. public void testUpate() {
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. //默认是手动提交的
  5. SqlSession session = factory.openSession(true);
  6. String statement = "cn.imentors.mybatis.test2.userMapper.updateUser";
  7. int update = session.update(statement, new User(3, "KK444", 25));
  8. session.close();
  9. System.out.println(update);
  10. }
  • delete
  1. @Test
  2. public void testDelete() {
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. // 默认是手动提交的
  5. SqlSession session = factory.openSession(true);
  6. String statement = "cn.imentors.mybatis.test2.userMapper.deleteUser";
  7. int delete = session.delete(statement, 3);
  8. session.close();
  9. System.out.println(delete);
  10. }

2、注解的实现

2.1 定义SQL映射的接口

  1. public interface UserMapper {
  2. @Insert("insert into users(name, age) values(#{name}, #{age})")
  3. public int insertUser(User user);
  4. @Delete("delete from users where id=#{id}")
  5. public int deleteUserById(int id);
  6. @Update("update users set name=#{name},age=#{age} where id=#{id}")
  7. public int updateUser(User user);
  8. @Select("select * from users where id=#{id}")
  9. public User getUserById(int id);
  10. @Select("select * from users")
  11. public List<User> getAllUser();
  12. }

2.2 在conf.xml中注册这个映射接口

  1. <mapper class="cn.imentors.mybatis.test3.UserMapper"/>

2.3 测试

  1. @Test
  2. public void testAdd() {
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. //默认是手动提交的
  5. SqlSession session = factory.openSession(true);
  6. //加载UserMapper接口
  7. UserMapper mapper = session.getMapper(UserMapper.class);
  8. int add = mapper.add(new User(4, "SS", 45));
  9. System.out.println(add);
  10. session.close();
  11. }
捐赠我们
    良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
    
posted @ 2015-09-15 09:34  师享空间  阅读(239)  评论(0编辑  收藏  举报