【Mybatis学习笔记2】实现添加、修改、删除、条件操作、万能map等接口

基于上一篇:https://www.cnblogs.com/yunren/p/14253813.html

本篇内容主要是实现insert、update、delete、带参数传递,以及万能map的几个接口。

我们只需要修改dao下的UserMapper接口,以及对应的UserMapper.xml配置文件里的相关内容:

 

1、添加一个用户

package com.coke.dao;

import com.coke.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserMapper {
// 添加一个用户
    int addUser(User user);

}
<?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.coke.dao.UserMapper">
    <!--
        id 对应namespace中的方法名
        resultType sql执行的返回值
    -->
    <insert id="addUser" parameterType="com.coke.pojo.User">
        insert into mybatis.user (id,name,pwd) values (#{id},#{name },#{pwd})
    </insert>

</mapper>
package com.coke.dao;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
测试类
增、删、改,必须提交事务,也就是sqlSession.commit();
*/
public class UserDaoTest {
@Test public void addUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(4,"哈哈","111111")); // 提交事务 sqlSession.commit(); sqlSession.close(); } }

 

2.修改一个用户信息(根据id字段修改)

因为sql语句是:update mybatis.user set name=%,pwd=% where id=%;,所以我们还是需要User的三个字段,故参数是User对象

package com.coke.dao;

import com.coke.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserMapper {

    // 更新用户
    int updateUser(User user);

}
<?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.coke.dao.UserMapper">
    <!--
        id 对应namespace中的方法名
        resultType sql执行的返回值
    -->
    <update id="updateUser" parameterType="com.coke.pojo.User">
        update mybatis.user set name=#{name},pwd=#{pwd}  where id=#{id};
    </update>
</mapper>
package com.coke.dao;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoTest {

    @Test
    public void updateUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.updateUser(new User(4,"who","444444"));
        sqlSession.commit();
        sqlSession.close();
    }

}

 

3.删除一个用户(根据id)

package com.coke.dao;

import com.coke.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserMapper {
// 删除用户
    int deleteUser(int id);

}
<?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.coke.dao.UserMapper">
    <!--
        id 对应namespace中的方法名
        resultType sql执行的返回值
    -->

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

</mapper>
package com.coke.dao;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoTest {

    @Test
    public void deleteUser(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        mapper.deleteUser(4);
        sqlSession.commit();
        sqlSession.close();

    }
}

 

4.万能map(实现添加用户)

package com.coke.dao;

import com.coke.pojo.User;

import java.util.List;
import java.util.Map;

public interface UserMapper {

    // 万能map
    int addUser2(Map<String,Object> map);

}
<?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.coke.dao.UserMapper">
    <!--
        id 对应namespace中的方法名
        resultType sql执行的返回值
    -->

    <insert id="addUser2" parameterType="map">
        insert into mybatis.user (id,name,pwd) values (#{userId},#{userName},#{passWord});
    </insert>
</mapper>
package com.coke.dao;

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDaoTest {

    @Test
    public void addUser2(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("userId",5);
        map.put("userName","houzi");
        map.put("passWord","000000");

        mapper.addUser2(map);

        sqlSession.commit();
        sqlSession.close();

    }
}

 

总结:

 1.UserMapper.xml

  • namespace,对应dao层mapper名称;
  • id,对应namespace里的方法名;
  • resultType,对应返回类型,只在select中用到;
  • parameterType,参数类型,有一个where条件的,就对应该字段的类型;多个字段时用User。

 

2.UserMapper.java与UserMapper.xml对应关系

 

posted @ 2021-01-09 11:37  愚人李愚  阅读(310)  评论(0编辑  收藏  举报