• MyBatis


    MyBatis

    概念
    • MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。
    • MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。
    • 官网:https://mybatis.org/mybatis-3/zh/index.html
    持久层
    • 负责将数据保存到数据库的那一层代码
    • JavaEE三层架构:表现层、业务层、持久层
    框架
    • 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码结构
    • 在框架的基础之上构建软件编写更加高效、规范、通用、可扩展

    MyBatis快速入门

    查询user表中的所有数据
    1. 创建user表,添加数据

    2. 创建模块,导入坐标

      
      
          4.0.0
      
          com.zxx
          MyBatis_Study
          1.0-SNAPSHOT
      
          
              8
              8
          
      
          
              
              
                  mysql
                  mysql-connector-java
                  5.1.32
              
      
              
              
                  org.mybatis
                  mybatis
                  3.5.5
              
      
              
              
                  junit
                  junit
                  4.13
                  test
              
      
              
              
                  org.slf4j
                  slf4j-api
                  1.7.36
                  test
              
      
              
              
                  ch.qos.logback
                  logback-classic
                  1.2.3
              
      
              
              
                  ch.qos.logback
                  logback-core
                  1.2.3
              
          
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
    3. 编写MyBatis核心配置文件 --> 替换连接信息,解决硬编码问题

      
      
      
      
          
          
              
          
          
          
          
              
                  
                  
                      
                      
                      
                      
                      
                  
              
      
              
                  
                  
                      
                      
                      
                      
                      
                  
              
          
      
          
              
              
      
              
              
          
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
    4. 编写SQL映射文件 --> 统一管理SQL语句,解决硬编码问题

      
      
      
      
          
      
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    5. 编码

      1. 定义POJO类

        package com.zxx.pojo;
        
        public class User {
            private String name;
            private String password;
        
            public String getName() {
                return name;
            }
        
            public void setName(String name) {
                this.name = name;
            }
        
            public String getPassword() {
                return password;
            }
        
            public void setPassword(String password) {
                this.password = password;
            }
        
            @Override
            public String toString() {
                return "User{" +
                        "name='" + name + '\'' +
                        ", password='" + password + '\'' +
                        '}';
            }
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
      2. 加载核心配置文件,获取SqlSessionFactory对象

      3. 获取SqlSession对象,执行SQL语句

      4. 释放资源

        package com.zxx;
        
        import com.zxx.pojo.User;
        import org.apache.ibatis.io.Resources;
        import org.apache.ibatis.session.SqlSession;
        import org.apache.ibatis.session.SqlSessionFactory;
        import org.apache.ibatis.session.SqlSessionFactoryBuilder;
        
        import java.io.InputStream;
        import java.util.List;
        
        /*
         * MyBatis快速入门
         */
        public class MyBatis {
            public static void main(String[] args) throws Exception {
                //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
                String resource = "mybatis-config.xml";
                InputStream inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        
                //2.获取SqlSession对象,用它来执行SQL
                SqlSession sqlSession = sqlSessionFactory.openSession();
        
                //3. 执行SQL
                List users = sqlSession.selectList("test.selectAllUser");
        
                System.out.println(users);
        
                //4.释放资源
                sqlSession.close();
            }
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33

    Mapper代理开发

    1. 定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL的映射文件放到同一个路径下,可以在Resources路径下创建与接口路径相同的结构,需要用 / 分隔,编译后会在同一个目录下

    2. 设置SQL映射文件的namespace属性为Mapper接口全限定名

    3. 在Mapper接口中定义方法,方法名就是SQL映射文件中的sql语句的id,并保持参数类型和返回值类型一致

      package com.zxx.mapper;
      
      import com.zxx.pojo.User;
      
      import java.util.List;
      
      public interface UserMapper {
          List selectAllUser();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    4. 编码

      package com.zxx;
      
      import com.zxx.mapper.UserMapper;
      import com.zxx.pojo.User;
      import org.apache.ibatis.io.Resources;
      import org.apache.ibatis.session.SqlSession;
      import org.apache.ibatis.session.SqlSessionFactory;
      import org.apache.ibatis.session.SqlSessionFactoryBuilder;
      
      import java.io.InputStream;
      import java.util.List;
      
      /*
       * MyBatis代理开发
       */
      public class MyBatis2 {
          public static void main(String[] args) throws Exception {
              //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
              String resource = "mybatis-config.xml";
              InputStream inputStream = Resources.getResourceAsStream(resource);
              SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      
              //2.获取SqlSession对象,用它来执行SQL
              SqlSession sqlSession = sqlSessionFactory.openSession();
      
              //3. 执行SQL
              // 3.1 获取UserMapper接口的代理对象
              UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
              List users = userMapper.selectAllUser();
      
              System.out.println(users);
      
              //4.释放资源
              sqlSession.close();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36

    配置文件完成增删改查

    安装MyBatisX插件

    1. 查询
      • 查询所有数据
        • 编写接口方法
        • 编写SQL语句
        • 执行方法
      • 查看详情
      • 条件查询
    2. 添加
    3. 修改
      • 修改全部字段
      • 修改动态字段
    4. 删除
      • 删除一个
      • 批量删除
    实体类
    package com.zxx.pojo;
    
    public class Brand {
        private Integer id;
        private String brandName;
        private String companyName;
        private Integer ordered;
        private String description;
        private Integer status;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getBrandName() {
            return brandName;
        }
    
        public void setBrandName(String brandName) {
            this.brandName = brandName;
        }
    
        public String getCompanyName() {
            return companyName;
        }
    
        public void setCompanyName(String companyName) {
            this.companyName = companyName;
        }
    
        public Integer getOrdered() {
            return ordered;
        }
    
        public void setOrdered(Integer ordered) {
            this.ordered = ordered;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public Integer getStatus() {
            return status;
        }
    
        public void setStatus(Integer status) {
            this.status = status;
        }
    
        @Override
        public String toString() {
            return "Brand{" +
                    "id=" + id +
                    ", brandName='" + brandName + '\'' +
                    ", companyName='" + companyName + '\'' +
                    ", ordered=" + ordered +
                    ", description='" + description + '\'' +
                    ", status=" + status +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    Mapper.xml
    
    
    
    
    
        
    
        
        
            id, brand_name as brandName, company_name as companyName,ordered, description, status
        
    
    
    
        
        
            
            
            
        
    
    
        
    
        
        
    
        
        
    
    
        
    
        
    
        
        
        
        
        
        
        
    
        
        
    
        
    
        
        
            INSERT INTO tb_brand ( brand_name, company_name, ordered, description, status )
            VALUES
            ( #{brandName},#{companyName},#{ordered},#{description},#{status}
            )
        
    
        
            update tb_brand
                set brand_name = #{brandName},
                    company_name = #{companyName},
                    ordered = #{ordered},
                    description = #{description},
                    status = #{status}
            where id = #{id}
        
    
        
            update tb_brand
                
                    
                        brand_name = #{brandName},
                    
                    
                        company_name = #{companyName},
                    
                    
                        ordered = #{ordered},
                    
                    
                        description = #{description},
                    
                    
                        status = #{status}
                    
                
            where id = #{id}
        
    
        
        
            delete from tb_brand where id = #{id}
        
    
        
        
            delete from tb_brand where id
            in(
                
                
                    #{id}
                
             )
    
        
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    Mapper接口
    package com.zxx.mapper;
    
    import com.zxx.pojo.Brand;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    public interface BrandMapper {
        /*
        * 查询所有
        */
        List selectAll();
    
        /*
        * 查看详情
        */
        Brand selectById(Integer id);
    
        /*
        * 多条件动态查询: 散装参数
        */
        List selectByCondition1(@Param("status") int status,@Param("companyName") String companyName,@Param("brandName") String brandName);
    
        /*
         * 多条件动态查询: 对象参数,对象属性的名称要和参数占位符名称一致
         */
        List selectByCondition2(Brand brand);
    
        /*
         * 多条件动态查询:map参数
         */
        List selectByCondition3(Map map);
    
        /*
        * 单条件动态查询
        * */
        List selectByConditionSingle(Brand brand);
    
        /*
        *  添加一个对象
        * */
        void add(Brand brand);
    
        /*
        * 修改全部字段
        * */
        int update(Brand brand);
    
        /*
         * 动态修改字段
         * */
        int updateByCondition(Brand brand);
    
        /*
        * 根据Id删除
        * */
        void deleteById(Integer id);
    
        /*
        * 批量删除
        * */
        void deleteByIds(@Param("ids") int[] ids);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    测试类
    package com.zxx.mapper;
    
    import com.zxx.pojo.Brand;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MyBatis_Test {
    
        @Test
        public void testSelectAll() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            List brands = brandMapper.selectAll();
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testSelectById() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            Brand brands = brandMapper.selectById(2);
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testSelectByCondition1() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            List brands = brandMapper.selectByCondition1(1, "%华为%", "%华为%");
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testSelectByCondition2() throws IOException {
    
            Brand brand = new Brand();
            brand.setStatus(1);
            brand.setCompanyName("%华为%");
            brand.setBrandName("%华为%");
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            List brands = brandMapper.selectByCondition2(brand);
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testSelectByCondition3() throws IOException {
            Map map = new HashMap<>();
            map.put("status", 1);
            //map.put("companyName","%华为%");
            // map.put("brandName","%华为%");
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            List brands = brandMapper.selectByCondition3(map);
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testSelectByConditionSingle() throws IOException {
            Brand brand = new Brand();
            //brand.setStatus(1);
            //brand.setCompanyName("%华为%");
            brand.setBrandName("%华为%");
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            SqlSession sqlSession = sqlSessionFactory.openSession();
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            List brands = brandMapper.selectByConditionSingle(brand);
            System.out.println(brands);
    
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testAdd() throws IOException {
            Brand brand = new Brand();
            brand.setStatus(1);
            brand.setCompanyName("波导手机");
            brand.setBrandName("波导");
            brand.setDescription("手机中的战斗机");
            brand.setOrdered(100);
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.add(brand);
    
            /*//5.手动提交事务
            sqlSession.commit();*/
    
            List brands = brandMapper.selectAll();
            System.out.println(brands);
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testAdd2() throws IOException {
            Brand brand = new Brand();
            brand.setStatus(1);
            brand.setCompanyName("波导手机");
            brand.setBrandName("波导");
            brand.setDescription("手机中的战斗机");
            brand.setOrdered(100);
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.add(brand);
            Integer id = brand.getId();
            System.out.println(id);
    
            /*//5.手动提交事务
            sqlSession.commit();*/
    
            List brands = brandMapper.selectAll();
            System.out.println(brands);
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testUpdate() throws IOException {
            Brand brand = new Brand();
            brand.setStatus(1);
            brand.setCompanyName("波导手机");
            brand.setBrandName("波导");
            brand.setDescription("波导手机,手机中的战斗机");
            brand.setOrdered(200);
            brand.setId(8);
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.update(brand);
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testupdateByCondition() throws IOException {
            Brand brand = new Brand();
            brand.setStatus(4);
            // brand.setCompanyName("波导手机");
            //brand.setBrandName("波导");
            //brand.setDescription("波导手机,手机中的战斗机");
            //brand.setOrdered(200);
            brand.setId(7);
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.updateByCondition(brand);
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testDeleteById() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.deleteById(7);
    
            List brands = brandMapper.selectAll();
            System.out.println(brands);
            //释放资源
            sqlSession.close();
        }
    
        @Test
        public void testDeleteByIds() throws IOException {
            int[] ids = new int[]{5,6};
    
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            brandMapper.deleteByIds(ids);
    
            List brands = brandMapper.selectAll();
            System.out.println(brands);
            //释放资源
            sqlSession.close();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337

    注解完成增删改查

    直接在接口上添加注解 并完成SQL
    package com.zxx.mapper;
    
    import com.zxx.pojo.User;
    import org.apache.ibatis.annotations.Param;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    public interface UserMapper {
        List selectAllUser();
    
        User select(@Param("name") String username, @Param("password") String password);
    
        @Select("select * from user where id=#{id}")
        User selectUserById(int id);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    测试类
    package com.zxx.mapper;
    
    import com.zxx.pojo.User;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    public class MyBatis_Test2 {
    
        @Test
        public void testUserSelect() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
            User user = userMapper.select("'zxx'","'zxxpass'");
            System.out.println(user);
        }
    
        @Test
        public void testUserSelectUserById() throws IOException {
            //1.加载MyBatis的核心配置文件,获取 SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
            //2.获取SqlSession对象,用它来执行SQL
            //SqlSession sqlSession = sqlSessionFactory.openSession();
            //自动提交事务
            SqlSession sqlSession = sqlSessionFactory.openSession(true);
    
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
    
            User user = userMapper.selectUserById(1);
            System.out.println(user);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    SAP-MM-查找采购订单的创建和修改日期
    webrtc 笔记
    Interest Rate|笔记
    企业如何快速采集分析日志?
    Ubuntu终端Terminator的安装
    中通IM测试实践
    解决zsh远程启动后台进程后无法退出的问题
    java学习笔记 day07-Java基础-综合练习
    【多线程与高并发】- 浅谈volatile
    React 前端应用中快速实践 OpenTelemetry 云原生可观测性(SigNoz/K8S)
  • 原文地址:https://blog.csdn.net/qq_45626507/article/details/128177584