springboot添加mybatis框架
时间: 2023-11-16 11:54:50 浏览: 75
springboot+mybatis框架
好的,下面是添加MyBatis框架的步骤:
1. 在pom.xml文件中添加MyBatis和MyBatis-Spring的依赖:
```xml
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
```
2. 在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
3. 创建Mapper接口和对应的Mapper.xml文件,例如:
```java
public interface UserMapper {
User selectUserById(Integer id);
}
```
```xml
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="selectUserById" resultType="com.example.demo.entity.User">
select * from user where id = #{id}
</select>
</mapper>
```
4. 在启动类上添加@MapperScan注解,指定Mapper接口所在的包路径:
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
以上就是添加MyBatis框架的基本步骤,你可以根据自己的需求进行配置和使用。
阅读全文