mybatis代码

Test

package com.neusoft.main;

import com.neusoft.domain.Dept;
import com.neusoft.domain.Emp;
import com.neusoft.mapper.DeptMapper;
import com.neusoft.mapper.EmpMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/21.
 */

public class Test {

    DeptMapper deptMapper = null;
    EmpMapper empMapper = null;
    SqlSession sqlSession = null;

    @org.junit.Before
    public void Create () throws IOException {

        String resource = "SqlMapConfig.xml";

        InputStream inputStream = Resources.getResourceAsStream(resource);

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        sqlSession = sqlSessionFactory.openSession();

        deptMapper = sqlSession.getMapper(DeptMapper.class);

        empMapper = sqlSession.getMapper(EmpMapper.class);

    }

    @org.junit.Test
    public void getDeptById() {

        Dept dept = deptMapper.getDeptById(10);

        System.out.println(dept);
    }
    @org.junit.Test
    public void getAllDepts() {

        List<Dept> deptList=deptMapper.getAllDepts();

        System.out.println(deptList);
    }
    @org.junit.Test
    public void deleteDeptById() {
        deptMapper.deletDept(40);

        sqlSession.commit();
    }
    @org.junit.Test
    public void getDeptCount() {
        int i =deptMapper.getDeptCount();

        System.out.println(i);
    }
    @org.junit.Test
    public void addDept() {
        Dept dept = new Dept();

        dept.setDname("销售");

        dept.setLoc("沈阳");

        deptMapper.addDept(dept);

        sqlSession.commit();

        System.out.println(dept.getDeptno());
    }
    @org.junit.Test
    public void selectlike() {
        Dept dept = new Dept();

        dept.setDname("销售");

//        dept.setLoc("沈阳");

        List<Dept> list = deptMapper.selectlike(dept);

        sqlSession.commit();

        System.out.println(list);
    }
    @org.junit.Test
    public void updateDept() {
        Dept dept = new Dept();

        dept.setDeptno(44);

        dept.setDname("55555555555");

//        dept.setLoc("8888888888888");

        deptMapper.updateDept(dept);

        sqlSession.commit();
    }
    @org.junit.Test
    public void getDeptListMap() {
        Map<String,Object> params = new HashMap();

        params.put("loc","沈阳");

        params.put("dname","销售");

        List<Dept> x = deptMapper.getDeptListMap(params);

        System.out.println(x);

    }
    @org.junit.Test
    public void getEmpDeptInfo() {
        List<Map<String, Object>> empDeptInfo = deptMapper.getEmpDeptInfo();

        for (Map<String, Object> maprow:empDeptInfo){

            for(Map.Entry<String, Object> entry:maprow.entrySet()){

                System.out.println(entry);
            }
        }
    }
    @org.junit.Test
    public void getEmp() {
     Emp emp = empMapper.selectByPrimaryKey(7369);
        System.out.println(emp);
    }
}

EmpMapper 

package com.neusoft.mapper;

import com.neusoft.domain.Emp;

public interface EmpMapper {
    int deleteByPrimaryKey(Integer empno);

    int insert(Emp record);

    int insertSelective(Emp record);

    Emp selectByPrimaryKey(Integer empno);

    int updateByPrimaryKeySelective(Emp record);

    int updateByPrimaryKey(Emp record);
}

DeptMapper

package com.neusoft.mapper;

import com.neusoft.domain.Dept;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/21.
 */
public interface DeptMapper {
    public Dept getDeptById(int i);

    public List<Dept> getAllDepts();

    public int deletDept(int id);

    public int addDept(Dept dept);

    public int updateDept(Dept dept);

    public List<Dept> selectlike(Dept dept);

    public int getDeptCount ();

    public List<Dept> getDeptListMap(Map<String,Object> params);

    public List<Map<String,Object>> getEmpDeptInfo();

}

Emp

package com.neusoft.domain;

import java.math.BigDecimal;
import java.util.Date;

public class Emp {
    private Integer empno;

    private String ename;

    private String job;

    private Integer mgr;

    private Date hiredate;

    private BigDecimal sal;

    private BigDecimal comm;

    private Integer deptno;

    @Override
    public String toString() {
        return "Emp{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", mgr=" + mgr +
                ", hiredate=" + hiredate +
                ", sal=" + sal +
                ", comm=" + comm +
                ", deptno=" + deptno +
                '}';
    }

    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 == null ? null : ename.trim();
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job == null ? null : job.trim();
    }

    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 BigDecimal getSal() {
        return sal;
    }

    public void setSal(BigDecimal sal) {
        this.sal = sal;
    }

    public BigDecimal getComm() {
        return comm;
    }

    public void setComm(BigDecimal comm) {
        this.comm = comm;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
}

Dept

package com.neusoft.domain;


/**
 * Created by Administrator on 2018/11/21.
 */
public class Dept {
    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", loc='" + loc + '\'' +
                '}'+'\n';
    }

    private int deptno;
    private String dname;
    private String loc;

    public int getDeptno() {
        return deptno;
    }

    public void setDeptno(int 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;
    }
}

EmpMapper.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.neusoft.mapper.EmpMapper" >
  <!--约定优先配置-->
  <resultMap id="BaseResultMap" type="com.neusoft.domain.Emp" >
    <id column="empno" property="empno" jdbcType="INTEGER" />
    <result column="ename" property="ename" jdbcType="VARCHAR" />
    <result column="job" property="job" jdbcType="VARCHAR" />
    <result column="mgr" property="mgr" jdbcType="INTEGER" />
    <result column="hiredate" property="hiredate" jdbcType="DATE" />
    <result column="sal" property="sal" jdbcType="DECIMAL" />
    <result column="comm" property="comm" jdbcType="DECIMAL" />
    <result column="deptno" property="deptno" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    empno, ename, job, mgr, hiredate, sal, comm, deptno
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from emp
    where empno = #{empno,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from emp
    where empno = #{empno,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.neusoft.domain.Emp" >
    insert into emp (empno, ename, job, 
      mgr, hiredate, sal, comm, 
      deptno)
    values (#{empno,jdbcType=INTEGER}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR}, 
      #{mgr,jdbcType=INTEGER}, #{hiredate,jdbcType=DATE}, #{sal,jdbcType=DECIMAL}, #{comm,jdbcType=DECIMAL}, 
      #{deptno,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.neusoft.domain.Emp" >
    insert into emp
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="empno != null" >
        empno,
      </if>
      <if test="ename != null" >
        ename,
      </if>
      <if test="job != null" >
        job,
      </if>
      <if test="mgr != null" >
        mgr,
      </if>
      <if test="hiredate != null" >
        hiredate,
      </if>
      <if test="sal != null" >
        sal,
      </if>
      <if test="comm != null" >
        comm,
      </if>
      <if test="deptno != null" >
        deptno,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="empno != null" >
        #{empno,jdbcType=INTEGER},
      </if>
      <if test="ename != null" >
        #{ename,jdbcType=VARCHAR},
      </if>
      <if test="job != null" >
        #{job,jdbcType=VARCHAR},
      </if>
      <if test="mgr != null" >
        #{mgr,jdbcType=INTEGER},
      </if>
      <if test="hiredate != null" >
        #{hiredate,jdbcType=DATE},
      </if>
      <if test="sal != null" >
        #{sal,jdbcType=DECIMAL},
      </if>
      <if test="comm != null" >
        #{comm,jdbcType=DECIMAL},
      </if>
      <if test="deptno != null" >
        #{deptno,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.neusoft.domain.Emp" >
    update emp
    <set >
      <if test="ename != null" >
        ename = #{ename,jdbcType=VARCHAR},
      </if>
      <if test="job != null" >
        job = #{job,jdbcType=VARCHAR},
      </if>
      <if test="mgr != null" >
        mgr = #{mgr,jdbcType=INTEGER},
      </if>
      <if test="hiredate != null" >
        hiredate = #{hiredate,jdbcType=DATE},
      </if>
      <if test="sal != null" >
        sal = #{sal,jdbcType=DECIMAL},
      </if>
      <if test="comm != null" >
        comm = #{comm,jdbcType=DECIMAL},
      </if>
      <if test="deptno != null" >
        deptno = #{deptno,jdbcType=INTEGER},
      </if>
    </set>
    where empno = #{empno,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.neusoft.domain.Emp" >
    update emp
    set ename = #{ename,jdbcType=VARCHAR},
      job = #{job,jdbcType=VARCHAR},
      mgr = #{mgr,jdbcType=INTEGER},
      hiredate = #{hiredate,jdbcType=DATE},
      sal = #{sal,jdbcType=DECIMAL},
      comm = #{comm,jdbcType=DECIMAL},
      deptno = #{deptno,jdbcType=INTEGER}
    where empno = #{empno,jdbcType=INTEGER}
  </update>
</mapper>

DeptMapper.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.neusoft.mapper.DeptMapper">
    <!--sql片段-->
    <sql id="query">
        <where>
            <if test="loc != null and loc != ''">
                and loc = #{loc}
            </if>
            <if test="dname != null">
                and dname like '%${dname}%'
            </if>
        </where>
    </sql>
    <!--多表连接-->
    <select id="getEmpDeptInfo" resultType="Map">
        SELECT emp.ename, emp.sal,dept.loc
        from  emp JOIN dept ON emp.deptno = dept.deptno
    </select>
    <!--根据id查dept-->
    <select id="getDeptById" parameterType="int" resultType="Dept">
        select * FROM  dept where deptno = #{deptno}
    </select>
    <!--Map实现多输入-->
    <select id="getDeptListMap" parameterType="Map" resultType="Dept">
        select * FROM  dept where loc = #{loc} and dname = #{dname}
    </select>
    <!--模糊查询-->
    <select id="selectlike" parameterType="Dept" resultType="Dept">
        SELECT * from dept
       <include refid="query"></include>
    </select>
    <!--where loc = #{loc} and dname like '%${dname}%'-->
    <!--查询所有-->
    <select id="getAllDepts" resultType="Dept">
        SELECT * FROM dept
    </select>
    <!--查询部门数-->
    <select id="getDeptCount" resultType="int">
        SELECT COUNT(*) from dept
    </select>
    <!--插入部门-->
    <insert id="addDept" parameterType="Dept">
        <selectKey keyProperty="deptno" order="AFTER" resultType="int">
            SELECT LAST_insert_id()
        </selectKey>
        INSERT INTO dept (dname , loc) values (#{dname},#{loc})
    </insert>
    <!--删除部门byid-->
    <delete id="deletDept" parameterType="int">
        DELETE from dept where deptno = #{value}
    </delete>
    <!--更新dept表-->
    <update id="updateDept" parameterType="Dept">
        update dept
        <set>
            <if test="dname != null ">
                dname = #{dname}
            </if>
            <if test="loc != null">
                loc = #{loc}
            </if>
        </set>
        where deptno = #{deptno}
    </update>
</mapper>

猜你喜欢

转载自blog.csdn.net/joob000/article/details/84440756