Mybatis各种查询
实体Emp
private int empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private Double sal;
private Double comm;
private int deptno;
dao层:
public List<Emp> empList();
//多条件查询
public List<Emp> empslist(String ename, String job);
public void AddEmp(Emp emp);
public Emp ToupdateEmp(int id);
public void UpdateEmp(Emp emp);
public void DeliteEmp(int id);
//根据名字查询(因为名字不是唯一)
public List<Emp> findbyename(String ename);
//根据名字和工作查询(因为名字和工作在一起是唯一,用户密码)
//public Emp findbysome(String ename,String job);
public Emp findbysome(Map<String, Object> paMap);
//根据名字和工作单位和牌号查询
//public List<Emp> findbysome(String ename,String job,int mgr);
public List<Emp> findbysomesome(Emp emps);
//模糊查询
public List<Emp> likeEmps(String ename);
映射文件
<?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.xml 中的namespace 设置为 mapper.java 的全限定名。 -->
<!-- 该映射文件取代了dao的实现类
namespace是指要实现的接口的全限定名 -->
<mapper namespace="com.oak.dao.EmpDao">
<!-- 所有的查询使用select 插入insert 更新 update 删除 delete
id在当前映射文件中不允许重复,要和接口的方法名保持一致
resultType表示方法的返回类型,如果返回类型中有泛型,则只描述泛型即可-->
<select id="empList" resultType="com.oak.entity.Emp">
select * from emp
</select>
<insert id="AddEmp" parameterType="com.oak.entity.Emp">
<!-- 使用序列生成主键值
keyProperty="aid"表示主键对应的属性,
order="BEFORE"表示生成id序列值时机:before表示在insert前,after表示在insert后
resultType="int"表示序列值的类型
-->
<selectKey keyProperty="empno" order="BEFORE" resultType="int">
select seq_customer_id.nextval from dual
</selectKey>
<!-- #{参数}代替了占位符? -->
insert into emp values(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
</insert>
<select id="ToupdateEmp" resultType="com.oak.entity.Emp" parameterType="int">
select * from emp where empno = #{id}
</select>
<update id="UpdateEmp" parameterType="com.oak.entity.Emp">
update emp set job = #{job}<!-- ,ename = #{ename}, mgr = #{mgr},hiredate= #{hiredate},sal = #{sal}, comm = #{comm}, deptno = #{deptno} --> where empno = #{empno}
</update>
<delete id="DeliteEmp" parameterType="int">
delete from emp where empno = #{id}
</delete>
<!-- 根据名字查询(因为名字不是唯一) -->
<select id="findbyename" parameterType="string" resultType="com.oak.entity.Emp">
select * from emp where ename=#{adc}
</select>
<!-- //根据名字和工作查询(因为名字和工作在一起是唯一,用户密码) -->
<select id="findbysome" parameterType="java.util.Map" resultType="com.oak.entity.Emp">
select * from emp where ename = #{ename} and job = #{job}
</select>
<!-- //根据名字和工作单位和牌号查询 -->
<select id="findbysomesome" parameterType="com.oak.entity.Emp" resultType="com.oak.entity.Emp">
select * from emp where ename = #{ename} and job = #{job} and mgr = #{mgr}
</select>
<!-- 模糊查询 -->
<select id="likeEmps" parameterType="string" resultType="com.oak.entity.Emp">
select * from emp where ename like '%${value}%'
</select>
</mapper>
测试:
package com.oak.test;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.oak.dao.EmpDao;
import com.oak.entity.Emp;
public class Emp_Test {
private SqlSession session;
@Before
public void init(){
String resources = "mybatis.xml";
Reader reader;
try {
reader = Resources.getResourceAsReader(resources);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
session = sf.openSession();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void EmpList() {
/*EmpDao empDao = session.getMapper(EmpDao.class);
List<Emp> emps = empDao.empList();*/
List<Emp> emps = session.selectList("com.oak.dao.EmpDao.empList");
System.out.println(emps);
}
@Test
public void AddEmp(){
//EmpDao empDao = session.getMapper(EmpDao.class);
Emp emp = new Emp();
emp.setComm(7803.3);
emp.setDeptno(20);
emp.setEname("高东军");
emp.setHiredate(new Date());
emp.setJob("etyrg");
emp.setMgr(2000);
emp.setSal(3000.0);
//empDao.AddEmp(emp);
session.insert("com.oak.dao.EmpDao.AddEmp", emp);
session.commit();
}
@Test
public void UpdateEmp(){
//EmpDao empDao = session.getMapper(EmpDao.class);
//Emp emp = empDao.ToupdateEmp(68);
Emp emp = session.selectOne("com.oak.dao.EmpDao.ToupdateEmp", 69);
System.out.println(emp);
emp.setJob("请厕所");
session.update("com.oak.dao.EmpDao.UpdateEmp",emp);
//empDao.UpdateEmp(emp);
session.commit();
}
@Test
public void DaleteEmp(){
//EmpDao empDao = session.getMapper(EmpDao.class);
//empDao.DeliteEmp(68);
session.delete("com.oak.dao.EmpDao.DeliteEmp", 69);
session.commit();
}
//根据名字查询(因为名字不是唯一)
@Test
public void findbyename(){
EmpDao empDao = session.getMapper(EmpDao.class);
List<Emp> ename = empDao.findbyename("FORD");
System.out.println(ename);
}
//根据名字和工作查询(因为名字和工作在一起是唯一,用户密码)
@Test
public void findbysome() {
EmpDao empDao = session.getMapper(EmpDao.class);
Map<String, Object> paMap = new HashMap<String, Object>();
paMap.put("ename", "SCOTT");
paMap.put("job", "ANALYST");
Emp emp = empDao.findbysome(paMap);
System.out.println(emp);
}
@Test
public void findbysomesome(){
EmpDao empDao = session.getMapper(EmpDao.class);
Emp emp = new Emp();
emp.setEname("SCOTT");
emp.setJob("ANALYST");
emp.setMgr(7566);
List<Emp> emps = empDao.findbysomesome(emp);
System.out.println(emps);
}
//模糊查询
@Test
public void like(){
EmpDao empDao = session.getMapper(EmpDao.class);
List<Emp> emps = empDao.likeEmps("A");
System.out.println(emps);
}
@After
public void over(){
session.close();
}
}