ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Springboot:mybatis与mapper.xml

2022-06-30 22:03:21  阅读:86  来源: 互联网

标签:mapper name 接口 user mybatis id select Springboot


(37条消息) Mybatis中mapper的实现原理_叩丁狼的博客-CSDN博客_mybatis中的mapper

 mybatis mapper详解 - 简书

Mybatis是一种利用Mapper接口来进行数据库查询和Java开发的ORM。

其中的Mapper接口(有时也称作Dao)中,只写了方法定义没有具体的实现类,那它是如何实现具体的业务呢?

1、Mybatis的相关文件

1)mapper接口文件

接口文件中只是定义了方法,并没有具体的实现:

public interface UserMapper {
    void save(User u);
}

mapper只是个接口,在其中只是说了有哪些方法;

在映射文件mapper.xml中,写这些方法对应的SQL语句。

注:有时也写成Dao

2)mapper.xml映射文件

<mapper namespace="cn.wolfcode.mybatis.mapper.UserMapper">
    <insert id="save">
        INSERT INTO user (id, username, password) VALUES (NULL, #{username}, #{password})
    </insert>
</mapper>

 mapper映射文件如何与mapper接口的方法相对应?

①mapper的namespace属性为带包名的mapper接口

②构造一个增删改查对应的片段:

  • 插入就是<insert>、查询就是<select>;

  • 片段的id属性是接口方法名;

  • 片段的内容就是具体的SQL语句,其中的变量#{username},#{password}就是接口方法传入的Bean的成员变量,以上文为例,会自动组装为u.username和u.password。

本例中只是用到了INSERT方法,还可以用别的:

//对于想mysql这样的,支持自动增加key值得可以使用如下方式
<insert id="insertStudent" parameterType="Student"useGeneratedKeys="true" keyProperty="studId">
  INSERT INTO STUDENTS(NAME, EMAIL, PHONE)
  VALUES(#{name},#{email},#{phone})
</insert>
//对于像oracle这样的不支持的,需要通过某个queue来获取的,可以使用下面两种方式
<insert id="insertStudent" parameterType="Student">
  <selectKey keyProperty="studId" resultType="int" order="BEFORE">
    SELECT ELEARNING.STUD_ID_SEQ.NEXTVAL FROM DUAL
  </selectKey>

  INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL, PHONE)
  VALUES(#{studId},#{name},#{email},#{phone})
</insert>

//下面这种方式通过在表上建trigger来插入key,然后通过selectKey获取
<insert id="insertStudent" parameterType="Student">
  INSERT INTO STUDENTS(NAME,EMAIL, PHONE)
  VALUES(#{name},#{email},#{phone})
  <selectKey keyProperty="studId" resultType="int" order="AFTER">
    SELECT ELEARNING.STUD_ID_SEQ.CURRVAL FROM DUAL
  </selectKey>
</insert>

<update id="updateStudent" parameterType="Student">
  UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, PHONE=#{phone}
  WHERE STUD_ID=#{studId}
</update>

<delete id="deleteStudent" parameterType="int">
  DELETE FROM STUDENTS WHERE STUD_ID=#{studId}
</delete>

返回值均为收此次sql语句执行影响到的数据行数

 

resultMap

当我们的JavaBean的属性与table的列名并非简单的一对一时(比如出现了属性名和列名不同、涉及嵌套等情况),可以在xml中通过resultMap的形式来指定它们的对应关系:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yy.springbootmybatisxml.dao.UserDao" >
    <resultMap id="BaseResultMap" type="com.yy.springbootmybatisxml.bean.User" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="name" property="name" jdbcType="VARCHAR" />
        <result column="pwd" property="pwd" jdbcType="VARCHAR" />
        <result column="age" property="age" jdbcType="INTEGER" />
    </resultMap>

    <select id="login" resultType="java.lang.Long">
        select u.id from t_user u where u.name=#{param1} and u.pwd=#{param2}
    </select>

    <select id="findUserById" resultMap="BaseResultMap">
        select * from t_user u where u.id=#{id}
    </select>

    <insert id="register">
        insert into t_user (name,age,pwd) values(#{param1},#{param2},#{param3})
    </insert>

</mapper>

以findUserById这段select为例:

    <select id="findUserById" resultMap="BaseResultMap">
        select * from t_user u where u.id=#{id}
    </select>

含义是,接口mapper中的findUserById方法,调用的select语句为select * from t_user u where u.id=#{id},返回结果为一个JavaBean,这个JavaBean由<resultMap>的type属性加以说明,是com.yy.springbootmybatisxml.bean.User,查询到的列与User的属性的对应关系,由resultMap的各行指定。 

也就是说,resultType指定了返回结果类型,而resultMap指定了返回结果与JavaBean间的映射关系。

注:#{id}的含义是,接口中该方法参数中的id

 

<sql>与<include refid="xxx">

mybatis include refid=“xxxx“的含义_bidianzhang的博客-CSDN博客

有时可能会出现下边这种SELECT语句

    <sql id="Base_Column_List" >
        id, name, age, pwd
    </sql>

    <select id="findUserByName" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from t_user
        where name=#{name}
    </select>

<include refid="xxx">的含义是,查询refid中指定的几个字段

 

 

3)配置文件.properties

在配置文件中,需要写明确mapper类及其对应的xml文件:

mybatis.typeAliasesPackage: com.yy.springbootmybatisxml.dao
mybatis.mapperLocations: classpath:mapper/*.xml

4)Service接口及实现类ServiceImpl

在Service类中调用mapper接口方法进行数据库操作。

需要用@Autowired注入之前的mapper接口

@Service
public class UserServicesImpl implements UserServices {
    @Autowired
    private UserMapper userMapper;
    ...
}

调用mapper方法

public Student save(User u){
   userMapper.save(u);
}

 

标签:mapper,name,接口,user,mybatis,id,select,Springboot
来源: https://www.cnblogs.com/ShineLeBlog/p/16427686.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有