Mybatis的增删改查操作

CRUD

注意点:增删改需要提交事务

1、nameSpace

nameSpace中的包名要和Dao/Mapper接口的包名一致!

2、select

选择、查询语句;

  • id:就是对应的nameSpace中的方法名

  • resultType:SQL语句执行的返回值!

  • parameterType:参数类型!

  1. 编写接口

    //根据id查询用户
    User getUserById(int id);
    
  2. 编写对应的mapper中的sql语句

    <select id="getUserById" parameterType="int" resultType="com.hua.pojo.User">
        select * from mybatis.user where id = #{id}
    select>
    
  3. 测试

    @Test
    public void getUserByIdTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        User userById = mapper.getUserById(1);
        System.out.println(userById);
        sqlSession.close();
    }
    

3、Insert

  1. 编写接口

    //插入一个用户
    int addUser(User user);
    
  2. 编写对应的mapper中的sql语句

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

    @Test
    public void addUserTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int user = mapper.addUser(new User(4, "鱼鱼", "4444"));
        System.out.println(user);
        if(user>0){
            System.out.println("插入成功!");
        }
        //提交事务
        sqlSession.commit();
        sqlSession.close();
    }
    

4、Update

  1. 编写接口

    //修改用户
    int updateUser(User user);
    
  2. 编写对应的mapper中的sql语句

    <update id="updateUser" parameterType="com.hua.pojo.User">
        update mybatis.user set name = #{name},pwd=#{pwd} where id=#{id};
    update>
    
  3. 测试

    @Test
    public void updateUserTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int result = mapper.updateUser(new User(1, "花花", "6666"));
        if(result>0){
            System.out.println("修改成功");
        }
        sqlSession.commit();
    }
    

5、Delete

  1. 编写接口

    //删除一个用户
    int deleteUser(int id);
    
  2. 编写对应的mapper中的sql语句

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

    @Test
    public void deleteUserTest(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        int i = mapper.deleteUser(4);
        if(i>0){
            System.out.println("删除成功");
        }
        sqlSession.commit();
        sqlSession.close();
    }
    

6、分析错误

  • 标签不要匹配错
  • resource绑定mapper,需要使用路径!
  • 程序配置文件必须符合规范
  • NullPointerException,没有注册到资源!
  • 输出的xml文件中存在中文乱码问题!
  • maven资源没有导出问题!

7、万能的Map

假设,我们的实体类,或者数据库中的表,字段或者参数过多,我们应当考虑使用Map!

测试一,添加用户

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

<insert id="addUser2" parameterType="map">
    insert into mybatis.user(id,name,pwd) values (#{userid},#{username},#{password});
insert>
@Test
public void addUserTes2(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao mapper = sqlSession.getMapper(UserDao.class);
    Map<String,Object>map=new HashMap<String,Object>();
    map.put("userid",4);
    map.put("username","鱼鱼");
    map.put("password","4444");
    mapper.addUser2(map);

    //提交事务
    sqlSession.commit();
    sqlSession.close();
}

测试二,查询用户

User getUserById2(Map<String,Object> map);
<select id="getUserById2" parameterType="map" resultType="com.hua.pojo.User">
    select * from mybatis.user where id = #{id} and name=#{name}
select>
@Test
public void getUserByIdTest2(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao mapper = sqlSession.getMapper(UserDao.class);
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("id",1);
    map.put("name","花花");
    User userById2 = mapper.getUserById2(map);
    System.out.println(userById2);
    sqlSession.close();
}

Map传递参数,直接在sql中取出key即可! 【parameterType=“map”】

对象传递参数,直接在sql中取对象的属性即可! 【parameterType=“Object”】

只有一个基本类型参数的情况下,可以直接在sql中取到!

多个参数用Map,或者注解!

8、模糊查询

模糊查询怎么写?

  1. Java代码执行的时候,传递通配符% %

    List<User> userList = mapper.getUserLike("&小&");
    
  2. 在sql拼接中使用通配符!

    select * from user where name like "%"#{value}"%"
    

编写接口

List<User> getUserLike(String value);

编写对应的mapper中的sql语句

<select id="getUserLike" resultType="com.hua.pojo.User">
    select * from user where name like "%"#{value}"%"
select>

测试

@Test
public void getUserLike(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    UserDao mapper = sqlSession.getMapper(UserDao.class);
    List<User> userList = mapper.getUserLike("小");
    for (User user : userList) {
        System.out.println(user);
    }
    sqlSession.close();
}

你可能感兴趣的:(Mybatis,mybatis,java,mysql)