spring boot+mybatis配置的简化ssm
时间: 2023-08-10 21:02:55 浏览: 60
Spring Boot + MyBatis可以通过注解和自动配置来简化SSM框架的配置,具体步骤如下:
1. 引入依赖:在pom.xml文件中添加Spring Boot和MyBatis的依赖,例如:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
```
2. 配置数据源:在application.properties或application.yml文件中配置数据源,例如:
```
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置MyBatis:在启动类中添加@MapperScan注解,指定Mapper接口所在的包路径,例如:
```
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
4. 编写Mapper接口和Mapper XML文件:在Mapper接口中添加@Mapper注解,编写SQL语句,在Mapper XML文件中配置SQL语句和结果映射,例如:
```
@Mapper
public interface UserMapper {
@Select("select * from user")
List<User> findAll();
}
```
```
<select id="findAll" resultType="com.example.demo.pojo.User">
select * from user
</select>
```
通过以上配置,就可以实现Spring Boot + MyBatis的简化SSM框架。在以上配置中,Spring Boot提供了自动配置和默认配置,可以省去很多繁琐的配置工作,同时还可以提高开发效率和代码可读性。