springboot项目中用mybatis写一个查询语句
时间: 2023-12-21 08:04:54 浏览: 43
好的,我可以给您提供一个例子:
首先在pom.xml中添加相关依赖:
```xml
<dependencies>
<!-- Spring Boot MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!-- MySQL 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
```
然后在application.properties中配置数据源和MyBatis:
```properties
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# MyBatis 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.domain
```
其中,"mydatabase"是你所操作的数据库名称,"root"是数据库用户名,"123456"是该用户名对应的密码。
接下来,定义一个实体类User:
```java
package com.example.domain;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
}
```
然后,定义一个Mapper接口UserMapper:
```java
package com.example.mapper;
import com.example.domain.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> findUsersByAge(@Param("age") Integer age);
}
```
接着,创建一个mapper/UserMapper.xml文件,在其中添加findUsersByAge查询语句:
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="findUsersByAge" resultType="com.example.domain.User">
SELECT * FROM user WHERE age = #{age}
</select>
</mapper>
```
最后,在Service层调用这个Mapper:
```java
package com.example.service.impl;
import com.example.domain.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> findUsersByAge(Integer age) {
return userMapper.findUsersByAge(age);
}
}
```
完成以上步骤后,即可以通过访问接口来获取符合条件的用户数据了。比如,如果想查找年龄为18的用户,可以访问http://localhost:8080/users/18。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)