您的位置:首页 > 科技 > 能源 > Mybatis入门

Mybatis入门

2024/10/6 1:46:13 来源:https://blog.csdn.net/qq_65095414/article/details/141056527  浏览:    关键词: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 configurationPUBLIC "-//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&amp;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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com