• 【JAVAEE框架】Mybatis常用操作(CRUD)


     哈喽~大家好呀,这篇来看看使用的常用操作。

     🥇个人主页:个人主页​​​​​               

    🥈 系列专栏:【JAVAEE框架】

    🥉与这篇相关的文章:              

    目录

    一、准备工作

    二、查询所有用户信息

    三、按 xx 进行查询

    四、模糊查询

    五、增删改操作

    六、联合查询

    七、分页查询


    一、准备工作

    如何新建 Mybatis 项目,可以看看上一篇的讲解——【JAVAEE框架】Mybatis项目起步讲解

    准备 tb_brand 表,字段id(id)、品牌名(brand_name)、公司名(company_name)、排序顺序(ordered)、描述信息(description)、状态(status)。

    项目结构在上篇有所讲解,这里就不重复了,直接上代码讲解。

    二、查询所有用户信息

    我们常见的 xx 管理系统最常见的操作之一,查看所有的记录。

    mapper

    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.itxzw.dao.IBrandDao">

    IBrandDao

    1. public interface IBrandDao {
    2. public List getUserList() throws Exception;
    3. }

    接着上次的讲,上次我们讲到SqlSession是dao与db建立的一次会话,就像 servlet 的 session 一样,但,有没有想过,我们以后一个项目有成千上万的访问者访问页面(获取 db 的信息),那么岂不是要建立成千上万次会话?这是很不合理的。所以就有了 MyBatisUtil,就像 jdbc 的 Util 一样

    MyBatisUtil

    1. public class MyBatisUtil {
    2. private static SqlSessionFactory factory;
    3. static {
    4. try {
    5. SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    6. InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
    7. factory = builder.build(is);
    8. } catch (IOException e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. public static SqlSession openSession(){
    13. return factory.openSession();
    14. }
    15. public static SqlSession openSession(boolean autoCommit){
    16. return factory.openSession(autoCommit);
    17. }
    18. }

    实体类(Brand)

    1. public class Brand {
    2. // id 主键
    3. private Integer id;
    4. // 品牌名称
    5. private String brand_name;
    6. // 企业名称
    7. private String company_name;
    8. // 排序字段
    9. private Integer ordered;
    10. // 描述信息
    11. private String description;
    12. // 状态:0:禁用 1:启用
    13. private Integer status;
    14. public Brand() {
    15. }
    16. public Brand(Integer id, String brand_name, String company_name, Integer ordered, String description, Integer status) {
    17. this.id = id;
    18. this.brand_name = brand_name;
    19. this.company_name = company_name;
    20. this.ordered = ordered;
    21. this.description = description;
    22. this.status = status;
    23. }
    24. public Integer getId() {
    25. return id;
    26. }
    27. public void setId(Integer id) {
    28. this.id = id;
    29. }
    30. public String getBrand_name() {
    31. return brand_name;
    32. }
    33. public void setBrand_name(String brand_name) {
    34. this.brand_name = brand_name;
    35. }
    36. public String getCompany_name() {
    37. return company_name;
    38. }
    39. public void setCompany_name(String company_name) {
    40. this.company_name = company_name;
    41. }
    42. public Integer getOrdered() {
    43. return ordered;
    44. }
    45. public void setOrdered(Integer ordered) {
    46. this.ordered = ordered;
    47. }
    48. public String getDescription() {
    49. return description;
    50. }
    51. public void setDescription(String description) {
    52. this.description = description;
    53. }
    54. public Integer getStatus() {
    55. return status;
    56. }
    57. public void setStatus(Integer status) {
    58. this.status = status;
    59. }
    60. @Override
    61. public String toString() {
    62. return "Brand{" +
    63. "id=" + id +
    64. ", brand_name='" + brand_name + '\'' +
    65. ", company_name='" + company_name + '\'' +
    66. ", ordered=" + ordered +
    67. ", description='" + description + '\'' +
    68. ", status=" + status +
    69. '}';
    70. }
    71. }

    测试

    1. public class TestMyBatis {
    2. @Test
    3. public void test01() throws Exception {
    4. IBrandDao userDao = new BrandDao();
    5. List userList = userDao.getUserList();
    6. for (Brand brand : userList) {
    7. System.out.println(brand);
    8. }
    9. }
    10. }

    效果

     三、按 xx 进行查询

    常见的按照姓名查询、按照 id 查询等,这里就演示 id 查询

    mapper

    IBrandDao

    这里就全部给了,这小节就不多写 IBrandDao 了

    1. public interface IBrandDao {
    2. public List getBrandList();
    3. public Brand getBrandById(String id);
    4. public List getBrandListByCondition(Brand conn);
    5. public List getBrandListByCondition(Map conn);
    6. public Integer addBrand(Brand brand);
    7. public Integer updateBrand(Brand brand);
    8. public Integer deleteBrand(Brand brand);
    9. public Integer getBrandByCondition(Brand brand);
    10. }

    测试

    1. @Test
    2. public void test02(){
    3. IBrandDao dao = new BrandDao();
    4. Brand brandList = dao.getBrandById("142");
    5. System.out.println(brandList);
    6. }

    效果

    四、模糊查询

    查询品牌含有8的品牌名

    mapper

    测试

    1. @Test
    2. public void test03(){
    3. SqlSession sqlSession = MyBatisUtil.openSession();
    4. IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
    5. Brand brand = new Brand();
    6. brand.setBrand_name("8");
    7. // brand.setCompany_name("小米");
    8. List brandList = mapper.getBrandListByCondition(brand);
    9. for (int i = 0; i < brandList.size(); i++) {
    10. System.out.println(brandList.get(i));
    11. }
    12. }

    效果

    扩:

    用法

       在使用mybatis 时我们sql是写在xml 映射文件中,如果写的sql中有一些特殊的字符的话,在解析xml文件的时候会被转义,但我们不希望他被转义,所以我们要使用来解决。

     eg:> < 这两个符号

    五、增删改操作

    mapper

    1. "addBrand" parameterType="Brand">
    2. insert into tb_brand values (#{id}, #{brand_name}, #{company_name}, #{ordered}, #{description}, #{status});
    3. ]]>
    4. "updateBrand" parameterType="Brand">
    5. update tb_brand set company_name = #{company_name} where id = #{id}
    6. ]]>
    7. "deleteBrand" parameterType="Brand">
    8. delete from tb_brand where id = #{id}
    9. ]]>

    测试

    1. @Test
    2. public void test04(){
    3. SqlSession sqlSession = MyBatisUtil.openSession();
    4. IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
    5. Brand brand = new Brand(null, "菠萝手机","菠萝",100,"美国有苹果,中国有菠萝",0);
    6. Integer integer = mapper.addBrand(brand);
    7. sqlSession.commit();
    8. System.out.println(integer);
    9. }
    10. @Test
    11. public void test05(){
    12. SqlSession sqlSession = MyBatisUtil.openSession();
    13. IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
    14. Brand brand = new Brand();
    15. brand.setId(191);
    16. brand.setCompany_name("大菠萝手机");
    17. Integer integer = mapper.updateBrand(brand);
    18. sqlSession.commit();
    19. System.out.println(integer);
    20. }
    21. @Test
    22. public void test06(){
    23. SqlSession sqlSession = MyBatisUtil.openSession();
    24. IBrandDao mapper = sqlSession.getMapper(IBrandDao.class);
    25. Brand brand = new Brand();
    26. brand.setId(195);
    27. Integer integer = mapper.deleteBrand(brand);
    28. sqlSession.commit();
    29. System.out.println(integer);
    30. }

    效果

     

     

     六、联合查询

    mapper

    1. "smbmsUser" type="smbmsUser">
    2. "id" column="id">
    3. "role" column="userrole" resultMap="smbmsRole">
    4. "addresses" column="id" ofType="smbmsAddress" resultMap="smbmsAddress">
    5. "smbmsRole" type="smbmsRole">
    6. "id" column="rid">
    7. "createdby" column="rcb">
    8. "creationdate" column="rcd">
    9. "modifyby" column="rmb">
    10. "modifydate" column="rmd">
    11. "smbmsAddress" type="smbmsAddress">
    12. "id" column="aid">
    13. "createdby" column="acb">
    14. "creationdate" column="acd">
    15. "modifyby" column="amb">
    16. "modifydate" column="amd">

    IUserDao

        public List getUserListByCondition(UserCondition conn);

    测试

    1. @Test
    2. public void test01(){
    3. SqlSession sqlSession = MyBatisUtil.openSession();
    4. IUserDao mapper = sqlSession.getMapper(IUserDao.class);
    5. UserCondition conn = new UserCondition();
    6. List userList = mapper.getUserListByCondition(conn);
    7. for (SmbmsUser smbmsUser : userList) {
    8. System.out.println(smbmsUser);
    9. }
    10. sqlSession.close();
    11. }

    效果

    七、分页查询

    mapper

     IUserDao

    public List selectByPage(@Param("begin") int begin, @Param("size") int size);

    测试

    1. @Test
    2. public void test01() {
    3. SqlSession sqlSession = MyBatisUtil.openSession();
    4. IUserDao mapper = sqlSession.getMapper(IUserDao.class);
    5. List pageBean = mapper.selectByPage(1, 10);
    6. for (Brand brand : pageBean) {
    7. System.out.println(brand);
    8. }
    9. sqlSession.close();
    10. }

    效果

    不积跬步无以至千里,趁年轻,使劲拼,给未来的自己一个交代!向着明天更好的自己前进吧!

    ​​

  • 相关阅读:
    猫毛过敏不能养猫了吗?除猫毛好的宠物空气净化器品牌有哪些?
    C语言time()函数的用法
    软件外包开发代码质量评测
    ELK 企业级日志分析系统
    CryptoJS 加解密类库的简单用法
    【Redis系列】Linux查看Redis版本信息
    安卓抓jdwskey
    物联网感知-光纤光栅传感器技术
    NAO机器人程序设计——接力赛准备
    golang从0到1实战系统四十:处理表单的输入
  • 原文地址:https://blog.csdn.net/aasd23/article/details/126777170