在上次做的父子模块的maven以及Ajax实现人工管理系统的基础上使用Mybatis实现员工管理系统的增删改查,具体运行效果如下:
Mybatis框架的一般执行流程:
因为其他的代码都没有改变,所以这里只展示一下使用Mybatis改变部分的源码:
mybatis-config.xml
DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties">properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
<typeAliases>
<package name="com.fs.model"/>
typeAliases>
<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="${user}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
<mappers>
<mapper class="com.fs.dao.EmpDao">mapper>
<mapper resource="mapper/EmpAndDeptMapper.xml">mapper>
<mapper class="com.fs.dao.DeptDao">mapper>
mappers>
configuration>
Dao层代码:
DeptDao
public interface DeptDao {
@Select("select * from dept")
List<Dept> queryAllDept();
}
EmpDao
public interface EmpDao {
@Select("select * from emp where ename=#{ename} and password=#{password}")
Emp queryEmp(@Param("ename") String ename, @Param("password") String password);
@Select("select * from emp where ename=#{ename} ")
Emp queryEmpByName(String ename) ;
@Insert("insert into emp values(#{empno},#{ename}, #{job}, #{mgr},#{hiredate}, #{sal},#{sal},#{deptno}, #{password})")
int addEmp(Emp emp);
@Delete("delete from emp where empno=#{empno}")
int deleteEmpById(String empno);
@Select("select * from emp where empno=#{empno}")
Emp queryEmpById(Integer empno);
@Update("update emp set ename=#{ename},job=#{job},mgr=#{mgr},hiredate=#{hiredate},sal=#{sal},comm=#{comm},deptno=#{deptno} where empno=#{empno}")
int updateEmp(Emp emp);
}
EmpAndDeptDao(这个没使用注解开发,而是写在了mapper.xml里面)
public interface EmpAndDeptDao {
List<EmpAndDept> queryAllEmps(EmpVo empVo);
}
对应的mapper.xml源码如下:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.fs.dao.EmpAndDeptDao">
<select id="queryAllEmps" resultType="EmpAndDept" parameterType="com.fs.vo.EmpVo">
select emp.*,dept.dname from emp INNER JOIN dept on emp.deptno=dept.deptno
<where>
<if test="dname!=null">
and dname like '%${dname}%'
if>
<if test="job!=null">
and job like '%${job}%'
if>
<if test="sal!=0">
and sal > #{sal};
if>
where>
select>
mapper>
对应service实现类的代码如下:
DeptServiceImpl:
public class DeptServiceImpl implements DeptService {
SqlSessionFactory sqlSessionFactory = BuilderSessionFactory.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession();
DeptDao mapper = session.getMapper(DeptDao.class);
@Override
public List<Dept> queryAllDept() {
return mapper.queryAllDept();
}
}
EmpAndDeptServiceImpl:
public class EmpAndDeptServiceImpl implements EmpAndDeptService {
SqlSessionFactory sqlSessionFactory = BuilderSessionFactory.getSqlSessionFactory();
@Override
public List<EmpAndDept> queryAllEmps(EmpVo empVo) {
SqlSession session = sqlSessionFactory.openSession();
EmpAndDeptDao mapper = session.getMapper(EmpAndDeptDao.class);
List<EmpAndDept> empAndDepts = mapper.queryAllEmps(empVo);
for (EmpAndDept empAndDept : empAndDepts) {
System.out.println(empAndDept);
}
session.close();
return empAndDepts;
}
EmpServiceImpl:
public class EmpServiceImpl implements EmpService {
SqlSessionFactory sqlSessionFactory = BuilderSessionFactory.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession(true);
EmpDao mapper = session.getMapper(EmpDao.class);
@Override
public int queryEmp(String ename, String password) {
Emp emp = mapper.queryEmp(ename, password);
if (emp!=null){
return 0;
}
return 1;
}
@Override
public Emp queryEmpByName(String ename) {
return mapper.queryEmpByName(ename);
}
@Override
public int addEmp(Emp emp) {
return mapper.addEmp(emp);
}
@Override
public int deleteEmpById(String empno) {
/*int i = mapper.deleteEmpById(empno);
session.commit();*/
return mapper.deleteEmpById(empno);
}
@Override
public Emp queryEmpById(Integer empno) {
return mapper.queryEmpById(empno);
}
@Override
public int updateEmp(Emp emp) {
return mapper.updateEmp(emp);
}
}
以上就是用Mybatis在上次做的父子模块的maven以及Ajax基础上实现的员工管理系统的增删改查功能。此外补充一点Mybatis面试时常考的题:
$
{}的区别是什么?$
{}是字符串替换。$
{}时,就是把$
{}替换成变量的值。