1.select

<select id="selectUserFromId" parameterType="int" resultType="com.zakisoft.ibt.UserVo">  
        select * from user where id = #{id}  
</select>  
<select id="selectAllUsers" resultType="com.zakisoft.ibt.UserVo">  
        select * from user  
</select>
public List<?> selectUserFromId(int id) {  
        SqlSession session = sqlSessionFactory.openSession();  
        List<?> users = null;  
        try {  
            UserOp mapper = session.getMapper(UserOp.class);  
            users = mapper.selectUserFromId(id);  
            if (0 < users.size()) {  
                UserVo user = (UserVo) users.get(0);  
                System.out.println("Get User Name: " + user.getName());  
            }  
        } finally {  
            session.close();  
        }  
        return users;  
    }  
    public List<?> selectAllUsers() {  
        SqlSession session = sqlSessionFactory.openSession();  
        try {  
            UserOp mapper = session.getMapper(UserOp.class);  
            List<?> users = mapper.selectAllUsers();  
            if (0 < users.size()) {  
                UserVo user;  
                for (int i = 0; i < users.size(); i++) {  
                    user = (UserVo) users.get(i);  
                    System.out.println("User Id:" + user.getId() + " --- " + "User Name:" + user.getName());  
                }  
            }  
        } finally {  
            session.close();  
        }  
        return null;  
    } 

2.insert

<insert id="insertNewOneUser" keyProperty="id" parameterType="com.zakisoft.ibt.UserVo" useGeneratedKeys="true">  
        insert into user (name, password) values (#{name},#{password})  
</insert>
public int insertNewOneUser(UserVo user) {  
        user = new UserVo();  
        user.setName("username: " + new Random().nextInt());  
        user.setPassword("passowrd: " + new Random().nextInt());  
        SqlSession session = sqlSessionFactory.openSession();  
        int count = 0;  
        try {  
            UserOp mapper = session.getMapper(UserOp.class);  
            count = mapper.insertNewOneUser(user);  
            System.out.println("count: " + count);  
            // 请注意一定需要提交。  
            session.commit();  
        } finally {  
            session.close();  
        }  
        return count;  
    } 

3.update

<update id="updateUser" parameterType="com.zakisoft.ibt.UserVo">  
        update user set name = #{name}, password = #{password} where id = #{id}  
</update> 
public int updateUser(UserVo user){  
        SqlSession session = sqlSessionFactory.openSession();  
        int count = 0;  
        try {  
            UserOp mapper = session.getMapper(UserOp.class);  
            count = mapper.updateUser(user);  
            System.out.println("count: " + count);  
            // 请注意一定需要提交。  
            session.commit();  
        } finally {  
            session.close();  
        }  
        return count;  
    }  

4.delete

<delete id="deleteUserById" parameterType="int">  
        delete from user where id = #{id}  
</delete> 
public int deleteUserById(int id){  
        SqlSession session = sqlSessionFactory.openSession();  
        int count = 0;  
        try {  
            UserOp mapper = session.getMapper(UserOp.class);  
            count = mapper.deleteUserById(id);  
            System.out.println("count: " + count);  
            // 请注意一定需要提交。  
            session.commit();  
        } finally {  
            session.close();  
        }  
        return count;  
    }