mybatis增删改查

1:namespace

namespace中的包名要和Dao/mapper接口的包名一致

2:select

id:方法名

代码

UserMapper接口

public interface UserMapper {
    // 查询全部用户
    List<User> getUserList();

    // 根据id查询用户
    User getUserById(int id);

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

    // 修改用户
    int updateUser(User user);

    // 删除一个用户
    int deleteUser(int id);


}

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.mine.dao.UserMapper">

    <select id="getUserList" resultType="com.mine.pojo.User">
    select * from mybatis.user
  </select>

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


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

    <update id="updateUser" parameterType="com.mine.pojo.User">
       update mybatis.user set name = #{name},pwd = #{pwd}  where id = #{id};
    </update>

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


</mapper>

 

UserDaoTest

public class UserDaoTest {
    @Test
    public void test(){
        // 第一步,获得sqlSession对象,类似于JDBC中的Connection
        SqlSession sqlSession = MybatisUtils.getSqlSession();

        // 执行sql,面向接口编程
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();

        for (User user : userList) {
            System.out.println(user);
        }
        // 关闭sqlSession
        sqlSession.close();
        System.out.println("--------------");
        System.out.println(UserMapper.class);


    }

    @Test
    public void getUserById(){
        // 获取连接
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 实例化对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User userById = mapper.getUserById(1);
        System.out.println(userById);
        sqlSession.close();
    }

    @Test
    public void addUser() {
        // 获取连接
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 实例化对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        int res = mapper.addUser(new User(4, "张培", "123456"));
        if (res > 0) {
            System.out.println("执行成功");
        }
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateUser() {
        // 获取连接
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 实例化对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.updateUser(new User(4,"张配","123123"));
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void deleteUser() {
        // 获取连接
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        // 实例化对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.deleteUser(4);
        // 提交事务
        sqlSession.commit();
        sqlSession.close();
    }



}

 

注意点:

  1. 增删改必须要提交事务
    sqlSession.commit();
  2. resource的路径必须要是/ 
    <mapper resource="com/mine/dao/UserMapper.xml"/>
  3. namespace的路径必须要是.  
    namespace="com.mine.dao.UserMapper"

 

报错:

mybatis增删改查

 

 

 出现原因及解决:insert的标签写成select,并且加了中文注释

mybatis增删改查

 

 

 

mybatis增删改查

 

mybatis增删改查

上一篇:软件开发常用术语中英文对照表


下一篇:Upload-labs Pass-08