MyBatis-Ext快速入门实战攻略
MyBatis-Ext是MyBatis的一个增强工具包,可以提供更加便捷的使用方式,提高开发效率。本文将提供一个MyBatis-Ext的快速入门实战攻略,包含配置、实现及示例演示。
配置
- 引入MyBatis-Ext的依赖
xml
<dependency>
<groupId>com.github.mybatis-ext</groupId>
<artifactId>mybatis-ext-core</artifactId>
<version>${mybatis-ext.version}</version>
</dependency>
- 在
mybatis-config.xml
中添加MyBatis-Ext插件
xml
<configuration>
<plugins>
<plugin interceptor="org.mybatis.ext.interceptor.PaginationInterceptor" />
</plugins>
</configuration>
实现
- 编写Mapper接口
```java
public interface UserMapper {
/**
* 根据用户ID查询用户
* @param id 用户ID
* @return User对象
*/
User getUserById(Long id);
/**
* 分页查询所有用户
* @param pageNum 第几页
* @param pageSize 每页大小
* @return 用户列表
*/
List<User> listAllUsers(Integer pageNum, Integer pageSize);
}
```
- 编写Mapper XML
```xml
<select id="getUserById" parameterType="long" resultMap="userMap">
select * from users where id = #{id}
</select>
<select id="listAllUsers" resultMap="userMap">
select * from users
</select>
- 使用MyBatis-Ext进行分页插件配置
```java
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
return pageHelper;
}
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
return paginationInterceptor;
}
/**
* 在这里创建 SqlSessionFactory
* @return
*/
@Bean
public SqlSessionFactoryBean getSqlSessionFactoryBean(){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setTypeAliasesPackage("com.sample.model");
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:mapper/*.xml"));
sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper(), paginationInterceptor()});
} catch (Exception e) {
e.printStackTrace();
}
return sqlSessionFactoryBean;
}
```
示例
示例一:根据用户ID查询用户
- 编写Controller
```java
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
public User getUserById(@PathVariable Long id) {
return userMapper.getUserById(id);
}
}
```
- 发起请求
请求示例:http://localhost:8080/users/1
返回结果:
json
{
"id": 1,
"name": "张三",
"age": 20,
"gender": "男",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
}
示例二:分页查询所有用户
- 编写Controller
```java
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<User> listAllUsers(@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
return userMapper.listAllUsers(pageNum, pageSize);
}
}
```
- 发起请求
请求示例:http://localhost:8080/users?pageNum=2&pageSize=5
返回结果:
json
[
{
"id": 6,
"name": "赵六",
"age": 18,
"gender": "女",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
},
{
"id": 7,
"name": "钱七",
"age": 22,
"gender": "男",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
},
{
"id": 8,
"name": "孙八",
"age": 25,
"gender": "女",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
},
{
"id": 9,
"name": "吴九",
"age": 30,
"gender": "男",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
},
{
"id": 10,
"name": "郑十",
"age": 21,
"gender": "女",
"createTime": "2021-08-18 10:28:32",
"updateTime": "2021-08-18 10:28:32"
}
]
至此,我们完成了一个通过MyBatis-Ext进行快速入门的示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:MyBatis-Ext快速入门实战 - Python技术站