17. Mybatis 查询操作-select

1. select 标签

select 标签是用于定义插入语句的, select 标签是Mybatis 中最复杂的标签了.

1.1 常用属性

select 的属性和子标签比较多, 常用属性有:

  • id: sql 片段在命名空间内的唯一标识. 和mapper 中方法名保持一致
  • useGeneratedKeys: 是否回填自动生成的主键
  • keyProperty: 主键回填到哪个属性
  • keyColumn: 主键回填的字段名, 可省略
  • parameterType: 参数类型, 通常都可以省略.
  • flushCache: 是否刷新(清空)一级缓存和二级缓存, 默认为false.
  • useCache: 是否将查询结果存入二级缓存, 默认为true. 对一级缓存无效.
  • timeout: sql 执行超时时间, 默认未设置, 由数据库驱动决定.
  • resultType: 指定结果集中每一行的封装类型, 支持自动封装为java中定义的类对象. 默认使用类的全限定名称, 配置别名后, 支持类的简单名称.
  • resultMap: 指定结果集中每一行的封装类型, 是使用resultMap 标签定义的类型.
  • statementType: 执行sql时使用的statement类型, 默认为PREPARED. 可选值为:STATEMENT,PREPARED 或 CALLABLE 的一个
<insert
  id="insertAuthor"
  parameterType="domain.blog.Author"
  flushCache="true"
  statementType="PREPARED"
  keyProperty=""
  keyColumn=""
  useGeneratedKeys=""
  timeout="20">
  
</insert>

1.2 返回值

Mybatis 的查询结果集封装着实很强大, 同一个sql片段, 会根据接口方法定义时返回值的不同, 而做自动封装. 笔者介绍一下常见的几种返回类型.

返回值类型 返回描述
String/Integer/Float… 返回单个的基本类型, Mybatis为基本类型内置了别名, resultType 可直接写别名.
List/Set<String/Integer…> 返回基本类型的集合
VO 单个的java 类对象, 不局限于PO, VO, DTO 等均可
List/Set java 对象集合
Map<String, Object> 单个对象的字段名, 与值构成的Map
Map<Object, VO> 单个对象的某个属性和对象构成key=value 键值对的map, 如 Map<PK, PO>; 需要注意两点: 第一, 需要借助于@Mapper 注解指定key的字段; 第二, 如果PK类型指定为Long, 那么当id较小时, 便会返回Integer, 而报错.

2. Select 标签返回举例

2.1 Mapper 方法定义


public interface DepartmentMapper {
   

    // 返回单个基本类型
    String queryDepartmentName(Long id);

    // 返回基本类型集合
    List<Long> queryAllIds();

    // 返回单一对象
    DepartmentPO findById(Long id);

    // 返回对象集合
    List<DepartmentPO> queryAll();

    // 返回单个对象的所有属性
    Map<String , Object&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Spring Boot 是一个快速开发框架,MyBatisMyBatis-Plus 是两个流行的 ORM 框架,Spring Boot 与 MyBatis/MyBatis-Plus 的整合可以帮助我们更快更方便地进行开发。 下面是使用Java Spring Boot整合MyBatis/MyBatis-Plus的步骤: 1. 在 pom.xml 文件中添加 MyBatis/MyBatis-Plus 和 MySQL 驱动的依赖: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` 2. 在 application.properties/application.yml 文件中配置数据源和 MyBatis/MyBatis-Plus 的配置信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.type-aliases-package=com.example.demo.entity mybatis-plus.mapper-locations=classpath:/mapper/*.xml ``` 3. 在 Spring Boot 启动类上添加 `@MapperScan` 注解,指定 MyBatis/MyBatis-Plus 的 Mapper 所在的包: ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 创建实体类和 Mapper 接口: ```java public class User { private Long id; private String name; private Integer age; // 省略 getter 和 setter 方法 } @Mapper public interface UserMapper extends BaseMapper<User> { } ``` 5. 创建 Mapper 的 XML 文件(如果使用 MyBatis-Plus 可以省略此步骤): ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" /> <result column="name" property="name" /> <result column="age" property="age" /> </resultMap> <select id="selectById" resultMap="BaseResultMap"> select * from user where id = #{id} </select> </mapper> ``` 6. 在 Service 中使用 Mapper: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public User getUserById(Long id) { return userMapper.selectById(id); } } ``` 这样就完成了 Java Spring Boot 整合 MyBatis/MyBatis-Plus 的基本步骤。需要注意的是,在使用 MyBatis-Plus 的情况下,Mapper 接口无需自己编写 CRUD 操作的方法,直接继承 `BaseMapper` 接口即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值