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

微信公众号

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/326580.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Visual Studio 2017全面上市

自从1997年第一版发布的20年以来&#xff0c;微软Visual Studio开发工具一向以易学易用、功能齐全而闻名&#xff0c;帮助开发者以简驭繁&#xff0c;即使面对越来越快速的交付压力&#xff0c;也能大幅提高生产力&#xff0c;好整以暇。对于Visual Studio的使用者而言&#xf…

art-template入门(七)之压缩页面

转载自 art-template压缩页面 压缩页面 template.defaults.minimize art-template 内建的压缩器可以压缩 HTML、JS、CSS&#xff0c;它在编译阶段运行&#xff0c;因此完全不影响渲染速度&#xff0c;并且能够加快网络传输。 开启 template.defaults.minimize true;配置 …

java getimage_Java ImageView.getImage方法代码示例

import javafx.scene.image.ImageView; //导入方法依赖的package包/类FXThreadprivate static void updateListener(NotNull final Node node, NotNull final ImageView imageView,NotNull final ReadOnlyBooleanProperty condition,NotNull final Object listenerKey, NotNull…

Visual Studio 2017发布会:黄金时代的家族聚会

美国时间三月七日&#xff08;北京2017年3月8日&#xff09;&#xff0c;微软正式发布了Visual Studio 2017&#xff0c;自己旗舰开发工具的最新版本。同日发布的主要产品还有 .NET Core Tooling 1.0.NET Core 微服务实例Visual Studio for Mac Preview 4Visual Studio Mobile …

java开发可以转什么软件有哪些_转行开发软件Java编程必须会什么

原标题&#xff1a;转行开发软件Java编程必须会什么要想开发软&#xff0c;Java编程必须会什么&#xff1f;最起码的就是逻辑思维要好&#xff0c;只要不是特别差就没有什么问题。数学是相对比较能够体现出一个人的逻辑思维如何。先想想自己以前上学的时候&#xff0c;数学成绩…

art-template入门(八)之选项

转载自 art-template选项 template.defaults // 模板名 filename: null,// 模板语法规则列表 rules: [nativeRule, artRule],// 是否开启对模板输出语句自动编码功能。为 false 则关闭编码输出功能 // escape 可以防范 XSS 攻击 escape: true,// 启动模板引擎调试模式。如果为…

2017蓝桥杯省赛---java---B---2(纸牌三角形)

题目描述 纸牌三角形 思路分析 全排列特殊去重 ans/6 代码实现 package com.atguigu.TEST;class Main{public static int[] a{1,2,3,4,5,6,7,8,9};public static int ans;public static void f(int k){if(k9){int x1 a[0] a[1] a[2] a[3];int x2 a[3] a[4] a[5] …

微软开源基于云的生理学研究工具

Bio Model Analyzer是一款微软基于云的生理学研究工具&#xff0c;可以用于对化细胞交互和通信进行建模&#xff0c;现已经在GitHub上开源&#xff0c;在MIT许可之下。 研究人员使用Bio Model Analyzer (BMA) 去创建计算机模型&#xff0c;该模型可以比较健康和不健康细胞内的处…

mysql group和order_mysql 用 group by 和 order by同时使用

首先,这是不可能实现的mysql的查询的顺序select -> from-> where->group by->having->order by.但mysql的解析器执行顺序:from-> where->group by->having->select->order by.所以,从执行的流程来看,是先group by 然后在 order by.order by拿到的…

art-template入门(九)之API

API template(filename, content) 根据模板名渲染模板。 参数&#xff1a; {string} filename{Object,string} content返回值&#xff1a; 如果 content 为 Object&#xff0c;则渲染模板并返回 string如果 content 为 string&#xff0c;则编译模板并返回 functionvar html…

2017蓝桥杯省赛---java---B---3(承压计算)

题目描述 7 5 8 7 8 8 9 2 7 2 8 1 4 9 1 8 1 8 8 4 1 7 9 6 1 4 5 4 5 6 5 5 6 9 5 6 5 5 4 7 9 3 5 5 1 7 5 7 9 7 4 7 3 3 1 4 6 4 5 5 8 8 3 2 4 3 1 1 3 3 1 6 6 5 5 4 4 2 9 9 9 2 1 9 1 9 2 9 5 7 9 4 3 3 7 7 9 3 6 1 3 8 8 3 7 3 6 8 1 5 3 9 5 8 3 8 1 8 3 3 8 3 2 3…

[C#7] 1.Tuples(元组)

1. 老版本代码 class Program { static void Main(string[] args) { var fullName GetFullName(); Console.WriteLine(fullName.Item1);// Item1,2,3不能忍&#xff0c;&#xff0c;, Console.WriteLine(fullName.Item2); Console.WriteLine(fullName.Item3); } static Tuple&…

mysql 行转列分级输出_MySQL如何实现行转列分级输出?_MySQL

概述好久没写SQL语句&#xff0c;今天看到问答中的一个问题&#xff0c;拿来研究一下。问题链接&#xff1a;关于Mysql 的分级输出问题情景简介学校里面记录成绩&#xff0c;每个人的选课不一样,而且以后会添加课程&#xff0c;所以不需要把所有课程当作列。数据表里面数据如下…

2017蓝桥杯省赛---java---B---7(日期问题)

题目描述 日期问题 标题&#xff1a;日期问题小明正在整理一批历史文献。这些历史文献中出现了很多日期。小明知道这些日期都在1960年1月1日至2059年12月31日。令小明头疼的是&#xff0c;这些日期采用的格式非常不统一&#xff0c;有采用年/月/日的&#xff0c;有采用月/日/…

ssm使用全注解实现增删改查案例——DeptMapperImpl

package org.dao.impl;import java.util.List;import org.dao.IDeptMapper; import org.entity.Dept; import org.springframework.beans.factory.annotation.Autowired;public class DeptMapperImpl implements IDeptMapper {//自动注入Autowiredprivate IDeptMapper deptMapp…

Gson的入门使用

转载自 Gson的入门使用 Java对象和Json之间的互转&#xff0c;一般用的比较多的两个类库是Jackson和Gson&#xff0c;下面记录一下Gson的学习使用。 基础概念&#xff1a; Serialization:序列化&#xff0c;使Java对象到Json字符串的过程。 Deserialization&#xff1a;反序…

Visual Studio 2017正式版离线安装及介绍

Visual Studio 2017 RTM正式版离线安装及介绍。 首先至官网下载&#xff1a;https://www.visualstudio.com/zh-hans/downloads/ VS 2017 正式版介绍&#xff1a; https://www.visualstudio.com/zh-hans/vs/whatsnew/ VS 2017 离线模式只离线.NET Core部分&#xff1a; Visual S…

ssm使用全注解实现增删改查案例——EmpMapperImpl

package org.dao.impl;import java.util.List;import org.dao.IEmpMapper; import org.entity.Emp; import org.springframework.beans.factory.annotation.Autowired;public class EmpMapperImpl implements IEmpMapper {//自动注入Autowiredprivate IEmpMapper empMapper;/*…

Linux下查找命令

转载自 Linux下查找命令 一.Linux查找文件的相关命令 常 用 命 令 简要中文说明 程序所在目录 more 分页显示一个文件或任何输出结果 /bin less 分页显示一个文件并且可以回头 /usr/bin whereis 寻找文件工具 /usr/bin find 寻找文件工具 /usr/bin locate 寻…

wadl2java cxf_java – CXF JAXRS |生成的wadl中不存在复杂响应类型

我们使用cxf 2.5.2和spring来暴露和消费宁静的服务.为了分发服务接口类,我们开始使用wadl2java目标(根据给定的wadl文件生成接口类)生成的wadl不包含正确的响应类型,因为我猜测,生成的接口都有’Response’作为返回类型.防爆.如果restful get方法返回’List’,则生成的wadl仅包…