如何同时兼任mybaits和mybaits
时间: 2023-05-27 09:06:04 浏览: 103
-plus
要同时使用MyBatis和MyBatis-Plus,需要进行以下步骤:
1. 在项目中引入MyBatis和MyBatis-Plus的依赖(可以使用Maven或Gradle进行依赖管理)。
2. 配置MyBatis的SqlSessionFactory和MapperScannerConfigurer。
```
@Configuration
public class MyBatisConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return new DruidDataSource();
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.example.model");
return sessionFactory;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("com.example.mapper");
return configurer;
}
}
```
3. 配置MyBatis-Plus的MapperScannerConfigurer和PaginationInterceptor。
```
@Configuration
public class MyBatisPlusConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
configurer.setBasePackage("com.example.mapper.plus");
return configurer;
}
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
```
4. 在Mapper接口中使用MyBatis和MyBatis-Plus的注解。
```
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE id = #{id}")
User findById(@Param("id") Long id);
@Select("SELECT * FROM user")
List<User> findAll();
@Insert("INSERT INTO user (name, age) VALUES (#{name}, #{age})")
@Options(useGeneratedKeys = true, keyProperty = "id")
int insert(User user);
@Update("UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id}")
int update(User user);
@Delete("DELETE FROM user WHERE id = #{id}")
int deleteById(@Param("id") Long id);
@Select("SELECT * FROM user WHERE name = #{name}")
User selectByName(@Param("name") String name);
List<User> selectPage(Page<User> page, @Param("name") String name);
}
```
5. 在Service层中使用MyBatis和MyBatis-Plus的Mapper接口。
```
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
@Autowired
private UserMapperPlus userMapperPlus;
public User findById(Long id) {
return userMapper.findById(id);
}
public List<User> findAll() {
return userMapper.findAll();
}
public int insert(User user) {
return userMapper.insert(user);
}
public int update(User user) {
return userMapper.update(user);
}
public int deleteById(Long id) {
return userMapper.deleteById(id);
}
public User selectByName(String name) {
return userMapper.selectByName(name);
}
public List<User> selectPage(Page<User> page, String name) {
return userMapperPlus.selectPage(page, name);
}
}
```
6. 在Controller层中调用Service层的方法。
```
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user/{id}")
public User findById(@PathVariable("id") Long id) {
return userService.findById(id);
}
@GetMapping("/user")
public List<User> findAll() {
return userService.findAll();
}
@PostMapping("/user")
public int insert(@RequestBody User user) {
return userService.insert(user);
}
@PutMapping("/user")
public int update(@RequestBody User user) {
return userService.update(user);
}
@DeleteMapping("/user/{id}")
public int deleteById(@PathVariable("id") Long id) {
return userService.deleteById(id);
}
@GetMapping("/user/name/{name}")
public User selectByName(@PathVariable("name") String name) {
return userService.selectByName(name);
}
@GetMapping("/user/page")
public List<User> selectPage(@RequestParam(value = "page", defaultValue = "1") int pageNum,
@RequestParam(value = "size", defaultValue = "10") int pageSize,
@RequestParam(value = "name", required = false) String name) {
Page<User> page = new Page<>(pageNum, pageSize);
return userService.selectPage(page, name);
}
}
```
这样就可以同时使用MyBatis和MyBatis-Plus了。在需要使用分页等高级功能时,可以使用MyBatis-Plus的Mapper接口和方法。在不需要高级功能时,可以使用MyBatis的注解和Mapper接口。
阅读全文