输出参数为简单类型、对象类型、HashMap及resultMap使用
项目结构
1.
CREATE TABLE empinfo (
eid INT (10) PRIMARY KEY,
name VARCHAR (20),
age INT(10),
sex VARCHAR(5)
);
2.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<typeAliases>
<!-- 单个定义别名 -->
<!-- <typeAlias type="org.lanqiao.entity.Empinfo" alias="empinfo"/> -->
<!-- 批量定义别名 -->
<package name="org.lanqiao.entity"/>
</typeAliases>
<!--通过 environments的 default值和environments的id值来指定MyBatis运行时的数据库环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<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>
<!--加载映射文件-->
<mapper resource="org/lanqiao/mapper/empinfoMapper.xml"/><!--修改的第二处-->
</mappers>
</configuration>
3.
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/company
username=root
password=root
4.
package org.lanqiao.entity;
public class Empinfo {
private int eid;
private String name;
private int age;
private String sex;
private String phone;
//无参构造
public Empinfo () {
}
//有参构造
public Empinfo (int eid, String name, int age, String sex, String phone) {
super();
this.eid = eid;
this.name = name;
this.age = age;
this.sex = sex;
this.phone = phone;
}
/**
* @return the eid
*/
public int getEid() {
return eid;
}
/**
* @param eid the eid to set
*/
public void setEid(int eid) {
this.eid = eid;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the sex
*/
public String getSex() {
return sex;
}
/**
* @param sex the sex to set
*/
public void setSex(String sex) {
this.sex = sex;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return eid+"-"+name+"-"+age+"-"+sex+"-"+phone ;
}
}
5.
<?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="org.lanqiao.mapper.EmpinfoMapper">
<!-- 查询Empinfo总数 -->
<select id="queryEmpinfoCount" resultType="int" >
select count(*) from empinfo
</select>
<!-- (1)按eid查询Empinfo-->
<select id="queryEmpinfoByeid" resultType="empinfo" parameterType="int" >
select * from empinfo where eid=${value}<!-- 但是eid也行,这是数据库不同的原因吗? -->
</select>
<!-- (4).查询所有Empinfo -->
<select id="queryAllEmpinfos" resultType="Empinfo">
select * from empinfo
</select>
<!-- 别名作为Map的key 1 -->
<select id="queryAllEmpinfoOutByHashMap" resultType="HashMap">
select eid "id",name "nm" from empinfo where eid=200812011
</select>
<!-- 别名作为Map的key 2-->
<select id="queryAllEmpinfosOutByHashMap" resultType="HashMap">
select eid "id",name "nm" from empinfo
</select>
</mapper>
6.
package org.lanqiao.mapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.lanqiao.entity.Empinfo;
public interface EmpinfoMapper {
//查询Empinfo总数
int queryEmpinfoCount();
Empinfo queryEmpinfoByeid(int eid);
List<Empinfo> queryAllEmpinfos();
//别名作为Map的key,1个学生的多个属性
HashMap<String,Object> queryAllEmpinfoOutByHashMap();
//别名作为Map的key,多个学生的多个属性
List<HashMap<String,Object>> queryAllEmpinfosOutByHashMap();
}
7.
package org.lanqiao.test;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.lanqiao.entity.Empinfo;
import org.lanqiao.mapper.EmpinfoMapper;
public class TestEmpinfo {
//查询Empinfo总数
public static void queryEmpinfoCount() throws IOException {
Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
int count=empinfoMapper.queryEmpinfoCount();
System.out.println(count);
session.close();
}
//(1)按eid查询Empinfo
public static void queryEmpinfoByeid() throws IOException {
Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
Empinfo empinfo=empinfoMapper.queryEmpinfoByeid(20170519);
System.out.println(empinfo);
session.close();
}
//(4).查询所有Empinfo
public static void queryAllEmpinfos() throws IOException {
Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
//String statement = "org.lanqiao.entity.empinfoMapper."+"queryAllEmpinfos";
//List<Empinfo> empinfos = session.selectList(statement);
EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
List<Empinfo> empinfos=empinfoMapper.queryAllEmpinfos();
System.out.println(empinfos);
session.close();
}
//查询Empinfo,结果为Map类型
public static void queryAllEmpinfoOutByHashMap() throws IOException {
Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
HashMap<String,Object> empinfoMap=empinfoMapper.queryAllEmpinfoOutByHashMap();
System.out.println(empinfoMap);
session.close();
}
//查询Empinfo,结果为Map类型
public static void queryAllEmpinfosOutByHashMap() throws IOException {
Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();
EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
List<HashMap<String,Object>> empinfoMap=empinfoMapper.queryAllEmpinfosOutByHashMap();
System.out.println(empinfoMap);
session.close();
}
public static void main(String[] args) throws IOException {
//queryEmpinfoCount();//查询Empinfo总数
//queryEmpinfoByeid();
//queryAllEmpinfos();
//queryAllEmpinfoOutByHashMap();
queryAllEmpinfosOutByHashMap();
}
}