mybatis使用全注解的方式案例(包含一对多关系映射)
前面我写过ssh:ssh(Spring+Spring mvc+hibernate)简单增删改查案例 和ssm:ssm(Spring+Spring mvc+mybatis)的案例,需要了解的可以去看看,今天我写了一下ssm(spring+springmvc+mybatis)全注解的方式又重新写了一遍两表增删改查的案例,其中别的地方都一样,就是有几个文件不一样,
1.其中:
mybatis-config.xml中:
<?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>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<typeAlias alias="Emp" type="org.entity.Emp"/>
<typeAlias alias="Dept" type="org.entity.Dept"/>
</typeAliases>
<mappers>
<mapper class="org.dao.IEmpMapper"/>
<mapper class="org.dao.IDeptMapper"/>
</mappers>
</configuration>
注意看, <mapper class="org.dao.IEmpMapper"/>,mapper后面是class,不是resource,一定要注意
2.还有:我们不需要EmpMapper.xml和DeptMapper.xml文件,直接删掉就可以了
3.修改我们的IEmpMapper接口为:
package org.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.entity.Emp;
public interface IEmpMapper {
//根据编号删除.
@Delete("delete from emp where eid = #{eid} ")
int deleteByPrimaryKey(Integer eid);
//添加
@Insert("insert into emp (eid, ename, eage, edate, did) " +
"values (#{eid,jdbcType=INTEGER}," +
" #{ename,jdbcType=VARCHAR}, " +
"#{eage,jdbcType=INTEGER}, " +
"#{edate,jdbcType=TIMESTAMP}," +
" #{did,jdbcType=INTEGER})")
int insert(Emp record);
//根据编号查询
@Select("select * from emp where eid = #{eid}")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="dept",column="did",javaType=org.entity.Dept.class,
one=@One(select="org.dao.IDeptMapper.selectByPrimaryKey"))
})
Emp selectByPrimaryKey(Integer eid);
//修改
@Update("pdate emp " +
" set ename = #{ename,jdbcType=VARCHAR}, " +
" eage = #{eage,jdbcType=INTEGER}, " +
" edate = #{edate,jdbcType=TIMESTAMP}, " +
" did = #{did,jdbcType=INTEGER} " +
"where eid = #{eid,jdbcType=INTEGER}")
int updateByPrimaryKey(Emp record);
//查询全部
@Select("select * from emp")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="dept",column="did",javaType=org.entity.Dept.class,
one=@One(select="org.dao.IDeptMapper.selectByPrimaryKey"))
})
List<Emp> findEmpAll();
//根据部门编号查询员工信息
@Select("select * from emp where did = #{dids}")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="dept",column="did",javaType=org.entity.Dept.class,
one=@One(select="org.dao.IDeptMapper.selectByPrimaryKey"))
})
List<Emp> findEmpByDept(int did);
}
4.修改我们的IDeptMapper接口为:
package org.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.entity.Dept;
public interface IDeptMapper {
@Delete("delete from dept where id = #{id}")
int deleteByPrimaryKey(Integer id);
@Insert("insert into dept (id, name, loc )" +
" values (#{id,jdbcType=INTEGER}, " +
"#{name,jdbcType=VARCHAR}, " +
"#{loc,jdbcType=VARCHAR})")
int insert(Dept record);
@Select("select * from dept where id = #{id}")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="loc",column="loc"),
@Result(property="empList",column="id",javaType=List.class,
many=@Many(select="org.dao.IEmpMapper.findEmpByDept"))
})
Dept selectByPrimaryKey(Integer id);
@Update("update dept " +
"set name = #{name,jdbcType=VARCHAR}, " +
" loc = #{loc,jdbcType=VARCHAR} " +
"where id = #{id,jdbcType=INTEGER}")
int updateByPrimaryKey(Dept record);
@Select("select * from dept")
List<Dept> findDeptAll();
}
然后就可以正常的主外键关联,包括查询显示,如图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gPquvH5t-1671194692981)(//img-blog.csdn.net/2018031416144635?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTM3Mzk3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]
需要注意的是主外键映射,我总结了以下的方法,大家可以进行看一下:
一:
@Select("select * from dept where id = #{id}")
@Results({
@Result(id=true,property="id",column="id"),
@Result(property="name",column="name"),
@Result(property="loc",column="loc"),
@Result(property="实体类里面的属性",
column="id",javaType=List.class,
many=@Many(select="多方的接口.根据一方的编号查询多方的集合"))
})
Dept selectByPrimaryKey(Integer id);
多:
//根据编号查询
@Select("select * from emp where eid = #{eid}")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="实体中的对象",column="外键列",javaType=一方类.class,
one=@One(select="一方接口.根据一方编号查询信息"))
})
Emp selectByPrimaryKey(Integer eid);
//查询全部
@Select("select * from emp")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="实体中的对象",column="外键列",javaType=一方类.class,
one=@One(select="一方接口.根据一方编号查询信息"))
})
List<Emp> findEmpAll();
//根据部门编号查询员工信息
@Select("select * from emp where did = #{dids}")
@Results({
@Result(id=true,property="eid",column="eid"),
@Result(property="ename",column="ename"),
@Result(property="eage",column="eage"),
@Result(property="实体中的对象",column="外键列",javaType=一方类.class,
one=@One(select="一方接口.根据一方编号查询信息"))
})
List<Emp> findEmpByDept(int did);
按照这个方法配置保证阿弥陀佛了!!!
下面就是源码:
控制器:
DeptController
EmpController
Dao层:
IDeptMapper
IEmpMapper
DaoImpl层:
DeptMapperImpl
EmpMapperImpl
实体类层:
Dept
Emp
Service层:
IDeptService
IEmpService
ServiceImpl层:
DeptServiceImpl
EmpServiceImpl
配置文件:
applicationContext-servlet.xml
applicationContext.xml
mybatis-config.xml
web.xml
前台页面:
index.jsp
saveDept.jsp
saveEmp.jsp
showDept.jsp
showEmp.jsp
updateDept.jsp
updateEmp.jsp
- 点赞
- 收藏
- 关注作者
评论(0)