springboot整合mybatis实现简单的一对多级联查询功能(注解版)&Date与String的转换
resultType 对应简单类型,也就是实体中的属性名称与数据库表字段名称一模一样; resultMap 对应复杂类型,属性名称与字段名称不一样可以通过resultMap中property,column进行映射,其中一对一与一对多都是用resultMap来映射
今天介绍标题
目录
4.4Mon Dec 09 00:00:00 GMT+08:00 2019====》 2019-12-09 00:00:00
一,注解版与xml版介绍
@Select("select * from t_order where id = #{id}")
@Results(
{@Result(column = "id",property = "id"),
@Result(column = "id",property = "member",
many= @ Many(
select = "com.apesource.ressetmealservice.mapper.MemberDao.findmemberbyid"
))
})
public List<Order> findorderandsetmeal(int id);
两表(member与Order)之间的关系:一个会员(member)有多个订单,一个订单只能有一个会员。
第一个Result可以看成是Order实体类里面的id属性,第二个Result可以看成是xml版本collection。
<resultMap type="me.gacl.domain.Classes" id="ClassResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="me.gacl.domain.Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association><!-- ofType指定students集合中的对象类型 -->
<collection property="students" ofType="me.gacl.domain.Student">
<id property="id" column="s_id"/>
<result property="name" column="s_name"/>
</collection>
</resultMap>
resultType 对应简单类型,也就是实体中的属性名称与数据库表字段名称一模一样;
resultMap 对应复杂类型,属性名称与字段名称不一样可以通过resultMap中 property,column进行映射,其中一对一与一对多都是用resultMap来映射
many对应会员对象List,property中的内容为Order实体类的属性对象member。column为会员表里面的列id。
第一个Result里面的property可以看成是order表的id属性。
二,介绍此次例子引用到的方法
"com.apesource.ressetmealservice.mapper.MemberDao.findmemberbyid"
@Select("SELECT * FROM t_member WHERE id = #{memeberid}")
public Member findmemberbyid(int memeberid);
三,介绍实体类
private Integer id;
private Integer memberId;
private Date orderDate;
private String orderType;
private String orderStatus;
private Integer setmealId;
如果我写的不清晰,可以去脚本之家查看:
四,Date和String的爱恨情仇
4.1将date转String
sdf.format(date2);
4.2给Date换格式
Date date= new Date("4/12/2022");
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
String s = simpleDateFormat.format(date);
System.out.println(s);
输出结果:2022-04-12
4.3Date中常见的格式
4.4Mon Dec 09 00:00:00 GMT+08:00 2019====》 2019-12-09 00:00:00
Parse方法:Tue Jan 09 00:20:00 GMT+08:00 2029
转为yyyy-dd-mm这种形式要用下面的方法。
SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss z", Locale.ENGLISH);
SimpleDateFormat sf = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy", Locale.ENGLISH);
Date date = sf.parse("Mon Dec 09 00:00:00 GMT+08:00 2019");
System.out.println(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = sdf.format(date);
System.out.println(result);
Mon Dec 09 00:00:00 GMT+08:00 2019
2019-12-09 00:00:00
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
今天介绍标题



所有评论(0)