MyBatis的集成与基本写法

2024年重新认识一下原始的Mybatis的用法

mybatisPlus用多了
tk.mybatis用多了
现在我们回到最初的mybatis的用法

又回到最初的起点 记忆中你青涩的脸 我们终於来到了这一天 桌垫下的老照片 无数回忆连结 今天男孩要赴女孩最后的约

集成到springboot

POM集成

        <!-- MyBatis Starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!-- 数据库驱动,例如MySQL -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    password: pwd
    url: jdbc:mysql://ip:3306/database?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
mybatis:
  mapper-locations: classpath:mapper/*.xml     # 这个是资源文件夹resources/mapper所有xml都写到这里
  type-aliases-package: xyz.zhengbanwansui.stock.helper # 自动发现所有子包的实体类,xml里不用写实体类的前缀包名了
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 日志显示sql和传参

写mapper接口和对应的XML

实体类

@Data
public class MysqlTodoDO {
    private Long id;
    private String todo;
    private Integer finish;
    private Date createTime;
}

Mapper接口

@Mapper
public interface MysqlTodoMapper {

    int insertOne(MysqlTodoDO mysqlTodoDO);

    int deleteOne(Long id);

    List<MysqlTodoDO> listAll();

    MysqlTodoDO getOne(Long id);

    int finishOne(Long id);

}

XML

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xyz.zhengbanwansui.stock.helper.todo.mapper.MysqlTodoMapper">
    <resultMap id="mysqlTodoMap" type="MysqlTodoDO">
        <id column="id" property="id"/>
        <result column="todo" property="todo"/>
        <result column="finish" property="finish"/>
        <result column="create_time" property="createTime"/>
    </resultMap>
    <insert id="insertOne" parameterType="MysqlTodoDO">
        INSERT INTO nexusphp.atis_todo (id, todo, finish, create_time)
        VALUES (#{id}, #{todo}, #{finish}, #{createTime})
    </insert>
    <delete id="deleteOne" parameterType="long">
        delete from nexusphp.atis_todo where id = #{id}
    </delete>
    <select id="listAll" resultMap="mysqlTodoMap">
        select * from nexusphp.atis_todo
    </select>
    <select id="getOne" parameterType="long" resultMap="mysqlTodoMap">
        select * from nexusphp.atis_todo where id = #{id}
    </select>
    <update id="finishOne" parameterType="long">
        update nexusphp.atis_todo
        set finish = 1
        where id = #{id}
    </update>
</mapper>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值