日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mybatis使用全注解的方式案例(包含一对多关系映射)

發布時間:2023/12/3 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 configurationPUBLIC "-//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,不是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(); }

然后就可以正常的主外鍵關聯,包括查詢顯示,如圖:

需要注意的是主外鍵映射,我總結了以下的方法,大家可以進行看一下:

一: @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

總結

以上是生活随笔為你收集整理的mybatis使用全注解的方式案例(包含一对多关系映射)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。