MyBatis第二天-Mybatis增删改查

1.dao层

  mapper

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

   List<User> getUserById(int id);

   boolean delete(String username);

   int Insert(User user);

   int updata(User user);
}

 

  mapper.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.nly.dao.UserMapper">
<!--    id 绑定方法名-->
    <select id="gstUser" resultType="com.nly.pojo.User">
        select * from mybatis.user;
    </select>

    <select id="getUserById" parameterType="int" resultType="com.nly.pojo.User">
        select * from mybatis.user where id = #{id};
    </select>

    <delete id="delete" parameterType="String" >
        delete from mybatis.user where username = #{username}
    </delete>

    <insert id="Insert" parameterType="com.nly.pojo.User">
       insert into mybatis.user (id,username,password) values (#{id},#{username},#{password})
    </insert>
    
    <update id="updata" parameterType="com.nly.pojo.User">
        update mybatis.user set username=#{username},password =#{password}  where id = #{id};
    </update>
</mapper>

2.Test

注意点增删改需要提交事务,才能对数据库内元素造成影响

sqlSession.commit();

 

package com.nly.dao;

import com.nly.pojo.User;
import com.nly.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class UserDaoTest {
    @Test
    public void Test(){
//        第一步:获取sqlSession
        SqlSession sqlSession = MybatisUtils.getSqlSession();
//        方式一:第二步:执行sql
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.gstUser();
        for (User user : users) {
            System.out.println(user);
        }
//        第三步:关闭sqlSession
        sqlSession.close();
    }
    @Test
    public void getUserById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            List<User> user = mapper.getUserById(2);
            System.out.println(user);
        }catch (Exception e){
            e.printStackTrace();
        }

        sqlSession.close();
    }
    @Test
    public void delete(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            boolean flag = mapper.delete("lizzy");
            //提交事务!!!!!
            sqlSession.commit();
            Test();
            if(flag){
                System.out.println("ok");
            }else{
                System.out.println("error");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        sqlSession.close();
    }

    @Test
    public void updata(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            Test();
            mapper.updata(new User(2,"awei","1313"));
            //提交事务!!!!!
            sqlSession.commit();
            Test();
        }catch (Exception e){
            e.printStackTrace();
        }

        sqlSession.close();
    }

    @Test
    public void Insert(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        try {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            int i = mapper.Insert(new User(4,"add","4646"));
            //提交事务!!!!!
            sqlSession.commit();
            Test();
        }catch (Exception e){
            e.printStackTrace();
        }

        sqlSession.close();
    }
}

 

上一篇:MyBatis总结


下一篇:MyBatis多表操作