对于SELECT记录MySQL:日期+1之间感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解mysql日期查询语句,并且为您提供关于Mybatisselect记录封装、Mybatissele
对于SELECT记录MySQL:日期+ 1之间感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解mysql日期查询语句,并且为您提供关于Mybatis select记录封装、Mybatis select记录封装的实现、MySQL SELECT WHERE日期时间匹配日期(不一定是时间)、mysql – 如果SELECT记录如果两个值之间的差值的绝对值大于一定数?的宝贵知识。
本文目录一览:- SELECT记录MySQL:日期+ 1之间(mysql日期查询语句)
- Mybatis select记录封装
- Mybatis select记录封装的实现
- MySQL SELECT WHERE日期时间匹配日期(不一定是时间)
- mysql – 如果SELECT记录如果两个值之间的差值的绝对值大于一定数?
SELECT记录MySQL:日期+ 1之间(mysql日期查询语句)
假设我要选择两个日期之间的所有记录,再加上该日期之前的一个记录和该日期之后的一个记录?所有记录均按日期排序。
答案1
(select * from t where date < start_date order by date desc limit 1) union (select * FROM t WHERE date between start_date and end_date) union (select * from t where date > end_date order by date asc limit 1)
Mybatis select记录封装
select记录封装
- 返回一个List集合, resultType要写集合中元素的类型
<!-- public List<Employee> getEmpsByLastNameLike(String lastName); -->
<!--resultType:如果返回的是一个集合,要写集合中元素的类型 -->
<select id="getEmpsByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select>
- 返回一条记录的map, key为列名, 值就是对应的值
<!--public Map<String, Object> getEmpByIdReturnMap(Integer id); -->
<select id="getEmpByIdReturnMap" resultType="map">
select * from tbl_employee where id=#{id}
</select>
- 多条记录封装成一个map, key为id, 值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key
@MapKey("lastName")
public Map<String, Employee> getEmpByLastNameLikeReturnMap(String lastName);
<!--public Map<Integer, Employee> getEmpByLastNameLikeReturnMap(String lastName); -->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select>
自动映射配置
- 全局setting设置
- autoMappingBehavior默认为PARTIAL, 开启自动映射功能;唯一的要求是列名和javaBean属性名一致
2.mapUnderscoreToCamelCase=true, 开启自动驼峰命名规范映射功能
- 自定义resultMap, 实现高级映射功能
resultMap自定义映射规则
<!--自定义某个javaBean的封装规则
type:自定义规则的Java类型
id:唯一id方便引用
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
<!--指定主键列的封装规则
id定义主键会底层有优化;
column:指定哪一列
property:指定对应的javaBean属性
-->
<id column="id" property="id"/>
<!-- 定义普通列封装规则 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
- 创建表
create table tb_dept (
id int(11) primary key auto_increment,
dept_name varchar(255)
)
添加列
alter table tb_emp add column d_id int(11);
添加约束
alter table tb_emp add constraint fk_emp_dept foreign key(d_id) references tb_dept(id);
联合查询:级联属性封装结果集
场景一:
查询Employee的同时查询员工对应的部门;一个员工有与之对应的部门信息;
<!--
场景一:
查询Employee的同时查询员工对应的部门
Employee===Department
一个员工有与之对应的部门信息;
id last_name gender d_id did dept_name (private Department dept;)
-->
<!--
联合查询:级联属性封装结果集
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
使用association定义关联的单个对象的封装规则;
<!--
使用association定义关联的单个对象的封装规则;
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDifEmp2">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<!-- association可以指定联合的javaBean对象
property="dept":指定哪个属性是联合的对象
javaType:指定这个属性对象的类型[不能省略]
-->
<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
</association>
</resultMap>
association分步查询
<!-- 使用association进行分步查询:
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、部门设置到员工中;
-->
<!-- id last_name email gender d_id -->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpByStep">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!-- association定义关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:指定将哪一列的值传给这个方法
流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性
-->
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</resultMap>
<!-- DepartmentMapper.xml -->
<mapper namespace="com.atguigu.mybatis.dao.DepartmentMapper">
<!--public Department getDeptById(Integer id); -->
<select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">
select id,dept_name departmentName from tbl_dept where id=#{id}
</select>
association分步查询&延迟加载
<!-- 可以使用延迟加载(懒加载);(按需加载)
Employee==>Dept:
我们每次查询Employee对象的时候,都将一起查询出来。
部门信息在我们使用的时候再去查询;
分段查询的基础之上加上两个配置:
-->
<!-- ==================association============================ -->
<!-- mybatis-config.xml-->
<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- value:false 表示按需加载; 否则会总是加载 -->
<setting name="aggressiveLazyLoading" value="false"/>
关联集合
嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则场景二:
查询部门的时候将部门对应的所有员工信息也查询出来:注释在DepartmentMapper.xml中
<!--
public class Department {
private Integer id;
private String departmentName;
private List<Employee> emps;
did dept_name || eid last_name email gender
-->
<!--嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDept">
<id column="did" property="id"/>
<result column="dept_name" property="departmentName"/>
<!--
collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型
-->
<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
<!-- 定义这个集合中元素的封装规则 -->
<id column="eid" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id); -->
<select id="getDeptByIdPlus" resultMap="MyDept">
SELECT d.id did,d.dept_name dept_name,
e.id eid,e.last_name last_name,e.email email,e.gender gender
FROM tbl_dept d
LEFT JOIN tbl_employee e
ON d.id=e.d_id
WHERE d.id=#{id}
</select>
collection:分段查询
<!-- collection:分段查询 -->
<resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep">
<id column="id" property="id"/>
<result column="dept_name" property="departmentName"/>
<collection property="emps"
select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpsByDeptId"
column="{deptId=id}" fetchType="lazy"></collection>
</resultMap>
<!-- 扩展:多列的值传递过去:
将多列的值封装map传递;
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载;
- lazy:延迟
- eager:立即
鉴别器
mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为
封装Employee:
如果查出的是女生:就把部门信息查询出来,否则不查询;
如果是男生,把last_name这一列的值赋值给email;
<!-- =======================鉴别器============================ -->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmpDis">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--
column:指定判定的列名
javaType:列值对应的java类型 -->
<discriminator javaType="string" column="gender">
<!--女生 resultType:指定封装的结果类型;不能缺少。/resultMap-->
<case value="0" resultType="com.atguigu.mybatis.bean.Employee">
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case>
<!--男生 ;如果是男生,把last_name这一列的值赋值给email; -->
<case value="1" resultType="com.atguigu.mybatis.bean.Employee">
<id column="id" property="id"/>
<result column="last_name" property="lastName"/>
<result column="last_name" property="email"/>
<result column="gender" property="gender"/>
</case>
</discriminator>
</resultMap>
Mybatis select记录封装的实现
这篇文章主要介绍了Mybatis select记录封装的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
select记录封装
返回一个List集合, resultType要写集合中元素的类型
select * from tbl_employee where last_name like #{lastName}
返回一条记录的map, key为列名, 值就是对应的值
select * from tbl_employee where id=#{id}
多条记录封装成一个map, key为id, 值是记录封装后的javaBean
//@MapKey:告诉mybatis封装这个map的时候使用哪个属性作为map的key @MapKey("lastName") public Map getEmpByLastNameLikeReturnMap(String lastName);
select * from tbl_employee where last_name like #{lastName}
自动映射配置
全局setting设置
1.autoMappingBehavior默认为PARTIAL, 开启自动映射功能;唯一的要求是列名和javaBean属性名一致
2.mapUnderscoreToCamelCase=true, 开启自动驼峰命名规范映射功能
自定义resultMap, 实现高级映射功能
resultMap自定义映射规则
创建表
create table tb_dept ( id int(11) primary key auto_increment, dept_name varchar(255) )
添加列
alter table tb_emp add column d_id int(11);
添加约束
alter table tb_emp add constraint fk_emp_dept foreign key(d_id) references tb_dept(id);
联合查询:级联属性封装结果集
场景一:
查询Employee的同时查询员工对应的部门;一个员工有与之对应的部门信息;
使用association定义关联的单个对象的封装规则;
association分步查询
select id,dept_name departmentName from tbl_dept where id=#{id}
association分步查询&延迟加载
Dept: 我们每次查询Employee对象的时候,都将一起查询出来。 部门信息在我们使用的时候再去查询; 分段查询的基础之上加上两个配置: -->
关联集合
嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则
场景二:
查询部门的时候将部门对应的所有员工信息也查询出来:注释在DepartmentMapper.xml中
SELECT d.id did,d.dept_name dept_name, e.id eid,e.last_name last_name,e.email email,e.gender gender FROM tbl_dept d LEFT JOIN tbl_employee e ON d.id=e.d_id WHERE d.id=#{id}
collection:分段查询
总结
以上是小编为你收集整理的Mybatis select记录封装的实现全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
MySQL SELECT WHERE日期时间匹配日期(不一定是时间)
如何解决MySQL SELECT WHERE日期时间匹配日期(不一定是时间)?
使用像这样的选择器DATE(datecolumns) = ''2012-12-24''
-它是性能杀手:
- 它将
DATE()
为所有不匹配的行(包括那些行)计算 - 它将无法使用索引进行查询
使用起来更快
SELECT * FROM tablename
WHERE columname BETWEEN ''2012-12-25 00:00:00'' AND ''2012-12-25 23:59:59''
因为这将允许索引使用而无需计算。
正如Used_By_Already所指出的那样,自2012年最初回答以来,出现了一些MysqL版本,在该版本中使用‘23:59:59’作为结束日不再安全。更新后的版本应为
SELECT * FROM tablename
WHERE columname >=''2012-12-25 00:00:00''
AND columname <''2012-12-26 00:00:00''
答案的要旨,即在计算出的表达式上避免选择器,当然仍然存在。
解决方法
我有一个包含datetime列的表。我希望无论何时都返回给定日期的所有记录。换句话说,如果我的表仅包含以下4条记录,那么如果我限制为2012-12-25,则仅返回第二和第三条记录。
2012-12-24 00:00:00
2012-12-25 00:00:00
2012-12-25 06:00:00
2012-12-26 05:00:00
mysql – 如果SELECT记录如果两个值之间的差值的绝对值大于一定数?
我有两个类型为int lap_time_1和lap_time_2的字段.是否有mysql查询来选择第1圈和第2圈之间的差异(绝对值)大于30的记录?
SELECT *
FROM YourTable
WHERE ABS(lap_time_1 - lap_time_2) > 30
关于SELECT记录MySQL:日期+ 1之间和mysql日期查询语句的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Mybatis select记录封装、Mybatis select记录封装的实现、MySQL SELECT WHERE日期时间匹配日期(不一定是时间)、mysql – 如果SELECT记录如果两个值之间的差值的绝对值大于一定数?等相关知识的信息别忘了在本站进行查找喔。
本文标签: