• MyBatis之增删查改功能


    文章目录

    • 一、创建各种类
    • 二、MyBatis的各种功能
      • 1、查询

        查询User表所有用户的信息

        因为查询的内容返回的数据不止一条,因此我们用List集合作为返回值

        SelectMapeer接口下定义方法名:List getAllUser()

        1. public interface SelectMapper {
        2. /*
        3. 若查询多条,不能用实体类对象为返回值,否则报错
        4. */
        5. //查询所有信息
        6. List getAllUser();
        7. }

        SelectMapper的xml文件下写sql语句

        1. "1.0" encoding="UTF-8" ?>
        2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        4. "com.mybatis.mapper.SelectMapper">

        注意:mapper的namespace必须对应接口名,select的id必须是我们写的方法名,两个必须要一样。查询结果需要返回,因此有resultType为实体类名 

        SelectMapperText类开始测试

        1. import com.mybatis.Util.SqlSessionUtil;
        2. import com.mybatis.mapper.SelectMapper;
        3. import com.mybatis.pojo.User;
        4. import org.apache.ibatis.io.Resources;
        5. import org.apache.ibatis.session.SqlSession;
        6. import org.apache.ibatis.session.SqlSessionFactory;
        7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
        8. import org.junit.Test;
        9. import javax.crypto.spec.PSource;
        10. import java.io.IOException;
        11. import java.io.InputStream;
        12. import java.util.List;
        13. import java.util.Map;
        14. public class SelectMapperText {
        15. @Test
        16. public void getAllUser() throws IOException {
        17. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        18. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        19. SqlSession sqlSession = sessionFactory.openSession(true);
        20. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        21. List allUser = mapper.getAllUser();
        22. allUser.forEach(s-> System.out.println(s));
        23. }
        24. }

         运行getAllUser后为:

        成功查到所有用户信息!!! 


        2、增加功能 

        向User表增加一个用户,用户信息为:

        用户名:sb,密码:123456,年龄:20,性别:男,邮件:123456@qq.com

         SelectMapeer接口下定义方法名:void insertUser(User user);

        1. package com.mybatis.mapper;
        2. import com.mybatis.pojo.User;
        3. import org.apache.ibatis.annotations.Param;
        4. import java.util.List;
        5. import java.util.Map;
        6. public interface SelectMapper {
        7. //查询所有信息
        8. List getAllUser();
        9. //插入用户信息
        10. void insertUser(User user);
        11. }

        SelectMapper的xml文件下写sql语句

        1. "1.0" encoding="UTF-8" ?>
        2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        4. "com.mybatis.mapper.SelectMapper">
        5. "insertUser">
        6. insert into user values (null,#{username},#{password},#{age},#{sex},#{email})

        SelectMapperText类开始测试

        1. import com.mybatis.Util.SqlSessionUtil;
        2. import com.mybatis.mapper.SelectMapper;
        3. import com.mybatis.pojo.User;
        4. import org.apache.ibatis.io.Resources;
        5. import org.apache.ibatis.session.SqlSession;
        6. import org.apache.ibatis.session.SqlSessionFactory;
        7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
        8. import org.junit.Test;
        9. import javax.crypto.spec.PSource;
        10. import java.io.IOException;
        11. import java.io.InputStream;
        12. import java.util.List;
        13. import java.util.Map;
        14. public class SelectMapperText {
        15. @Test
        16. public void getAllUser() throws IOException {
        17. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        18. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        19. SqlSession sqlSession = sessionFactory.openSession(true);
        20. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        21. List allUser = mapper.getAllUser();
        22. allUser.forEach(s-> System.out.println(s));
        23. }
        24. @Test
        25. public void insertUser() throws IOException {
        26. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        27. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        28. SqlSession sqlSession = sessionFactory.openSession(true);
        29. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        30. User user = new User(null,"sb","123456",20,"男","123456@qq.com");
        31. mapper.insertUser(user);
        32. }
        33. }

        成功添加用户信息!!!  


        3、修改功能 

        修改用户名为admin的信息,将性别改为女

        SelectMapeer接口下定义方法名: void updateUser(String username)

        1. package com.mybatis.mapper;
        2. import com.mybatis.pojo.User;
        3. import org.apache.ibatis.annotations.Param;
        4. import java.util.List;
        5. import java.util.Map;
        6. public interface SelectMapper {
        7. //查询所有信息
        8. List getAllUser();
        9. //插入用户信息
        10. void insertUser(User user);
        11. //修改用户信息
        12. void updateUser(@Param("username") String username);//用注释当sql名
        13. }

        SelectMapper的xml文件下写sql语句

        1. "1.0" encoding="UTF-8" ?>
        2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        4. "com.mybatis.mapper.SelectMapper">
        5. "insertUser">
        6. insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
        7. "updateUser">
        8. update user set sex = '女' where username=#{username}

        SelectMapperText类开始测试

        1. import com.mybatis.Util.SqlSessionUtil;
        2. import com.mybatis.mapper.SelectMapper;
        3. import com.mybatis.pojo.User;
        4. import org.apache.ibatis.io.Resources;
        5. import org.apache.ibatis.session.SqlSession;
        6. import org.apache.ibatis.session.SqlSessionFactory;
        7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
        8. import org.junit.Test;
        9. import javax.crypto.spec.PSource;
        10. import java.io.IOException;
        11. import java.io.InputStream;
        12. import java.util.List;
        13. import java.util.Map;
        14. public class SelectMapperText {
        15. @Test
        16. public void getAllUser() throws IOException {
        17. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        18. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        19. SqlSession sqlSession = sessionFactory.openSession(true);
        20. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        21. List allUser = mapper.getAllUser();
        22. allUser.forEach(s-> System.out.println(s));
        23. }
        24. @Test
        25. public void insertUser() throws IOException {
        26. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        27. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        28. SqlSession sqlSession = sessionFactory.openSession(true);
        29. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        30. User user = new User(null,"sb","123456",20,"男","123456@qq.com");
        31. mapper.insertUser(user);
        32. }
        33. @Test
        34. public void updateUser() throws IOException {
        35. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        36. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        37. SqlSession sqlSession = sessionFactory.openSession(true);
        38. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        39. mapper.updateUser("admin");
        40. }
        41. }

        成功修改用户信息!!! 


        4、删除功能 

        删除用户为“root”的所有信息

        SelectMapeer接口下定义方法名:void deleteUser(String username)

        1. package com.mybatis.mapper;
        2. import com.mybatis.pojo.User;
        3. import org.apache.ibatis.annotations.Param;
        4. import java.util.List;
        5. import java.util.Map;
        6. public interface SelectMapper {
        7. //查询所有信息
        8. List getAllUser();
        9. //插入用户信息
        10. void insertUser(User user);
        11. //修改用户信息
        12. void updateUser(@Param("username") String username);//用注释当sql名
        13. //删除用户信息
        14. void deleteUser(@Param("username") String username);
        15. }

        SelectMapper的xml文件下写sql语句

        1. "1.0" encoding="UTF-8" ?>
        2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        4. "com.mybatis.mapper.SelectMapper">
        5. "insertUser">
        6. insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
        7. "updateUser">
        8. update user set sex = '女' where username=#{username}
        9. "deleteUser">
        10. delete from user where username = #{username}

        SelectMapperText类开始测试 

        1. import com.mybatis.Util.SqlSessionUtil;
        2. import com.mybatis.mapper.SelectMapper;
        3. import com.mybatis.pojo.User;
        4. import org.apache.ibatis.io.Resources;
        5. import org.apache.ibatis.session.SqlSession;
        6. import org.apache.ibatis.session.SqlSessionFactory;
        7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
        8. import org.junit.Test;
        9. import javax.crypto.spec.PSource;
        10. import java.io.IOException;
        11. import java.io.InputStream;
        12. import java.util.List;
        13. import java.util.Map;
        14. public class SelectMapperText {
        15. @Test
        16. public void getAllUser() throws IOException {
        17. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        18. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        19. SqlSession sqlSession = sessionFactory.openSession(true);
        20. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        21. List allUser = mapper.getAllUser();
        22. allUser.forEach(s-> System.out.println(s));
        23. }
        24. @Test
        25. public void insertUser() throws IOException {
        26. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        27. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        28. SqlSession sqlSession = sessionFactory.openSession(true);
        29. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        30. User user = new User(null,"sb","123456",20,"男","123456@qq.com");
        31. mapper.insertUser(user);
        32. }
        33. @Test
        34. public void updateUser() throws IOException {
        35. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        36. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        37. SqlSession sqlSession = sessionFactory.openSession(true);
        38. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        39. mapper.updateUser("admin");
        40. }
        41. @Test
        42. public void deleteUser() throws IOException {
        43. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        44. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        45. SqlSession sqlSession = sessionFactory.openSession(true);
        46. SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
        47. mapper.deleteUser("root");
        48. }
        49. }

         成功删除用户信息!!!


        三、总结

                首先创造好接口写入要进行的方法名,然后创建xml文件专门写sql语句,值得注意的是:namespace要和接口名相同,id名要和接口里面的方法名相同,在测试类中:先获取映射文件,然后创建数据会议工厂生成数据会议,最后创建mapper使用接口的函数。

        总之流程为:

                1、创建接口写方法名。

                2、创建xml文件写sql语句。

                3、测试类进行测试

                如果本篇文章对你有用的话,请一键三连支持一下博主,一起共同学习共同进步!!!

  • 相关阅读:
    Draw Your Cards
    Hadoop Hbase
    基于CentOS 7.9离线安装CM6.3.1和CDH6.3.2大数据平台
    SkeyeVSS H5无插件视频云直播点播系统协助药店实现远程监控系统全覆盖
    c++ 常用STL 之set
    互融云农产品追溯系统:区块链存证技术实现双向可追溯
    MySQL My.cnf参数梳理与延伸 (MYSQL 8 INNODB 类)
    SpringMVC(三)获取请求参数
    第一篇-专题说明及当前进度
    Spring Boot(二)
  • 原文地址:https://blog.csdn.net/m0_62476845/article/details/132998876