MyBatis学習ノート(三)データの添削操作CRUDを調べる

7897 ワード

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