本篇衔接以下链接 https://blog.csdn.net/H972002/article/details/159616919?spm1001.2014.3001.5501一、复杂情况查询大概会有三种情况一对一一个用户对应一个身份信息描述一对多一个用户借了多本图书多对一多个学生属于一个班级多对多多个班级对应多个学生(本篇不叙述因为博主没写例子)二、使用步骤1.一对一用户表用户信息表每一个用户都会对应一个具体的信息描述两张表通过id相等就合成一张表在控制台效果就是User对象里包含了用户和用户信息的具体描述。这里是将用户表映射到java中的User类用户信息表映射到java中的Detail类再把Detai对象作为User类中的一个属性。booksnull不用管上面一些 符号会在补充说明中提到先创建一个Detail类Data public class Detail { private int id; private String hobby; private String description; }User类中增加Detail属性private Detail detail;maperTest.xml中写入sql语句(有嵌套写法和关联写法两种)嵌套写法左连接通过user 表的id和detail表的id相等将两张表连接起来查询语句用select标签嵌套语句用resultMap标签select标签中idselectUserById表示接口方法selectUserByIdresultMaptest表示找到id属性是test的resultMap标签这里被嵌套的表detail用association标签id标签主要用于主键字段result标签用于普通字段property属性表示java类中的属性名column属性表示数据库中该表的字段名注意association标签中propertydetail表示User类中包含了一个detail属性名javaTypeDetail表示是该包含了detail属性的类型columnid表示是通过id字段关联的select idselectUserById parameterTypeint resultMaptest select * from user left join detail on user.id detail.id where user.id #{id} /select resultMap idtest typeUser id propertyid columnid/ result propertyname columnname/ result propertyage columnage/ association propertydetail columnid javaTypeDetail id propertyid columnid/ result propertyhobby columnhobby/ result propertydescription columndescription/ /association /resultMap关联写法通两个查询语句将两张表关联起来selectselectDetailById表示找到id属性是selectDetailById的select标签其它意思都一样select idselectUserById resultMaptest select * from user where id #{id} /select resultMap idtest typeUser id propertyid columnid/ result propertyname columnname/ result propertyage columnage/ association propertydetail columnid selectselectDetailById/ /resultMap select idselectDetailById resultTypeDetail select * from detail where id #{id} /select接口添加测试方法public interface MapperTest { User selectUserById(int id); }测试类实现接口方法public class Test { public static void main(String[] args) throws IOException { InputStream inputStream Resources.getResourceAsStream(mybatis-config.xml); SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession sqlSessionFactory.openSession(true); MapperTest mapper sqlSession.getMapper(MapperTest.class);User user01 mapper.selectUserById(1); System.out.println(user01);} }2.一对多用户表图书表id是1的用户借了两本书两张表的连接就是控制台效果就是User类中除了有自己的属性外还包含了图书信息图书信息是一个数组里面是一个个Book对象先创建一个Book类Data public class Book { private int b_id; private String bookName; }User类中增加List属性注意因为有多个book对象所以是List列表类型private ListBook books;maperTest.xml中写入sql语句(有嵌套写法和关联写法两种)两种写法和上面类似这里就用嵌套写法了一对多标签需要用collection注意book表里面的id不要设置成主键同时id字段用result标签(否则Mybatis会当成主键查询这样结果只会显示查到的第一个)一定要区别user表和book表的id(设置别名)否则Mybatis不知把查询到的id值对应给谁select idselectUserById2 resultMaptest2 SELECT u.id, u.name, u.age, b.id as b_id, b.bookName FROM user u LEFT JOIN book b ON u.id b.id WHERE u.id #{id} /select resultMap idtest2 typeUser id propertyid columnid/ result propertyname columnname/ result propertyage columnage/ collection propertybooks columnid ofTypeBook result propertyb_id columnb_id/ result propertybookName columnbookName/ /collection /resultMap接口添加测试方法public interface MapperTest { User selectUserById2(int id); }测试类实现接口方法public class Test { public static void main(String[] args) throws IOException { InputStream inputStream Resources.getResourceAsStream(mybatis-config.xml); SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession sqlSessionFactory.openSession(true); MapperTest mapper sqlSession.getMapper(MapperTest.class); User user1 mapper.selectUserById2(1); System.out.println(user1); } }3.多对一学生表班级表多名学生对应某个班级两张表连接就是控制台效果就是这里筛选出class_id是1的全部学生有两名学生属于class_id等于1添加Student类和Class类添加接口新创建一个Mapper并写入sql语句注意这里我的数据库中学生表和班级表有字段id重名需要将班级id起别名原因上面解释了多对一需要用association标签(多对多也是一样)?xml version1.0 encodingUTF-8 ? !DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespaceorg.example.MapperTestStudent select idselectStudentAndClassById resultMaptest3 SELECT student.*, class.id as cid FROM student LEFT JOIN class ON student.class_id class.id WHERE student.class_id #{class_id}; /select resultMap idtest3 typeStudent id propertyid columnid/ result propertyname columnname/ result propertyclass_id columnclass_id/ association propertyclass_ columnclass_id javaTypeClass result propertyclass_id columncid/ /association /resultMap /mapper这里namespaceorg.example.MapperTestStudent来绑定上面创建好的接口添加扫描路径在mybatis-config.xml文件中的mappers标签里添加如下一段mapper resourceMapper/MapperTestStudent.xml/这一段是新创建一个Mapper的路径让Mybatis通过扫描知道新创建的映射器的位置在哪这样就能执行具体sql语句了测试类创建一个新的会话并实现接口方法重新创建了sqlSession2会话这是一个新的需求推荐重新创建一个会话否则可能因为原会话缓存出现某些错误(博主还没学完个人观点)然后引发太阳膨胀全球灾变因此人类启动流浪地球消灭三体文明计划...............补充控制台打印的这个信息是日志可以辅助看到Mybatis查询的信息以及返回到的结果 查询的信息 返回的结果只需要在mybatis-config.xml中configuration标签添加configuration settings setting namelogImpl valueSTDOUT_LOGGING/ /settings/configuration
从零开始的Web全景开发:Marzipano全景引擎完全指南 【免费下载链接】marzipano A 360 media viewer for the modern web. 项目地址: https://gitcode.com/gh_mirrors/ma/marzipano
1. 为什么Marzipano能重新定义Web全景体验?
在VR技术蓬勃发展的…
终极指南:如何将JSXBIN二进制文件转换为可读JSX源代码 【免费下载链接】jsxbin-to-jsx-converter JSXBin to JSX Converter written in C# 项目地址: https://gitcode.com/gh_mirrors/js/jsxbin-to-jsx-converter
你是否曾经面对过Adobe产品的JSXBIN文件感到…