• Mybatis简介


    一 什么是MyBatis?

    • MyBatis是一款优秀的持久层框架,用于简化JDBC开发
    • MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apache software foundation 迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github
    • 官网:https://mybatis.org/mybatis-3/zh/index.html
    • 持久层
      • 负责将数据到保存到数据库的那一层代码
      • JavaEE三层架构:表现层【页面展示】、业务层【逻辑处理】、持久层【保存数据】
    • 框架
      • 框架就是一个半成品软件,是一套可重用的、通用的、软件基础代码模型
      • 在框架的基础之上构建软件编写更加高效、规范、通用、可扩展
        在这里插入图片描述

    二 MyBatis快速入门

    在这里插入图片描述

    在这里插入图片描述

    2.1 解决SQL映射文件的警告提示

    • 产生原因:idea和数据库没有建立连接,不识别表信息
    • 解决方式:在idea中配置MySQL数据库连接
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述

    三 Mapper代理开发

    在这里插入图片描述
    在这里插入图片描述

    • 使用Mapper代理方式完成入门案例
    1. 定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下
    2. 设置SQL映射文件的namespace属性为Mapper接口全限定名
    3. 在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
    4. 编码
      • 通过SqlSession的getMapper方法获取Mapper接口的代理对象
      • 调用对应方法完成sal的执行

    细节:如果Mapper接口名称和SQL映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简
    SOL映射文件的加载

    四 MyBatis核心配置文件

    在这里插入图片描述
    在这里插入图片描述

    • 类型别名(typeAliases)
    • 细节:配置各个标签时,需要遵守前后顺序

    4.1 MyBatisX插件

    在这里插入图片描述

    • MybatisX是一款基于IDEA的快速开发插件,为效率而生
    • 主要功能:
      • XML和接口方法相互跳转
      • 根据接口方法生成statement

    五 配置文件完成增删改查

    5.1 查询-查询所有数据

    1. 编写接口方法:Mapper接口
      • 参数:无
      • 结果:List< Brand>
    2. 编写sql语句:SQL映射文件
    3. 执行方法,测试
      在这里插入图片描述
    • MyBatis完成操作需要几步?
      三步:编写接口方法–>编写SQL–>执行方法

    在这里插入图片描述

    • 方法一:起别名
      在这里插入图片描述
      Sql片段
      在这里插入图片描述
      方法二:
      在这里插入图片描述

    • 实体类属性名和数据库表列名不一致,不能自动封装数据
      1)起别名:在SQL语句中,对不一样的列名起别名,别名和实体类属性名一样
      *可以定义< sql >片段,提升复用性
      2)resultMap:定义 < resultMap > 完成不一致的属性名和列名的映射

    5.2 查询-查看详情

    1. 编写接口方法:Mapper接口
      • 参数:id
      • 结果:Brand
    2. 编写SQL语句:SOL映射文件
    3. 执行方法,测试
      在这里插入图片描述
    • 参数占位符:
      1)#{}:执行SQL时,会将#占位符替换为?将来自动设置参数值
      2)¥{}:拼SQL。会存在SQL注入问题
      3)使用时机:
    • 参数传递,都使用#()
      如果要对表名、列名进行动态设置,只能使用$()进行sql拼接
    • parameterType
      用于设置参数类型,该参数可以省略
    • SQL语句中特殊字符处理:
      转义字符

    5.3 代码

    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/3-14:56
     */
    public class User {
        private Integer id;
        private String username;
        private String password;
        private String gender;
        private String addr;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getGender() {
            return gender;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    
        public String getAddr() {
            return addr;
        }
    
        public void setAddr(String addr) {
            this.addr = addr;
        }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    ", gender='" + gender + '\'' +
                    ", addr='" + addr + '\'' +
                    '}';
        }
    }
    
    
    • 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
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--namespace:命名空间-->
    <mapper namespace="com.yang.mapper.UserMapper">
        <!--statement-->
        <select id="selectAll" resultType="com.yang.pojo.User">
            select * from tb_user;
        </select>
    </mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.yang;
    
    import com.yang.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.IOException;
    import java.io.InputStream;
    import java.util.List;
    
    /**
     * @author 缘友一世
     * @date 2022/7/3-15:03
     * mybatis快速入门
     */
    public class MyBatisDemo {
        public static void main(String[] args) 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.执行sql
            List<User> users = sqlSession.selectList("test.selectAll");
    
            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

    六 动态SQL

    6.1 多条件查询

    在这里插入图片描述

    /**
         *条件查询
         *  *参数接受
         *      1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
         *      2.对象参数:对象的属性名称要和参数占位符名称一致
         *      3.map集合参数:
         *
         * @param //status
         * @param //companyName
         * @param //brandName
         * @return
         */
        //List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName);
    
        //List<Brand> selectByCondition(Brand brand);
    
        List<Brand> selectByCondition(HashMap map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
     <resultMap id="brandResultMap" type="brand">
            <result column="brand_name" property="brandName"></result>
            <result column="company_name" property="companyName"></result>
        </resultMap>
    <!--
            动态条件查询
                *if:条件判断
                    *test:逻辑表达式
                *问题:
                    * 恒等式
                    * <where> 替换where 关键字
        -->
        <select id="selectByCondition" resultMap="brandResultMap">
            select *
            from tb_brand
           /* where 1=1*/
           <where>
                  <if test="status!=null">
                      and status=#{status}
                  </if>
                  <if test="companyName!=null and companyName!=''">
                    and company_name like #{companyName}
                  </if>
                  <if test="brandName !=null and brandName!=''">
                      and brand_name like #{brandName};
                  </if>
           </where>
        </select>
    
    
    • 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
    public static void testSelectByCondition() throws IOException {
            //接受参数
            int status=1;
            String companyName="华为";
            String brandName="华为";
    
            //处理参数
            companyName="%"+companyName+"%";
            brandName="%"+brandName+"%";
    
            //封装对象
            /*Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);*/
    
            HashMap map = new HashMap();
    
            //map.put("status",status);
            map.put("companyName",companyName);
            //map.put("brandName",brandName);
    
            //1.加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //2.获取sqlSession对象,用它来执行sql
            //如果没有设置,默认是false,这时候我们需要手动设置事务提交了。
            //如果我们设置了true那么就是自动提交了
            SqlSession sqlSession = sqlSessionFactory.openSession(false);
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
    
            List<Brand> brands = brandMapper.selectByCondition(map);
    
            //List<Brand> brands = brandMapper.selectByCondition(map);
    
            System.out.println(brands);
    
            //5.释放资源
            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

    在这里插入图片描述

    • SQL语句设置多个参数有几种方式?
      散装参数:
      • 1)需要使用@Param(“sQL中的参数占位符名称”)
      • 2)实体类封装参数
        只需要保证SQL中的参数名和实体类属性名对应上,即可设置成功
      • 3)map集合
        只需要保证SQL中的参数名和map集合的键的名称对应上,c即可设成
        在这里插入图片描述

    6.2 多条件动态查询

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    6.3 单条件动态查询

    在这里插入图片描述

    /**
         * 单条件动态查询
         * @param brand
         * @return
         */
        List<Brand> selectByConditionSingle(Brand brand);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    <resultMap id="brandResultMap" type="brand">
            <result column="brand_name" property="brandName"></result>
            <result column="company_name" property="companyName"></result>
        </resultMap>
    <select id="selectByConditionSingle" resultMap="brandResultMap">
            select *
            from tb_brand
            where
            <choose> <!--类似switch-->
                <when test="status!=null"> <!--类似于case-->
                    status=#{status}
                </when>
                <when test="companyName!=null and company!=''">
                    company_name like #{companyName}
                </when>
                <when test="branName!=null and brandName!=''">
                    brand_name like #{brandName}
                </when>
                <otherwise> <!--类似与default-->
                    1=1
                </otherwise>
            </choose>
        </select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    6.4 添加

    在这里插入图片描述

    /**
         * 添加
         * @param brand
         */
        void add(Brand brand);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    <!--修改所有字段-->
        <update id="update">
            update tb_brand
            set brand_name=#{brandName},
                company_name=#{companyName},
                ordered=#{ordered},
                description=#{description},
                status=#{status}
            where id=#{id};
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    public static void testAdd() throws IOException {
            //接受参数
            int status=1;
            String companyName="导波手机";
            String brandName="导波手机";
            String description="手机中的战斗机";
            int ordered=100;
    
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
            brand.setDescription(description);
            brand.setOrdered(ordered);
    
            //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.执行方法
            brandMapper.add(brand);
            Integer id=brand.getId();
            System.out.println(id);
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            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

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    6.5 添加–主键返回

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    6.7 动态修改

    在这里插入图片描述

    <!--动态修改字段-->
        <update id="ActiveUpdate">
            update tb_brand
            <set>
                <if test="brandName != null and brandName != ''">
                    brand_name=#{brandName},
                </if>
                <if test="companyName != null and companyName !=''">
                    companyName=#{companyName}
                </if>
                <if test="ordered != null">
                    ordered=#{ordered},
                </if>
                <if test="description != null and description != ''">
                    description=#{description},
                </if>
                <if test="status != null">
                    status=#{status}
                </if>
            </set>
            where id = #{id};
        </update>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
     public static void testActiveUpdate() throws IOException {
            //接受参数
            int status=0;
            String companyName="先科手机";
            String brandName="先科手机";
            String description="导波手机,手机中的战斗机";
            int ordered=150;
            int id=768;
    
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            //brand.setCompanyName(companyName);
            //brand.setBrandName(brandName);
            //brand.setDescription(description);
            //brand.setOrdered(ordered);
            brand.setId(id);
    
            //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.执行方法
            int count=brandMapper.ActiveUpdate(brand);
    
            if(count>0) {
                System.out.println("更新成功!");
            }else {
                System.out.println("更新失败!");
            }
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            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

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    6.8 批量删除

    在这里插入图片描述

     <!--
            批量删除
            mybatis会将数组参数,封装为一个Map对象
                * 默认:Array 数组
                * 使用@Param注解改变map集合的默认Key的名称
                * separator="," 分隔符
                * open="(" close=")" 拼接字符
            -->
        <delete id="deleteByIds">
            delete from tb_brand where id
        in
                <foreach collection="ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            ;
        </delete>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public static void testDeleteByIds() throws IOException {
            //接受参数
            int[] ids={3,4};
    
            //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.执行方法
            brandMapper.deleteByIds(ids);
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            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

    在这里插入图片描述

    6.9 所有的代码

    Brand.java

    package com.yang.pojo;
    
    /**
     * @author 缘友一世
     * @date 2022/7/2-15:09
     * 品牌
     * 快捷键 alt+鼠标左键——整列编辑
     *       Ctrl+r——快速替换
     */
    public class Brand {
        // id主键
        private Integer id;
    	// 品牌名称
        private String brandName;
    	// 企业名称
        private String companyName;
    	// 排序字段
        private Integer ordered;
        // 描述信息
        private String description;
    	// 状态:0:禁用 1:启用
        //在实体类中,基本数据类型建议使用对应的包装类型 默认值是NULL
    	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
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    BrandMapper.java

    package com.yang.mapper;
    
    import com.yang.pojo.Brand;
    import com.yang.pojo.User;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.HashMap;
    import java.util.List;
    
    /**
     * @author 缘友一世
     * @date 2022/7/3-19:53
     */
    public interface BrandMapper {
        /**
         * 查询所有
         */
        List<Brand> selectAll();
        /**
         * 查询详情
         */
        Brand selectById(int id);
    
        /**
         *条件查询
         *  *参数接受
         *      1.散装参数:如果方法中有多个参数,需要使用@Param("SQL参数占位符名称")
         *      2.对象参数:对象的属性名称要和参数占位符名称一致
         *      3.map集合参数:
         *
         * @param //status
         * @param //companyName
         * @param //brandName
         * @return
         */
        //List<Brand> selectByCondition(@Param("status")int status,@Param("companyName")String companyName,@Param("brandName")String brandName);
    
        //List<Brand> selectByCondition(Brand brand);
    
        List<Brand> selectByCondition(HashMap map);
    
        /**
         * 单条件动态查询
         * @param brand
         * @return
         */
        List<Brand> selectByConditionSingle(Brand brand);
    
        /**
         * 添加
         * @param brand
         */
        void add(Brand brand);
    
        /**
         * 更新所有字段
         */
        int update(Brand brand);
    
        /**
         * 动态更新字段
         */
        int ActiveUpdate(Brand brand);
    
        /**
         * 删除
         */
        void deleteById(int 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
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    MyBatisTest.java

    package com.yang.test;
    
    import com.yang.mapper.BrandMapper;
    import com.yang.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 java.io.IOException;
    import java.io.InputStream;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * @author 缘友一世
     * @date 2022/7/3-22:05
     */
    public class MyBatisTest {
        public static void main(String[] args) throws IOException {
            System.out.println("1……查询所有");
            System.out.println("2……条件查询");
            System.out.println("3……动态条件查询");
            System.out.println("4……添加记录");
            System.out.println("5……更新所有记录");
            System.out.println("6……动态更新记录");
            System.out.println("7……删除记录");
            System.out.println("8……批量删除");
            System.out.println("请输入您的选择:");
    
            Scanner scanner = new Scanner(System.in);
            int select=scanner.nextInt();
            scanner.close();
                switch (select) {
                    case 1:
                        testSelectAll();
                        break;
                    case 2:
                        testSelectById();
                        break;
                    case 3:
                        testSelectByCondition();
                        break;
                    case 4:
                        testAdd();
                        break;
                    case 5:
                        testUpdate();
                        break;
                    case 6:
                        testActiveUpdate();
                        break;
                    case 7:
                        testDeleteById();
                        break;
                    case 8:
                        testDeleteByIds();
                        break;
                    default:
                        System.out.println("请重新输入!");
                        break;
                }
        }
        public static 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<Brand> brands = brandMapper.selectAll();
    
            System.out.println(brands);
            //5.释放资源
            sqlSession.close();
        }
        public static void testSelectById() throws IOException {
            //设置参数
            int id=1;
            //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 brand = brandMapper.selectById(id);
            System.out.println(brand);
    
            //5.释放资源
            sqlSession.close();
        }
        public static void testSelectByCondition() throws IOException {
            //接受参数
            int status=1;
            String companyName="华为";
            String brandName="华为";
    
            //处理参数
            companyName="%"+companyName+"%";
            brandName="%"+brandName+"%";
    
            //封装对象
            /*Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);*/
    
            HashMap map = new HashMap();
    
            //map.put("status",status);
            map.put("companyName",companyName);
            //map.put("brandName",brandName);
    
            //1.加载mybatis的核心配置文件,获取SqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //2.获取sqlSession对象,用它来执行sql
            //如果没有设置,默认是false,这时候我们需要手动设置事务提交了。
            //如果我们设置了true那么就是自动提交了
            SqlSession sqlSession = sqlSessionFactory.openSession(false);
            //3.获取Mapper接口的代理对象
            BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
    
            //4.执行方法
            //List<Brand> brands = brandMapper.selectByCondition(status, companyName, brandName);
    
            List<Brand> brands = brandMapper.selectByCondition(map);
    
            //List<Brand> brands = brandMapper.selectByCondition(map);
    
            System.out.println(brands);
    
            //5.释放资源
            sqlSession.close();
        }
    
        public static void testAdd() throws IOException {
            //接受参数
            int status=1;
            String companyName="导波手机";
            String brandName="导波手机";
            String description="手机中的战斗机";
            int ordered=100;
    
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
            brand.setDescription(description);
            brand.setOrdered(ordered);
    
            //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.执行方法
            brandMapper.add(brand);
            Integer id=brand.getId();
            System.out.println(id);
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            sqlSession.close();
        }
    
        public static void testUpdate() throws IOException {
            //接受参数
            int status=1;
            String companyName="导波手机";
            String brandName="导波手机";
            String description="导波手机,手机中的战斗机";
            int ordered=200;
            int id=768;
    
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            brand.setCompanyName(companyName);
            brand.setBrandName(brandName);
            brand.setDescription(description);
            brand.setOrdered(ordered);
            brand.setId(id);
    
            //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.执行方法
            int count=brandMapper.update(brand);
    
            if(count>0) {
                System.out.println("更新成功!");
            }else {
                System.out.println("更新失败!");
            }
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            sqlSession.close();
        }
        public static void testActiveUpdate() throws IOException {
            //接受参数
            int status=0;
            String companyName="先科手机";
            String brandName="先科手机";
            String description="导波手机,手机中的战斗机";
            int ordered=150;
            int id=768;
    
            //封装对象
            Brand brand = new Brand();
            brand.setStatus(status);
            //brand.setCompanyName(companyName);
            //brand.setBrandName(brandName);
            //brand.setDescription(description);
            //brand.setOrdered(ordered);
            brand.setId(id);
    
            //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.执行方法
            int count=brandMapper.ActiveUpdate(brand);
    
            if(count>0) {
                System.out.println("更新成功!");
            }else {
                System.out.println("更新失败!");
            }
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            sqlSession.close();
        }
        public static void testDeleteById() throws IOException {
            //接受参数
            int id=768;
    
            //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.执行方法
            brandMapper.deleteById(id);
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            sqlSession.close();
        }
        public static void testDeleteByIds() throws IOException {
            //接受参数
            int[] ids={3,4};
    
            //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.执行方法
            brandMapper.deleteByIds(ids);
    
            //5.注意:需要手动提交事务,否则自动回滚
            sqlSession.commit();
    
            //6.释放资源
            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

    BrandMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--namespace:命名空间-->
    <mapper namespace="com.yang.mapper.BrandMapper">
        <!--
            数据库的字段名和实体类的属性名 不一致,则不能自动封装数据
            *起别名:对不一样的列名起别名,让别名和实体类的属性名一样
                *缺点:每次查询都要定义一次别名
                    *sql片段
                        *缺点:不灵活
            *resultMap:
                1.定义<resultMap>标签
                    //id:唯一标识
                    //Type:映射的类型,支持别名
                    <resultMap id="" type="">
                            /*
                                id:完成主键字段的映射
                                column:表的列名
                                property:实体类的属性名
                                result:完成一般字段的映射
                                column:表的列名
                                property:实体类的属性名
                            */
                            <id column="" property=""></id>
                            <result column="" property=""></result>
                    </resultMap>
    
                2.在<select>标签中,使用resultMap属性替换resultType属性
    
        -->
        
        <resultMap id="brandResultMap" type="brand">
            <result column="brand_name" property="brandName"></result>
            <result column="company_name" property="companyName"></result>
        </resultMap>
    
        <select id="selectAll" resultMap="brandResultMap">
            select
            *
            from tb_brand;
        </select>
    
        <!--
            &lt;!&ndash;sql片段&ndash;&gt;
            <sql id="brand_column">id, brand_name brandName, company_name companyName, ordered, description, status</sql>
            <select id="selectAll" resultType="Brand">
                select
                <include refid="brand_column"></include>
                from tb_brand;
            </select>
            -->
        
        <!--<select id="selectAll" resultType="Brand">
            select *
            from tb_brand;
        </select>-->
        <!--
            *参数占位符
                1.#{}:会将其替换为?,为了防止SQL注入
                2.${}:拼SQL,会存在SQL注入问题
                3.使用时机:
                    *参数传递的时候使用:#{}
                    *表名或者列名不固定的情况下${},会存在SQL注入的问题
            *特殊字符处理
                1.转义字符: <等价于&lt;
                2.CDATA区
        -->
        <select id="selectById" resultMap="brandResultMap">
            select *
            from tb_brand where id
            <![CDATA[
            <=
            ]]>
            #{id};
        </select>
    
        <!--条件查询-->
        <!--<select id="selectByCondition" resultMap="brandResultMap">
            select *
            from tb_brand
            where
                    status=#{status}
                    and company_name like #{companyName}
                    and brand_name like #{brandName};
        </select>-->
    
        <!--
            动态条件查询
                *if:条件判断
                    *test:逻辑表达式
                *问题:
                    * 恒等式
                    * <where> 替换where 关键字
        -->
        <select id="selectByCondition" resultMap="brandResultMap">
            select *
            from tb_brand
           /* where 1=1*/
           <where>
                  <if test="status!=null">
                      and status=#{status}
                  </if>
                  <if test="companyName!=null and companyName!=''">
                    and company_name like #{companyName}
                  </if>
                  <if test="brandName !=null and brandName!=''">
                      and brand_name like #{brandName};
                  </if>
           </where>
        </select>
    
    
        <!--单条件动态查询-->
        <!--一级形态-->
        <!--二级形态-->
        <!--也可以使用<where>标签-->
        <select id="selectByConditionSingle" resultMap="brandResultMap">
            select *
            from tb_brand
            where
            <choose> <!--类似switch-->
                <when test="status!=null"> <!--类似于case-->
                    status=#{status}
                </when>
                <when test="companyName!=null and company!=''">
                    company_name like #{companyName}
                </when>
                <when test="branName!=null and brandName!=''">
                    brand_name like #{brandName}
                </when>
                <otherwise> <!--类似与default-->
                    1=1
                </otherwise>
            </choose>
        </select>
    
        <insert id="add" useGeneratedKeys="true" keyProperty="id">
            insert into tb_brand (brand_name, company_name, ordered, description, status)
            values (#{brandName},#{companyName},#{ordered},#{description},#{status});
        </insert>
    
        <!--修改所有字段-->
        <update id="update">
            update tb_brand
            set brand_name=#{brandName},
                company_name=#{companyName},
                ordered=#{ordered},
                description=#{description},
                status=#{status}
            where id=#{id};
        </update>
    
        <!--动态修改字段-->
        <update id="ActiveUpdate">
            update tb_brand
            <set>
                <if test="brandName != null and brandName != ''">
                    brand_name=#{brandName},
                </if>
                <if test="companyName != null and companyName !=''">
                    companyName=#{companyName}
                </if>
                <if test="ordered != null">
                    ordered=#{ordered},
                </if>
                <if test="description != null and description != ''">
                    description=#{description},
                </if>
                <if test="status != null">
                    status=#{status}
                </if>
            </set>
            where id = #{id};
        </update>
    
        <!--删除-->
        <delete id="deleteById">
            delete from tb_brand
            where id=#{id};
        </delete>
        <!--
            批量删除
            mybatis会将数组参数,封装为一个Map对象
                * 默认:Array 数组
                * 使用@Param注解改变map集合的默认Key的名称
                * separator="," 分隔符
                * open="(" close=")" 拼接字符
            -->
        <delete id="deleteByIds">
            delete from tb_brand where id
        in
                <foreach collection="ids" item="id" separator="," open="(" close=")">
                    #{id}
                </foreach>
            ;
        </delete>
    </mapper>
    
    • 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

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yang</groupId>
        <artifactId>mybits-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <!--mybatis依赖-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.5</version>
            </dependency>
            <!--mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.29</version>
            </dependency>
            <!--junit单元测试-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
            <!--添加slf4j日志api-->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.20</version>
            </dependency>
            <!--添加logback-class依赖-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!--添加logback-core依赖-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
        </dependencies>
    
    </project>
    
    • 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

    logback.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <!--CONSOLE :表示当前的日志信息是可以输出到控制台的。-->
        <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>【%level】 %blue(%d{HH:mm:ss.SSS}) %cyan(【%thread】) %boldGreen(%logger{15}) - %msg %n</pattern>
            </encoder>
        </appender>
    
        <logger name="com.yang" level="DEBUG" additivity="false">
            <appender-ref ref="Console"/>
        </logger>
    
        <root level="DEBUG">
            <appender-ref ref="Console"/>
        </root>
    </configuration>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    mybatis-config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        
        <typeAliases>
            <package name="com.yang.pojo"/>
        </typeAliases>
        <environments default="development">
            <!--environment:配置数据库连接环境,可以配置多个environment,通过default属性切换不同的environment-->
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <!--数据库连接信息-->
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql:///mybatis?useSSL=false"/>
                    <property name="username" value="root"/>
                    <property name="password" value="??????????"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <!--加载sql映射文件-->
    
            <!--Mapper代理方式-->
            <package name="com.yang.mapper"/>
        </mappers>
    </configuration>
    
    • 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

    七 注解完成增删改查

    • 使用注解开发会比配置文件开发更加方便
    @Select("select*from tb_user whereid=#(id)")
    publicUser selectByld(intid):
    
    • 1
    • 2

    查询:@Select
    添加:@lnsert
    修改:@Update
    删除:@Delete

    • 提示:
      • 注解完成简单功能
      • 配置文件完成复杂功能
    • 使用注解来映射简单语句会使代码显得更加简洁,但对于稍微复杂一点的语句,Java 注解不仅力不从心,还会让你本就复杂的 SQL 语句更加混乱不堪。 因此,如果你需要做一些很复杂的操作,最好用 XML 来映射语句。
    • 选择何种方式来配置映射,以及认为是否应该要统一映射语句定义的形式,完全取决于你和你的团队。 换句话说,永远不要拘泥于一种方式,你可以很轻松的在基于注解和 XML 的语句映射方式间自由移植和切换。

    八 MyBatis参数传递

    在这里插入图片描述

  • 相关阅读:
    【CCPC2020长春站】【区间dp】Abstract Painting
    呼叫中心系统信息发送功能的应用
    网络是什么?(网络零基础入门篇)
    mysql 数据备份与恢复使用详解
    目标检测YOLO实战应用案例100讲-面向路边停车场景的目标检测(中)
    大数据NoSQL数据库HBase集群部署
    音视频添 加水印
    Golang 的锁机制
    [C++基础]-类和对象(中~万字详解)
    0递归中等 LeetCode306. 累加数
  • 原文地址:https://blog.csdn.net/yang2330648064/article/details/125628674