mybatis多对一处理两种处理方式
1.Teacher.java
public class Teacher {private int id;private String name;//省略getter、setter及toString方法
}
2.Student.java
public class Student {private int id;private String name;private int age;private int tid;//多个学生对应一个老师private Teacher teacher;
3.StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.manytoone.entity.StudentMapper"><!-- 多对一的处理1.按查询结果嵌套处理2.按查询嵌套--><!-- 1.按查询结果嵌套处理 --><select id="selectStudent" resultMap="studentTeacher">select s.id,s.name,s.age,s.tid,t.id tids,t.name tname from student s,teacher t where s.tid=t.id</select><resultMap type="Student" id="studentTeacher"><id column="id" property="id"/><result column="name" property="name"/><result column="age" property="age"/><result column="tid" property="tid"/><!-- 关联对象property在实体类student中 的属性--><association property="teacher" javaType="Teacher"><id column="tids" property="id"/><result column="tname" property="name"/></association></resultMap><!-- 2.按查询结果处理 --><select id="getStudents" resultMap="studentMapper">select * from student</select><resultMap type="Student" id="studentMapper"><association property="teacher" column="tid" javaType="Teacher"select="selectTeacher"></association></resultMap><select id="selectTeacher" resultType="com.manytoone.entity.Teacher">select * from teacher where id=#{id}</select>
</mapper>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 加载数据库配置文件--><properties resource="sql.properties"></properties><typeAliases><!-- <typeAlias type="com.mybatis.entity.Pet" alias="Pet"/> --><!-- 默认别名是对应的类名 --><package name="com.mybatis.entity"/><package name="com.manytoone.entity"></package></typeAliases><!--environments 指mybatis可以配置多个环境默认,default只想默认的环境每个sqlSessionFactory对应一个environment--><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- UNPOOLED访问数据库时每次都会打开关闭连接POOLED数据库池减少了每次初始化、创建连接、和请求验证的时间 --><dataSource type="POOLED"><property name="driver" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${username}" /><property name="password" value="${password}" /></dataSource></environment></environments><mappers><!-- 定义SQL语句的映射文件 --><mapper resource="com/mybatis/entity/PetMapper.xml"/><mapper resource="com/manytoone/entity/StudentMapper.xml"/><mapper resource="com/manytoone/entity/TeacherMapper.xml"/><mapper class="com.annotation.dao.PetDao"/></mappers>
</configuration>
StudentDao.java
package com.manytoone.dao;import java.io.IOException;
import java.util.List;import org.apache.ibatis.session.SqlSession;import com.manytoone.entity.Student;
import com.mybatis.util.MyBatisUtil;public class StudentDao {public List<Student> selectAll() throws IOException{SqlSession sqlSession=MyBatisUtil.getSqlSession();List<Student> students=sqlSession.selectList("com.manytoone.entity.StudentMapper.selectStudent");return students;}public List<Student> getAll() throws IOException{SqlSession sqlSession=MyBatisUtil.getSqlSession();List<Student> students=sqlSession.selectList("com.manytoone.entity.StudentMapper.getStudents");return students;}
}
Test.java
package com.manytoone.test;import java.io.IOException;
import java.util.List;import com.manytoone.dao.StudentDao;
import com.manytoone.entity.Student;public class Test {public List<Student> selectAll() throws IOException{StudentDao studentDao=new StudentDao();List<Student> students=studentDao.selectAll();return students;}public List<Student> getAll() throws IOException{StudentDao studentDao=new StudentDao();List<Student> students=studentDao.selectAll();return students;}public static void main(String[] args) throws IOException {Test test=new Test();List<Student> students=test.getAll();for(Student stu:students) {System.out.println(stu);}}
}
mybatis多对一处理两种处理方式相关推荐
- mybatis批量更新的两种实现方式
mapper.xml文件,后台传入一个对象集合,另外如果是mysql数据库,一点在配置文件上加上&allowMultiQueries=true,这样才可以执行多条sql,以下为mysql: & ...
- mybatis 级联查询兑现_MyBatis之自查询使用递归实现 N级联动效果(两种实现方式)...
A:首先先看下一个简单的面试题 斐波那契数列 计算数组{1,1,2,3,5,8.......} 第30位值 规律:1 1 从第三项开始,每一项都是前两项之和 有两种实现方式 第一种方式: public ...
- MyBatis获取参数值的两种方式以及传参情况
MyBatis获取参数值的两种方式 MyBatis获取参数值的两种方式:${}和#{} 传参情况 演示环境 1.单个字面量类型的参数 2.多个字面量类型的参数 3.map集合类型的参数 4.实体类类型 ...
- SSM整合之Mybatis笔记( MyBatis获取参数值的两种方式(重要))(P020—P025)
P20 MyBatis获取参数值的两种方式(重要) 一.什么叫做获取取参数值的两种方式 以后在我们开发是方向B/S系统,然后我们现在要做的任务通过浏览器/页面/视图 收集用户输入的一些数据,然后把这些 ...
- mybatis 一对多 两种查询方式
注意事项: 如果是多表 例如有A表,B表 A是多端,B是一端 那么就要在B端声明A端的集合 必须 上代码: 我使用的是java语言,先看实体类 这是单表的一对多 多表跟单表逻辑是一致 public ...
- Web框架之Django_07 进阶操作(MTV与MVC、多对多表三种创建方式、前后端传输数据编码格式contentType、ajax、自定义分页器)
阅读目录 一.MVC与MTV 二.多对多表的创建 三.ajax,前后端传输编码格式contentType 四.批量插入数据与自定义分页器 摘要 MTV与MVC 多对多表三种创建方式 ajax ,前后端 ...
- C++中的两种绑定方式(静态绑定、动态绑定)
两种绑定方式 静态绑定:在编译时刻,根据指针或引用变量的静态类型来决定成员函数属于哪一个类. 动态绑定:在运行时刻,根据指针或引用变量实际指向或引用的对象类型(动态类型)来确定成员函数属于哪一个类. ...
- (C++)string 的两种输入方式和输出方式
注:头文件如下 #include<string> #include<cstdio> #include<iostream>using namespace std; 注 ...
- 三十张图片让你彻底弄明白图的两种遍历方式:DFS和BFS
1 引言 遍历是指从某个节点出发,按照一定的的搜索路线,依次访问对数据结构中的全部节点,且每个节点仅访问一次.图的遍历.遍历过程中得到的顶点序列称为图遍历序列. 2 深度优先搜索 2.1 算法思想 ...
最新文章
- Golang 301永久重定向
- 字符串匹配的sunday算法
- Windows 10全新界面要来了:焕然一新!
- EasyUI(前端框架)
- python各种包安装顺序_史上最全的Python包管理工具:Anaconda教程
- leaflet加载离线OSM(OpenStreetMap)
- java基础—几种for循环编程思想
- 金融科技的别样财报:趣店坎坷后回暖、品钛首盈利
- Android模拟器SDL_app:emulator.exe 解决方法
- js得到自定义属性和操作table表格
- oracle里面asm的作用,深入了解Oracle ASM(一):基础概念
- android+模拟示波器,模拟示波器
- 动软出现“添加服务器配置失败,请检查是否有写入权限或文件是否存在“错误
- HttpClient4
- C# 在 Excel 中创建组合图表
- springmvc GET POST 接受参数方式
- NSSCTF部分复现
- svm算法 最通俗易懂讲解
- android10系统是平板电脑吗,买平板电脑应该选win10还是安卓系统?
- 【行业交流】后勤管理信息化的需求
热门文章
- C#中的DataSet添加DataTable问题
- 腾讯2013年校园招聘笔试试题
- mvc4站点支持.html
- MSSQLSERVER数据库- 递归查询例子
- python怎么创建方法_python中的__new__方法
- linux批量管理服务,通过PSSH批量管理Linux服务器
- vla点转为lisp点_专家们说,中国股市将进入牛市,大盘可能超12000点是真吗?
- capsule 安装_CAPSULE SERVANT安卓版下载-CAPSULE SERVANT手游app安装-菜鸟下载
- python中的列表是指针吗_Python中的指针——到底指什么(二)
- php实例类,php实例-对象与类