创建springboot项目所需依赖(最右侧)如下图:
Mapper.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namesparce对应dao层的路径,id对应dao层方法名,resultType对应实体类路径,给dao层类上添加@Mapper注解-->
<mapper namespace="com.btrc.mapper.LogMapper">
<select id="findAll" resultType="com.btrc.entity.Log">
select * from log
</select>
<!--通过id查询-->
<select id="findByLogId" resultType="com.btrc.entity.Log">
SELECT * FROM log WHERE log_id = #{log_id}
</select>
<!--查询所有的最新数据-->
<select id="findByMaxTime" resultType="com.btrc.entity.Log">
select
g2.*
from
(select log_id,name,max(logal) logal from log group by log_id,name) g1
left join log g2
on g1.log_id = g2.log_id and g1.logal = g2.logal and g1.name = g2.name
</select>
<!--插入数据-->
<insert id="insertLog" keyProperty="id" useGeneratedKeys="false">
insert into log(id, log_id, name, logal, beginTime, overTime, status, time)
values (#{id}, #{log_id}, #{name}, #{logal}, #{beginTime}, #{overTime}, #{status}, #{time})
</insert>
<!--条件查询根据时间查询-->
<select id="findByStatusOrTime" resultType="com.btrc.entity.Log">
select
id,log_id, name, logal, beginTime, overTime, status, time
from log
<where>
<if test="beginTime != '' and beginTime != null">
and beginTime >= #{beginTime,jdbcType=DATE}
</if>
<if test="overTime != '' and overTime != null">
and overTime <= #{overTime,jdbcType=DATE}
</if>
<if test="status != '' and status != null">
and status = #{status}
</if>
</where>
</select>
</mapper>
Application.propetites改名为Application.yml
server:
port: 9001
spring:
#数据库配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/bigdata?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: root
#查找mapper.xml所在位置 一般在resources根目录下
mybatis:
mapper-locations: classpath:mapper/*.xml