Mybatis入门
基础介绍#
官网
https://blog.mybatis.org/
MyBatis本是apache的一个开源项目iBatis,2010年更改项目为MyBatis。
mybatis入门(参考手册) #
1.创建Maven工程
2.添加mybais依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.16</version> </dependency>
3.配置文件中配置数据库连接信息
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 配置日志框架使用LOGBACK 也可以是 SLF4J--> <setting name="logImpl" value="SLF4J"/> <!-- 开启下划线转小驼峰 true开启--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <environments default="development"> <environment id="development"> <!-- 配置使用jdbc事物 对应mysql的 begin/commit/rollback--> <transactionManager type="JDBC"/> <!-- 配置数据库连接池--> <dataSource type="POOLED"> <!-- 配置基本mysql连接信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/news?serverTimezone=Asia/Shanghai&characterEncoding=utf-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- <mappers>--> <!-- <mapper resource="org/mybatis/example/BlogMapper.xml"/>--> <!-- </mappers>--> <!--注册mapper接口--> <mappers> <mapper class="com.lu.mapper.UserMapper"/> </mappers> </configuration>
简单解释这个配置:
<settings>中的logImpl属性指定使用Log4j2输出日志以及开启下划线转驼峰映射
<transactionManager> 指定事务管理类型,type="JDBC"指直接使用JDBC的提交与回滚
<environments>环境配置中主要配置了数据库连接包括数据库驱动,url,用户名,密码
<mappers>中配置了包含完整类路径的xml,这是一个mybatis的sql语句和映射配置文件(即你的sql应当写在相应xml中,mybatis才能读到).之后会详细解释.
4.添加mysql驱动
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.32</version> </dependency>
4.书写测试代码
注解使用SQL #
1.就是直接将sql写在接口的方法上#
优点: 对于简单需求的sql,效率较高
缺点: 当sql有变化时需要重新编译代码
因此建议一般sql比较简单使用注解开发
2.相关注解 #
@Select
@Select("select * from user") ArrayList<User> selectUser();
@Param
给sql绑定方法参数
/** * 根据id查询用户信息 * @param id * @return 返回用户信息 */ @Select("select * from user where id = #{id}") User selectUserById(@Param("id") int id);
@Insert
/** * 添加一条数据 * @param user * @return */ @Insert("insert into user(user_name,full_name,password,role) values (#{user.userName},#{user.fullName},#{user.password},#{user.role})") int insertUser(@Param("user") User user);
- 概要获取刚刚插入数据的主键值(插入回显)
- 使用selectKey,并且使用MySQL的last_insert_id()函数时,before必为false,也就是说必须先插入然后执行last_insert_id()才能获得刚刚插入数据的ID
@SelectKey(statement = "select last_insert_id()" ,keyProperty = "id" ,resultType = Integer.class, before = false)
before=false,先插入,再查询,这时只能将结果赋给keyProperty
resultType 用于指定statement的sql执行后的返回值
keyProperty表示查询结果赋值给代码中的哪个对象的属性值,keyColumn表示将查询结果赋值给数据库表中哪一列
statement是要运行的SQL语句,它的返回值通过resultType来指定
@Delete
SqlSession sqlSession = build.openSession(true);//mybatis默认开启事物但没有开启事物提交这个设置为true就是开启提交
/** * 通过id删除用户 * 增删改返回值都是int 因为jdbc接受的是mysql受影响的行数 * @param id * @return */ @Delete("delete from user where id = #{id}") int DeleteUserById(@Param("id") int id);
@Update
/** * 修改用户信息 * 通过id更新 * 注意: * 一般指定id * 注意实体类对象的属性赋值一定要和表的字段的结束对应 * 例如非空一定要赋值,唯一的不能重复,数据长度 * @param user * @return */ @Update("update user set user_name = #{user.userName},full_name = #{user.fullName} where id = #{user.id}") int updateUser(@Param("user") User user);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本