MyBatis学习笔记(三)数据的增删改查 CRUD操作

MyBatis学习笔记(三)数据的增删改查 CRUD操作
我们接着上篇的环境,继续讲如何对表执行CRUD操作

1.基于XML实现CRUD

定义sql映射xml文件,UsersMapper.xml如下:




<mapper namespace="com.mapping.UsersMapper">
    
    <select id="getUser" parameterType="int" resultType="Users">
        select * from users where id=#{id}
    select>

    
    <insert id="addUser" parameterType="Users">
        insert into users(name, age, address) values(#{name}, #{age}, #{address})
    insert>

    
    <delete id="deleteUser" parameterType="int">
        delete from users where id=#{id}
    delete>

    
    <select id="updateUser" parameterType="Users">
        update users set name=#{name}, age=#{age}, address=#{address} where id=#{id};
    select>

    
    <select id="getAllUsers" resultType="Users">
        select * from users
    select>
mapper>

测试用例:

package com.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.model.Users;
import com.util.MyBatisUtil;

/**
 *
 * @author wenjuanhenxing
 * @version Test.java 2015年12月4日 上午10:34:26
 */
public class Test {

    public static void testGetUser() {
        // 使用工具类创建session
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        /**
         * 映射sql的标识字符串,
         * com.mapping.UsersMapper是UsersMapper.xml文件中mapper标签的namespace属性的值,
         * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
         */
        String stmt = "com.mapping.UsersMapper.getUser";
        Users user = sqlSession.selectOne(stmt, 1);
        // 执行完关闭session
        MyBatisUtil.closeSession(sqlSession);
        System.out.println(user);
    }

    public static void testAddUser() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        String stmt = "com.mapping.UsersMapper.addUser";
        Users user = new Users();
        user.setName("你好");
        user.setAge(18);
        user.setAddress("中国");
        int retResult = sqlSession.insert(stmt, user);
        // 手动提交事务
        // sqlSession.commit();
        // 使用SqlSession执行完SQL之后需要关闭SqlSession
        MyBatisUtil.closeSession(sqlSession);
        System.out.println(retResult);
    }

    public static void testDeleteUser() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        String stmt = "com.mapping.UsersMapper.deleteUser";
        int result = sqlSession.delete(stmt, 2);
        MyBatisUtil.closeSession(sqlSession);
        System.out.println(result);
    }

    public static void testUpdateUser() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        String stmt = "com.mapping.UsersMapper.updateUser";
        Users user = new Users();
        user.setId(3);
        user.setName("赵六");
        user.setAge(50);
        int result = sqlSession.update(stmt, user);
        MyBatisUtil.closeSession(sqlSession);
        System.out.println(result);
    }

    public static void testGetAllUsers() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        String stmt = "com.mapping.UsersMapper.getAllUsers";
        // 执行查询操作,将查询结果自动封装成List返回
        List list = sqlSession.selectList(stmt);
        MyBatisUtil.closeSession(sqlSession);
        System.out.println(list);
    }

    public static void main(String[] args) {

        testGetAllUsers();
        testGetUser();
        testAddUser();
        testDeleteUser();
        testUpdateUser();
        testGetAllUsers();
    }
}

你可能感兴趣的:(MyBatis)