目录
2、select元素
5、联合查询
<groupId>com.mashibing</groupId>
<artifactId>mybatis_helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- <build>-->
<!-- <resources>-->
<!-- <resource>-->
<!-- <directory>src/main/java</directory>-->
<!-- <includes>-->
<!-- <include>**/*.xml</include>-->
<!-- </includes>-->
<!-- </resource>-->
<!-- </resources>-->
<!-- </build>-->
Emp.java
public class Emp {
private Integer empno; //员工编号
private String ename; //员工姓名
private String job; //职业
private Integer mgr; //上级编号
private Date hiredate; //入职日期
private Double sal; //工资
private Double comm; //商品
private Dept dept; //部门(点进去会跳转到Dept.java)
public Integer getEmpno() {return empno;}
public void setEmpno(Integer empno) {this.empno = empno;}
public String getEname() {return ename;}
public void setEname(String ename) {this.ename = ename;}
public String getJob() {return job;}
public void setJob(String job) {this.job = job;}
public Integer getMgr() {return mgr;}
public void setMgr(Integer mgr) {this.mgr = mgr;}
public Date getHiredate() {return hiredate;}
public void setHiredate(Date hiredate) {this.hiredate = hiredate;}
public Double getSal() {return sal;}
public void setSal(Double sal) {this.sal = sal;}
public Double getComm() {return comm;}
public void setComm(Double comm) {this.comm = comm;}
public Integer getDeptno() {return deptno;}
public void setDeptno(Integer deptno) {this.deptno = deptno;}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}
Dept.java(部门)
public class Dept {
private Integer deptno;//部门编号
private String dname; //部门名称
private String loc; //地方
public Integer getDeptno() {return deptno;}
public void setDeptno(Integer deptno) {this.deptno = deptno;}
public String getDname() {return dname;}
public void setDname(String dname) {this.dname = dname;}
public String getLoc() {return loc;}
public void setLoc(String loc) {this.loc = loc;}
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
", emps=" + emps +
'}';
}
}
EmpDao.java
public interface EmpDao {
public Emp selectEmpByEmpno(Integer empno);
}
EmpDao.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.mashibing.dao.EmpDao">
//将每一个属性值都映射成对象中的数据,如果有实体类对象,就写成对象.属性的方式
<resultMap结果映射 id="myEmp" type="com.mashibing.bean.Emp">
<id column="empno" property="empno"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="mgr" property="mgr"></result>
<result column="hiredate" property="hiredate"></result>
<result column="sal" property="sal"></result>
<result column="comm" property="comm"></result>
<result column="deptno" property="dept.deptno"></result>
<result column="dname" property="dept.dname"></result>
<result column="loc" property="dept.loc"></result>
</resultMap>
<select id="selectEmpByEmpno" resultMap结果映射="myEmp">
select * from emp where empno = #{empno}
</select>
</mapper>
MyTest.java
public class MyTest {
SqlSessionFactory sqlSessionFactory = null;
@Before
public void init(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test01() throws IOException {
//获取与数据库相关的会话
SqlSession sqlSession线程 = sqlSessionFactory.openSession();
//获取对应的映射接口对象
EmpDao mapper映射 = sqlSession.getMapper(EmpDao.class);
Emp emp = mapper.selectEmpByEmpno(7369);
System.out.println(emp);
//关闭会话
sqlSession.close();
}
}
//该集合中还存在null
EmpDao.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.mashibing.dao.EmpDao">
//将每一个属性值都映射成对象中的数据,如果有实体类对象,就写成对象.属性的方式
<resultMap结果映射 id="myEmp" type="com.mashibing.bean.Emp">
<id column="empno" property="empno"></id>
<result column="ename" property="ename"></result>
<result column="job" property="job"></result>
<result column="mgr" property="mgr"></result>
<result column="hiredate" property="hiredate"></result>
<result column="sal" property="sal"></result>
<result column="comm" property="comm"></result>
<result column="deptno" property="dept.deptno"></result>
<result column="dname" property="dept.dname"></result>
<result column="loc" property="dept.loc"></result>
</resultMap>
<select id="selectEmpByEmpno" resultMap结果映射="myEmp">
========