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>