MyBatis基本增删改查操作

jopen 4年前

       本文内容主要介绍单条记录的增删改查操作,MyBatis提供了很多完成单条记录的增删改查操作的API。本例主要讲 述<UserMapper> UserMapper org.apache.ibatis.session.SqlSession.getMapper(Class<UserMapper> clazz)的使用。使用此API,我们需要创建UserMapper操作接口,函数名和MyBatis的User.xml配置文件中的操作id名对应。

    public interface UserMapper {                    /*======基本增删改查操作======*/            /**            * 删除操作            * @param userName            */            public void deleteUser(String userName);                        /**            * 更新操作            * @param user            */            public void updateUser(User user);                        /**            * 添加操作            * @param user            */            public void insertUser(User user);                           /**            * 查询操作            * @param id            * @return            */            public User selectUser(int id);         }  

  MyBatis具体增删改查的操作实现过程。
Session session = ssf.openSession();            try {                UserMapper userMapper = session.getMapper(UserMapper.class);                User newUser = new User("MyBatisUser", "123456");                userMapper.insertUser(newUser);                // 这里一定要提交,不然数据进不去数据库中                session.commit();                System.out.println(newUser);            } catch (Exception e) {                e.printStackTrace();            } finally {                MyBatisUtil.closeSession(session);            }        }            /**        * 删除操作        */        public static void deleteUser(String userName) {            SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();            SqlSession session = ssf.openSession();            try {                UserMapper userMapper = session.getMapper(UserMapper.class);                userMapper.deleteUser(userName);                //提交Session,更新数据库                session.commit();            } catch (Exception e) {                e.printStackTrace();            } finally {                MyBatisUtil.closeSession(session);            }        }            /**        * 删除操作        */        public static void updateUser(User user) {            SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();            SqlSession session = ssf.openSession();            try {                UserMapper userMapper = session.getMapper(UserMapper.class);                            userMapper.updateUser(user);                // 这里一定要提交,不然数据进不去数据库中                session.commit();            } catch (Exception e) {                e.printStackTrace();            } finally {                MyBatisUtil.closeSession(session);            }        }            /**        * 查询操作        */        public static User selectUser(int id) {            SqlSessionFactory ssf = MyBatisUtil.getSqlSessionFactory();            SqlSession session = ssf.openSession();            try {                UserMapper userMapper = session.getMapper(UserMapper.class);                User user = userMapper.selectUser(id);                return user;            } catch (Exception e) {                e.printStackTrace();                return null;            } finally {                MyBatisUtil.closeSession(session);            }        }  
来自:http://blog.csdn.net/mahoking