Mybatis - CRUD演示

简介: 1.根据ID查询用户接口类:


1.根据ID查询用户


接口类:


public interface UserMapper {
    // 根据ID查询用户
    User getUserById(int id);
}


UserMapper.xml:

parameterType为传输的参数数据类型


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.imustctf.dao.UserMapper">
    <select id="getUserById" resultType="top.imustctf.pojo.User" parameterType="int">
        select * from mybatis.user where id = #{id}
    </select>
</mapper>


测试类:


@Test
public void test() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    try {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.getUserById(1);
        System.out.println(user);
        // User{id=1, name='dahe', password='123456'}
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }
}


2.添加用户


接口类:


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


UserMapper.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.imustctf.dao.UserMapper">
    <insert id="addUser" parameterType="top.imustctf.pojo.User">
        insert into mybatis.user (id,name,password) values (#{id},#{name},#{password})
    </insert>
</mapper>


测试类:


public class UserMapperTest {
    @Test
    public void test() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = new User(4, "meimei", "admin");
            int i = userMapper.addUser(user);
            System.out.println(i);  // 1
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }
}


3.更新用户


接口类:


public interface UserMapper {
    // 更新用户
    int updateUser(User user);
}


UserMapper.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.imustctf.dao.UserMapper">
    <update id="updateUser" parameterType="top.imustctf.pojo.User">
        update mybatis.user set name=#{name} , password=#{password} where id = #{id}
    </update>
</mapper>


测试类:


public class UserMapperTest {
    @Test
    public void test() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            User user = new User(5,"titi", "password");
            int i = userMapper.updateUser(user);
            System.out.println(i);  // 1
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }
}


4.根据ID删除用户


接口类:


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


UserMapper.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.imustctf.dao.UserMapper">
    <delete id="delUserById">
        delete from mybatis.user where id = #{id}
    </delete>
</mapper>


测试类:


@Test
public void test() {
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    try {
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        int i = userMapper.delUserById(1);
        System.out.println(i);  // 1
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        sqlSession.close();
    }
}


5.模糊查询


接口类:


public interface UserMapper {
    // 模糊查询
    List<User> getUserLike(String value);
}


UserMapper.xml:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.imustctf.dao.UserMapper">
    <select id="getUserLike" resultType="top.imustctf.pojo.User">
        select * from mybatis.user where name like "%"#{value}"%"
    </select>
</mapper>


测试类:


public class UserMapperTest {
    @Test
    public void test() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        try {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = userMapper.getUserLike("i");
            for (User user : userList) {
                System.out.println(user);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            sqlSession.close();
        }
    }
}
目录
相关文章
|
4月前
MyBatisPlus-标准数据层CRUD功能制作
MyBatisPlus-标准数据层CRUD功能制作
54 0
|
11月前
|
SQL Java 数据库连接
MyBatis快速入门以及环境搭建和CRUD的实现
MyBatis快速入门以及环境搭建和CRUD的实现
49 0
|
4月前
|
SQL Java 数据库连接
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
135 0
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
|
4月前
|
XML Java 数据库连接
MyBatis深入探索:原生API与注解方式实现CRUD操作
MyBatis深入探索:原生API与注解方式实现CRUD操作
111 0
|
4月前
|
Java 关系型数据库 数据库连接
【MyBatisPlus 】MyBatisPlus CRUD 工程使用
【1月更文挑战第19天】【MyBatisPlus 】MyBatisPlus CRUD 工程使用
|
3月前
|
缓存 Java 数据库连接
我们后端程序员不是操作MyBatis的CRUD Boy
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天我们接着新的一篇Java进阶指南。为啥都戏称后端是CRUD Boy?难道就因为天天怼着数据库CRUD吗?要我说,是这个岗位的位置要的就是你CRUD,你不得不CRUD。哪有公司天天能给你搭建高并发、高可用、大数据框架的活呢,一条业务线总要成长吧,慢慢成熟了就要装修工来缝缝补补、美化美化,也就是CRUD的活。不能妄自菲薄CRUD Boy,我们是后端工程师。今天来指南下操作数据库之MyBatis框架。
我们后端程序员不是操作MyBatis的CRUD Boy
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
MybatisPlus-标准CRUD制作,新增boolean save(T t),删除 ~ delete(int id),修改 ~ update(T t),根据id查询,T getById....
|
3月前
|
SQL XML Java
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
31 1
|
4月前
|
SQL Java 关系型数据库
基于SpringBoot使用MyBatisPlus,MyBatisPlus标准数据层开发(CRUD)、MyBatisPlus分页功能的使用
基于SpringBoot使用MyBatisPlus,MyBatisPlus标准数据层开发(CRUD)、MyBatisPlus分页功能的使用
53 2