springboot整合mybatis实现对MYSQL的定时查询

具体实现详见我的上一篇博客:https://blog.csdn.net/qq_40983417/article/details/82344622
mysql作为免费的数据库拥有广泛的应用,也是我们学习数据库的最合适的第一试炼。
MySQL的application.properties的文件内容如下:

spring.datasource.url=jdbc:mysql://ip地址:端口号/库名
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driverClassName=com.mysql.jdbc.Driver
mybatis.type-aliases-package=com.domain
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
logging.level.root=INFO
logging.level.org.springframework.web=INFO
logging.file=D:/scalDaily_Mysql.log
logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
logging.pattern.file=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
server.port=8380

与上一篇oracle的相比,最大的区别就是url的格式不同,当然,查询语句的写法也是不同的,这一点大家要根据自己的需求去查找资料。按照上一篇的demo,给出一个MySQL查询语句文件的示例:
UserMapper.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" >
<mapper namespace="com.dao.QueryMapper" > 
    <select id="getData"  resultType="com.domain.Data" parameterType="String">
			SELECT * FROM 表名 WHERE ( datediff ( 字段 , '${_parameter}' ) = 0 )
    </select>
</mapper>

其中SELECT * FROM 表名 WHERE ( datediff ( 字段 , ‘${_parameter}’ ) = 0 )这一句是为了查询某一天的所有数据。其中日期格式的范例为"2018-09-18"。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现关联查询需要使用MyBatis的多表查询功能,并在Spring Boot中进行整合。以下是一些基本步骤: 1. 在pom.xml中添加MyBatisMySQL依赖。 2. 创建实体类和Mapper接口。 3. 在MyBatis的Mapper配置文件中编写关联查询语句。 4. 在Spring Boot中配置MyBatis。 5. 在Service中调用Mapper接口方法实现关联查询。 下面是一个示例: 1. 添加依赖 ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 创建实体类和Mapper接口 ``` public class User { private Long id; private String name; private Integer age; private List<Article> articles; // getter and setter } public class Article { private Long id; private String title; private String content; private Long userId; // getter and setter } public interface UserMapper { User getUserById(Long id); } ``` 3. 编写关联查询语句 在MyBatis的Mapper配置文件中编写关联查询语句,例如: ``` <select id="getUserById" resultType="User"> SELECT u.*, a.id as article_id, a.title as article_title, a.content as article_content FROM user u LEFT JOIN article a ON u.id = a.user_id WHERE u.id = #{id} </select> ``` 4. 配置MyBatis 在application.properties文件中配置MyBatis的数据源、Mapper扫描路径等: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath*:mapper/*.xml ``` 5. 在Service中调用Mapper接口方法实现关联查询 ``` @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.getUserById(id); } } ``` 以上就是使用Spring Boot整合MyBatis实现关联查询的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值