Mybatis实现增删改查

承接上一篇《我的第一个mybatis程序》,继续增加增删改查部分的内容。

在mybatis环境搭建完成之后,原来的需要的jdbc工作也差不多算是被mybatis框架替代了,我们只需要编写少量的代码就可以实现对数据库的操作。

环境搭建完成之后,只需要以下三个步骤可以实现增删改查了:

  • UserMapper.java中编写方法的接口

  • UserMapper.xml中编写sql语句

  • Mytest.java中实现测试类

具体如下:

  • UserMapper.java中编写方法的接口

<?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">
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.wong.dao.UserMapper"><!--select查询语句-->
    <select id="selectUser" resultType="com.wong.pojo.User">
    select * from user
  </select><select id="getUserById" parameterType="int" resultType="com.wong.pojo.User">
        select * from mybatis.user where id= #{id}
    </select><!--对象中的属性可以直接取出来-->
    <insert id="addUser" parameterType="com.wong.pojo.User">
        insert into mybatis.user(id,name,pwd) values (#{id},#{name},#{pwd})
    </insert><!--update-->
    <update id="updateUser" parameterType="com.wong.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id}
    </update><!--delete-->
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id}
    </delete></mapper>
  • UserMapper.xml中编写sql语句

package com.wong.dao;
​
import com.wong.pojo.User;
​
import java.util.List;
​
public interface UserMapper {
    //获取全部查询
    List<User> selectUser();
​
    //根据id查询用户
    User getUserById(int id);
​
    //insert 一个用户
    int addUser(User user);
​
    //修改信息
    int updateUser(User user);
​
    //删除用户
    int deleteUser(int id);
​
}
  • Mytest.java中实现测试类

package com.wong.dao;
​
import com.wong.pojo.User;
import com.wong.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
​
import java.util.List;
​
public class MyTest {
    @Test
    public void selectUser() {
        SqlSession session = MybatisUtils.getSession();
        //方法一:
        //List<User> users = session.selectList("com.wong.mapper.UserMapper.selectUser");
        //方法二:
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> users = mapper.selectUser();
​
        for (User user: users){
            System.out.println(user);
        }
        session.close();
    }
​
    @Test
    public void getUserId(){
        SqlSession session = MybatisUtils.getSession();
​
        UserMapper mapper = session.getMapper(UserMapper.class);
        User user = mapper.getUserById(1);
        System.out.println(user);
​
        session.close();
    }

   //增删改需要提交事务
    @Test
    public void addUser(){      //
        SqlSession session = MybatisUtils.getSession();
​
        UserMapper mapper = session.getMapper(UserMapper.class);
        mapper.addUser(new User(4,"michael","99856"));
​
        session.commit();       //提交事务
        session.close();
    }
​
    @Test
    public void updateUser(){   //
        SqlSession session = MybatisUtils.getSession();
​
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.updateUser(new User(4, "Kangkang", "123456789"));
        if (i>0){
            System.out.println("更新成功!");
        }
​
        session.commit();       //提交事务
        session.close();
    }
​
    @Test
    public void deleteUser(){       //
        SqlSession session = MybatisUtils.getSession();
​
        UserMapper mapper = session.getMapper(UserMapper.class);
        int i = mapper.deleteUser(4);
        if (i>0){
            System.out.println("删除成功!");
        }
        session.commit();       //提交事务
        session.close();
    }
}
 

 

 
posted @ 2020-07-19 13:51  HaydenWong211  阅读(242)  评论(0编辑  收藏  举报