mybatis-plus如何连接数据库
时间: 2023-11-28 20:43:49 浏览: 166
MyBatis-Plus连接数据库需要进行以下几个步骤:
1.在pom.xml文件中添加MyBatis-Plus的依赖,例如:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2.在application.properties或application.yml文件中配置数据库连接信息,例如:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
```
3.在MyBatis的Mapper接口中使用注解或XML配置SQL语句,例如:
```java
@Mapper
public interface UserMapper extends BaseMapper<User> {
@Select("select * from user where name = #{name}")
User findByName(@Param("name") String name);
}
```
4.在Service层中调用Mapper接口中的方法,例如:
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User findByName(String name) {
return userMapper.findByName(name);
}
}
```
阅读全文