MyBatis使用Dao实现SQL语句

it2024-01-21  67

先配置pom.xml文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>webapp7mybatis</groupId> <artifactId>webapp7mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>webapp7mybatis</name> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </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/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> <include>**/*.jsp</include> </includes> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.tld</include> </includes> <filtering>true</filtering> </resource> </resources> </build> </project> 在pom.xml中配置完成之后,创建一个对应着数据库表的实体类. package com.pojo; public class Emp { private int id; private String name; private String job; private int mgrId; private Double salary; private int deptId; private String hireate; private long longtime; private Dept dept; public Emp(int id, String name, String job, int mgrId, Double salary, int deptId, String hireate, long longtime, Dept dept) { this.id = id; this.name = name; this.job = job; this.mgrId = mgrId; this.salary = salary; this.deptId = deptId; this.hireate = hireate; this.longtime = longtime; this.dept = dept; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + '\'' + ", job='" + job + '\'' + ", mgrId=" + mgrId + ", salary=" + salary + ", deptId=" + deptId + ", hireate='" + hireate + '\'' + ", longtime=" + longtime + '}'; } public Emp() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public int getMgrId() { return mgrId; } public void setMgrId(int mgrId) { this.mgrId = mgrId; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public int getDeptId() { return deptId; } public void setDeptId(int deptId) { this.deptId = deptId; } public String getHireate() { return hireate; } public void setHireate(String hireate) { this.hireate = hireate; } public long getLongtime() { return longtime; } public void setLongtime(long longtime) { this.longtime = longtime; } } 自定义一个接口 package Mapper; import com.pojo.Emp; import java.util.HashMap; import java.util.List; public interface EmpInter { List<Emp> listEmps(); //查询员工姓名和薪资 public List<HashMap<String ,String>> listEmpsNS(); //根据部门查询员工 List<HashMap<String ,String>> listEmpBu(); //查询员工和部门 List<Emp> listEmpanddept(); } 在与接口统计的目录下创建一个.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="Mapper.EmpInter"> <resultMap id="listEmpsNS" type="hashmap"> <result column="name" property="name"></result> <result column="salary" property="sal"></result> </resultMap> <resultMap id="resultEmpBu" type="hashmap"> <result column="name" property="name"></result> <result column="salary" property="sal"></result> <result column="bumen" property="bumen"></result> </resultMap> <resultMap id="listEmpanddept" type="emp"> <result column="name" property="name"></result> <result column="salary" property="salary"></result> <result column="bumen" property="dept.bumen"></result> </resultMap> <select id="listEmps" resultType="emp"> select id, name, job, mgrId, salary, deptId, hireate, longtime from staff </select> <select id="listEmpsNS" resultMap="listEmpsNS"> select name, salary from staff </select> <select id="listEmpBu" resultMap="resultEmpBu"> SELECT s.name,s.salary,b.bumen FROM staff s,bumen b where s.deptId = b.detpId </select> <select id="listEmpanddept" resultMap="listEmpanddept"> SELECT s.name,s.salary,b.bumen FROM staff s,bumen b where s.deptId = b.detpId </select> </mapper>

同时在 中配置好文件.<mapper resource="Mapper/EmpMapper.xml"/>

创建测试类 在测试类中运行接口中的方法 package test; import Mapper.EmpInter; import com.pojo.Emp; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.io.Reader; import java.util.HashMap; import java.util.List; /** * FileNameEmpTest * <p> * Author: 歪歪 * <p> * Date: 2020/10/20 */ public class EmpTest { SqlSession session = null; @Before public void test0()throws IOException { String conf = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(conf); //创建SessionFactory对象 SqlSessionFactoryBuilder sfb =new SqlSessionFactoryBuilder(); SqlSessionFactory sf = sfb.build(reader); //查创建session session =sf.openSession(); } @Test public void test1() throws IOException { EmpInter emp = session.getMapper(EmpInter.class); List<Emp> emps = emp.listEmps(); for (Emp empss : emps) { System.out.println(empss); } } @Test public void test2() throws IOException { EmpInter emp = session.getMapper(EmpInter.class); List<HashMap<String,String>> hashMap = emp.listEmpsNS(); for(int i=0; i<hashMap.size(); i++){ HashMap map =hashMap.get(i); String name=(String) map.get("name"); double salary = (Double)map.get("salary"); System.out.println(name+"\t"+salary); } } @Test public void test3() throws IOException { EmpInter emp = session.getMapper(EmpInter.class); List<HashMap<String,String>> hashMap = emp.listEmpBu(); for(int i=0; i<hashMap.size(); i++){ HashMap map =hashMap.get(i); String name=(String) map.get("name"); double salary = (Double)map.get("sal"); String dept = (String) map.get("bumen"); System.out.println(name+"\t"+salary+"\t"+dept); } } @Test public void test4() throws IOException { EmpInter mapper = session.getMapper(EmpInter.class); List<Emp> emps = mapper.listEmpanddept(); for (int i=0;i<emps.size(); i++){ Emp emp = emps.get(i); System.out.println(emp.getName()+"\t"+"部门"+emp.getDept().getBumen()); } } }

在EmpMapper.xml中一定要注意 namespace中的地址一定要写对.

这里的id与接口中方法名要一致. resultMap 中的属性要与resultMap中的id相同

最新回复(0)