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

简介: 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,不是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


相关文章
|
9天前
|
存储 Java 数据库连接
Mybatisplus中的主要使用注解
3.有些注解需要配合其他配置使用。例如,@Version需要配合乐观锁插件使用,@EnumValue需要配合对应的TypeHandler使用。
57 11
|
1月前
|
SQL XML Java
MyBatis——选择混合模式还是全注解模式?
在MyBatis开发中,Mapper接口的实现方式有两种:全注解模式和混合模式。全注解模式直接将SQL嵌入代码,适合小规模、简单逻辑项目,优点是直观简洁,但复杂查询时代码臃肿、扩展性差。混合模式采用接口+XML配置分离的方式,适合大规模、复杂查询场景,具备更高灵活性与可维护性,但学习成本较高且调试不便。根据项目需求与团队协作情况选择合适模式至关重要。
46 4
|
2月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
52 0
|
4月前
|
XML Java 数据库连接
Mybatis一对一,一对多关联查询
## MyBatis一对一、一对多关联查询详解 MyBatis是一款优秀的持久层框架,提供了灵活的SQL映射功能,支持复杂的数据库操作。本文将详细介绍MyBatis中一对一和一对多关联查询的实现。 ### 一对一关联查询 一对一关联关系指的是一个表中的一条记录与另一个表中的一条记录相关联。例如,一个用户有一个地址信息。 #### 数据库表设计 假设有两个表:`user`和 `address`。 ``` CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE address
93 18
|
6月前
|
SQL Java 数据库连接
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
MyBatis-Plus是一个MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。本文讲解了最新版MP的使用教程,包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段等核心功能。
1037 4
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
|
6月前
|
SQL 缓存 Java
MyBatis如何关闭一级缓存(分注解和xml两种方式)
MyBatis如何关闭一级缓存(分注解和xml两种方式)
196 5
|
4月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
173 2
|
7月前
|
Java 数据库连接 Maven
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和MyBatis Generator,使用逆向工程来自动生成Java代码,包括实体类、Mapper文件和Example文件,以提高开发效率。
295 2
mybatis使用一:springboot整合mybatis、mybatis generator,使用逆向工程生成java代码。
|
7月前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
193 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
7月前
|
前端开发 Java Apache
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个
本文详细讲解了如何整合Apache Shiro与Spring Boot项目,包括数据库准备、项目配置、实体类、Mapper、Service、Controller的创建和配置,以及Shiro的配置和使用。
1599 1
Springboot整合shiro,带你学会shiro,入门级别教程,由浅入深,完整代码案例,各位项目想加这个模块的人也可以看这个,又或者不会mybatis-plus的也可以看这个