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();
        }
    }
}
目录
相关文章
|
6月前
|
SQL Java 数据库连接
|
6月前
MyBatisPlus-标准数据层CRUD功能制作
MyBatisPlus-标准数据层CRUD功能制作
68 0
|
6月前
|
SQL Java 数据库连接
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
157 0
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
|
6月前
|
XML Java 数据库连接
MyBatis深入探索:原生API与注解方式实现CRUD操作
MyBatis深入探索:原生API与注解方式实现CRUD操作
121 0
|
6月前
|
Java 关系型数据库 数据库连接
【MyBatisPlus 】MyBatisPlus CRUD 工程使用
【1月更文挑战第19天】【MyBatisPlus 】MyBatisPlus CRUD 工程使用
|
19天前
|
SQL Java 数据库连接
MyBatis-Plus:简化 CRUD 操作的艺术
MyBatis-Plus 是一个基于 MyBatis 的增强工具,它旨在简化 MyBatis 的使用,提高开发效率。
43 1
MyBatis-Plus:简化 CRUD 操作的艺术
|
1月前
|
SQL Java 数据库连接
Mybatis方式完成CRUD操作
Mybatis方式完成CRUD操作
37 0
|
5月前
|
缓存 Java 数据库连接
我们后端程序员不是操作MyBatis的CRUD Boy
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天我们接着新的一篇Java进阶指南。为啥都戏称后端是CRUD Boy?难道就因为天天怼着数据库CRUD吗?要我说,是这个岗位的位置要的就是你CRUD,你不得不CRUD。哪有公司天天能给你搭建高并发、高可用、大数据框架的活呢,一条业务线总要成长吧,慢慢成熟了就要装修工来缝缝补补、美化美化,也就是CRUD的活。不能妄自菲薄CRUD Boy,我们是后端工程师。今天来指南下操作数据库之MyBatis框架。
122 3
我们后端程序员不是操作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....
|
5月前
|
SQL XML Java
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)
45 1