小工具知识网

当前位置: 首页 > 行业知识 > 【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)

行业知识

【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)

2026-10-26 17:17:17 来源:互联网转载

博客主页:从零开始的-CodeNinja之路

⏩ 收录文章:【MyBatis】 MyBatis框架下的高效数据操作:深入理解增删查改(CRUD)

欢迎大家点赞评论收藏⭐文章

目录

  • My Batis
    • 前言(配置环境)
        • 开启驼峰命名(推荐)
        • 打印日志
        • 一. 使用注解方式
            • 1.1 增(Insert)
            • 1.2 删(Delete)
            • 1.3 改(Update)
            • 1.4 查(Select)
            • 二.使用 XML 方式
              • 配置连接字符串和MyBatis
              • 写持久层代码
                • 2.1 增(Insert)
                • 2.2 删(Delete)
                • 2.3 改(Update)
                • 2.4 查(Select)
                • 总结

                  My Batis

                  Mybatis的开发有两种方式:

                  1. 注解
                  2. XML

                  下⾯将学习注解和XML的方式

                  ,使用Mybatis的注解方式,主要是来完成⼀些简单的增删改查功能.如果需要实现复杂的SQL功能,建

                  议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中.

                  前言(配置环境)

                  开启驼峰命名(推荐)

                  通常数据库列使用蛇形命名法进行命名(下划线分割各个单词),而Java属性⼀般遵循驼峰命名法约定.

                  为了在这两种命名方式之间启用自动映射,需要将 mapUnderscoreToCamelCase 设置为true。

                  mybatis:configuration:map-underscore-to-camel-case: true #配置驼峰自动转换

                  驼峰命名规则:abc_xyz=>abcXyz

                  • 表中字段名:abc_xyz

                  • 类中属性名:abcXyz

                  Java代码不做任何处理:

                  @Select("select id, username, `password`, age, gender, phone, delete_flag asdeleteFlag, " +"create_time as createTime, update_time as updateTime from userinfo")public List queryAllUser();

                  添加上述配置,运行代码,字段全部进行正确赋值.

                  打印日志

                  在Mybatis当中我们可以借助日志,查看到sql语句的执行、执行传递的参数以及执行结果在配置文件中进行配置即可

                  mybatis:configuration: # 配置打印 MyBatis日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

                  如果是application.properties,配置内容如下:

                  #指定mybatis输出日志的位置, 输出控制台mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

                  重新运行程序,可以看到SQL执行内容,以及传递参数和执行结果

                  一. 使用注解方式

                  我们学习了Mybatis的查询操作,接下来我们学习MyBatis的增,删,改操作

                  1.1 增(Insert)

                  SQL语句:

                  insert into userinfo (username, `password`, age, gender, phone) values("zhaoliu","zhaoliu",19,1,"18700001234")

                  把SQL中的常量替换为动态的参数

                  Mapper接口

                  @Insert("insert into userinfo (username, `password`, age, gender, phone)values (#{username},#{password},#{age},#{gender},#{phone})")Integer insert(UserInfo userInfo);
                  1.2 删(Delete)

                  SQL语句:

                  delete from userinfo where id=6

                  把SQL中的常量替换为动态的参数

                  Mapper接口

                  @Delete("delete from userinfo where id = #{id}")void delete(Integer id);
                  1.3 改(Update)

                  SQL语句:

                  update userinfo set username="zhaoliu" where id=5

                  把SQL中的常量替换为动态的参数

                  Mapper接口

                  @Update("update userinfo set username=#{username} where id=#{id}")void update(UserInfo userInfo);
                  1.4 查(Select)

                  我们在上⾯查询时发现,有⼏个字段是没有赋值的,只有Java对象属性和数据库字段⼀模⼀样时,才会进行赋值

                  接下来我们多查询⼀些数据

                  @Select("select id, username, `password`, age, gender, phone, delete_flag,create_time, update_time from userinfo")List queryAllUser();

                  二.使用 XML 方式

                  如果需要实现复杂的SQL功能,建议使用XML来配置映射语句,也就是将SQL语句写在XML配置文件中.

                  MyBatisXML的方式需要以下两步:

                  1. 配置数据库连接字符串和MyBatis
                  2. 写持久层代码

                  配置连接字符串和MyBatis

                  此步骤需要进行两项设置,数据库连接字符串设置和MyBatis的XML文件配置。

                  如果是application.yml文件,配置内容如下:

                  # 数据库连接配置spring:datasource:url: jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver# 配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件mybatis:mapper-locations: classpath:mapper/**Mapper.xml

                  如果是application.properties文件,配置内容如下:

                  #驱动类名称spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver#数据库连接的urlspring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis_test?characterEncoding=utf8&useSSL=false#连接数据库的用⼾名spring.datasource.username=root#连接数据库的密码spring.datasource.password=root# 配置 mybatis xml 的文件路径,在 resources/mapper 创建所有表的 xml 文件mybatis.mapper-locations=classpath:mapper/**Mapper.xml

                  写持久层代码

                  持久层代码分两部分

                  1. 方法定义Interface
                  2. 方法实现:XXX.xml
                  2.1 增(Insert)

                  UserInfoMapper接口:

                  Integer insertUser(UserInfo userInfo); 

                  UserInfoMapper.xml实现:

                  insert into userinfo (username, `password`, age, gender, phone) values (#{username}, #{password}, #{age},#{gender},#{phone})

                  以下是对以上标签的说明:

                  • 标签:需要指定 namespace 属性,表示命名空间,值为mapper接口的全限定 名,包括全包名.类名。
                  • 查询标签:是用来执行数据库的查询操作的:
                  • id :是和 Interface (接口)中定义的方法名称⼀样的,表示对接口的具体实现方法。
                  • resultType :是返回的数据类型,也就是开头我们定义的实体类.
                    2.2 删(Delete)

                    UserInfoMapper接口:

                    Integer deleteUser(Integer id);

                    UserInfoMapper.xml实现:

                    delete from userinfo where id = #{id}
                    2.3 改(Update)

                    UserInfoMapper接口:

                    Integer updateUser(UserInfo userInfo);

                    UserInfoMapper.xml实现:

                    update userinfo set username=#{username} where id=#{id}
                    2.4 查(Select)

                    同样的,使用XML的方式进行查询,也存在数据封装的问题我们把SQL语句进行简单修改,查询更多的字段内容

                    select id, username,`password`, age, gender, phone, delete_flag,create_time, update_time from userinfo

                    运行结果:

                    总结

                    MySQL开发企业规范

                    1. 表名,字段名使用小写字母或数字,单词之间以下划线分割.尽量避免出现数字开头或者两个下划线

                      中间只出现数字.数据库字段名的修改代价很⼤,所以字段名称需要慎重考虑。

                    MySQL在Windows下不区分⼤小写,但在Linux下默认是区分⼤小写.因此,数据库名,表名,字

                    段名都不允许出现任何⼤写字母,避免节外⽣枝 正例:aliyun_admin,rdc_config,level3_name

                    反例:AliyunAdmin,rdcConfig,level_3_name

                    1. 表必备三字段:id,create_time,update_time

                    id必为主键,类型为bigintunsigned,单表时自增,步⻓为1

                    create_time,update_time的类型均为datetime类型,create_time表示创建时间,

                    update_time表示更新时间 有同等含义的字段即可,字段名不做强制要求

                    1. 在表查询中,避免使用*作为查询的字段列表,标明需要哪些字段(课堂上给⼤家演示除外).

                      1. 增加查询分析器解析成本
                      2. 增减字段容易与resultMap配置不⼀致
                      3. 无用字段增加网络消耗,尤其是text类型的字段

上一篇:MBR与GPT,磁盘分区技术的核心详解

下一篇:食堂饭卡服务器怎么用?