从入门到精通的使用Mybatis框架(二)——基础的CRUD操作

Mapper接口类

package com.PAG.mapper;

import com.PAG.pojo.User;

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

public interface userMapper {
    //查询所有用户信息
    List<User> selectUser();
    //通过id查询用户信息
    User selectUserById(int id);
    //通过用户名和密码查询用户信息
    User selectUserByMap(Map<String,Object> map);
    //增加用户
    int addUser(User user);
    //修改用户信息
    int updateUser(User user);
    //删除用户信息
    int deleteUser(int id);
}

select

查询所有用户

    <select id="selectUser" resultType="com.PAG.pojo.User">
        select * from user
    </select>

测试类

   @Test
    public void selectUser(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        List<User> users = mapper.selectUser();
        for (User user:users) {
            System.out.println(user);
        }
        session.close();
    }

通过id查询用户

    <select id="selectUserById" resultType="com.PAG.pojo.User">
        select * from user where id = #{id}
    </select>

测试类

    @Test
    public void selectUserById(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        User user = mapper.selectUserById(1);
        System.out.println(user);
        session.close();
    }

通过名字和密码查询用户(Map)

    <select id="selectUserByMap" parameterType="map" resultType="com.PAG.pojo.User">
        select * from user where name = #{name} and pwd = #{pwd}
    </select>

测试类

    @Test
    public void selectUserByMap(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        Map<String, Object> map = new HashMap<String,Object>();
        map.put("name","pag");
        map.put("pwd","123456");
        User user = mapper.selectUserByMap(map);
        System.out.println(user);
        session.close();
    }

insert

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

测试类

    @Test
    public void addUser(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        User user = new User(3,"p","1212");
        int i = mapper.addUser(user);
        System.out.println(i);
        session.commit();
        session.close();
    }

update

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

测试类

  @Test
    public void updateUser(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        User user = mapper.selectUserById(3);
        user.setPwd("2333");
        int i = mapper.updateUser(user);
        System.out.println(i);
        session.commit();
        session.close();
    }

delete

    <delete id="deleteUser" parameterType="com.PAG.pojo.User">
        delete from user where id = #{id}
    </delete>

测试类

    @Test
    public void deleteUser(){
        SqlSession session = MybatisUtils.getSession();
        userMapper mapper = session.getMapper(userMapper.class);
        int i = mapper.deleteUser(3);
        System.out.println(i);
        session.commit();
        session.close();
    }
}
上一篇:一个工具类搞定 CRUD 的创建人、修改人、时间等字段赋值!


下一篇:MySQL笔记Day1