以下是将 Spring Boot 集成 MyBatis 的方法:
1.添加 MyBatis 和 MyBatis-Spring 依赖:
1 2 3 4 5 6 7 8 9 10 11 <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > {mybatis-version}</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > {mybatis-version}</version > </dependency >
2.配置数据源,例如使用 HikariCP 数据库连接池:
1 2 3 4 5 6 7 8 9 10 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root type: com.zaxxer.hikari.HikariDataSource hikari: maximum-pool-size: 10 minimum-idle: 5
3.配置 MyBatis:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Configuration @MapperScan(basePackages = {"com.example.mapper"}) public class MyBatisConfig { @Autowired private DataSource dataSource; @Bean public SqlSessionFactory sqlSessionFactory () throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean (); sessionFactory.setDataSource(dataSource); return sessionFactory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate () throws Exception { SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate (sqlSessionFactory()); return sqlSessionTemplate; } }
4.创建 Mapper 接口和 XML 文件,在 XML 文件中编写 SQL 语句,例如:
1 2 3 4 5 6 <mapper namespace ="com.example.mapper.UserMapper" > <select id ="getUserById" resultType ="com.example.entity.User" > SELECT * FROM user WHERE id = #{id} </select > </mapper >
1 2 3 4 5 @Mapper public interface UserMapper { User getUserById (int id) ; }
5.在 Service 或 Controller 中使用 Mapper 接口,例如:
1 2 3 4 5 6 7 8 9 10 11 @RestController @RequestMapping("/users") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/{id}") public User getUserById (@PathVariable int id) { return userMapper.getUserById(id); } }
这样,在 Spring Boot 应用中就可以集成 MyBatis 并使用它来操作数据库了。
阅读剩下更多