Mybatis實現數據的增刪改查

Mybatis實現數據的增刪改查

大致思路


只需對三個部分的文件代碼進行編寫java

  • 在對應接口添加方法。sql

  • 在綁定的接口的 x m l xml xml文件實現該方法。apache

  • T e s t Test Test類中使用該方法。session


例子1:經過id查詢用戶。

1.在對應接口添加方法。mybatis

public interface UserMapper { 
    //經過id查詢用戶
    User getUserById(int id);
}

2.在綁定的接口的 x m l xml xml文件實現該方法。app

<!--這裏的參數類型是int,由於是經過id查詢-->
<select id="getUserById" parameterType="int" resultType="com.harris.pojo.User">
        select * from mybatis.user where id= #{id}
    </select>

3.在 T e s t Test Test類中使用該方法。ide

@Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(2);
        System.out.println(user);
        sqlSession.close();
    }

例子1:添加用戶。

1.在對應接口添加方法。spa

//添加用戶
    int addUser(User user);

2.在綁定的接口的 x m l xml xml文件實現該方法。code

<insert id="addUser" parameterType="com.harris.pojo.User">
       insert into mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd})
    </insert>

3.在 T e s t Test Test類中使用該方法。xml

@Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int flag = mapper.addUser(new User(3,"harris","123134"));
        if(flag>0){
            System.out.println("插入成功");
        }
        sqlSession.commit();//增刪改須要提交事務
        sqlSession.close();
    }

這裏須要注意的是增刪改都須要進行提交事務即調用 c o m m i t ( ) commit() commit()方法,不然增刪改是無效的。


完整的增刪查改 C R U D CRUD CRUD

1.UserMapper.java

package com.harris.dao;

import com.harris.pojo.User;

import java.util.List;

public interface UserMapper {
    List<User> getUserList();

    //經過id查詢用戶
    User getUserById(int id);
    //添加用戶
    int addUser(User user);
    //修改用戶
    int updUser(User user);
    //刪除用戶
    int delUser(int id);
}

2.UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.harris.dao.UserMapper">
    <select id="getUserList" resultType="com.harris.pojo.User">
        select * from mybatis.user
    </select>
    <select id="getUserById" parameterType="int" resultType="com.harris.pojo.User">
        select * from mybatis.user where id= #{id}
    </select>
    <insert id="addUser" parameterType="com.harris.pojo.User">
       insert into mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd})
    </insert>
    <update id="updUser" parameterType="com.harris.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd}  where id = #{id}
    </update>
    <delete id="delUser" parameterType="com.harris.pojo.User">
        delete from mybatis.user where id=#{id}
    </delete>
</mapper>

3.Test類

package com.harris.dao;

import com.harris.pojo.User;
import com.harris.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import org.junit.Test;

public class UserMapperTest {
    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for(User user:userList){
            System.out.println(user);
        }
        sqlSession.close();
    }
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.getUserById(2);
        System.out.println(user);
        sqlSession.close();
    }
    @Test
    public void addUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int flag = mapper.addUser(new User(3,"harris","123134"));
        if(flag>0){
            System.out.println("插入成功");
        }
        sqlSession.commit();//增刪改須要提交事務
        sqlSession.close();
    }
    @Test
    public void updUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int flag = mapper.updUser(new User(4,"yyy","654321"));
        if(flag>0){
            System.out.println("修改爲功");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void delUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int flag = mapper.delUser(3);
        if(flag>0){
            System.out.println("刪除成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }
}
相關文章
相關標籤/搜索