Springboot:mybatis与mapper.xml

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

 mybatis mapper详解 - 简书

(41条消息) 3.Mapper.xml 详解_Training.L的博客-CSDN博客_mapper.xml

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

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

1、Mybatis的相关文件

1)mapper接口文件

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

注:mapper接口有时也写成Dao

//dao包下写接口
public interface UserMapper {
void save(User u); }

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

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

2)mapper.xml映射文件

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

 以上边这段代码为例介绍,mapper映射文件(xml文件)如何与mapper接口方法相对应

①第一行:mapper的namespace属性带包名的mapper接口

 

<mapper namespace="com.mybatis.mapper.UserMapper">
 ...
</mapper>

 

②<mapper>结构块内:构造增删改查对应的片段:

  • 插入是<insert>、查询是<select>,删除是<delete>,更新是<update>;

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

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

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

<!--对于想mysql这样的,支持自动增加key值得可以使用如下方式-->
<insert id="insertStudent" parameterType="Student"useGeneratedKeys="true" keyProperty="studId">
  INSERT INTO STUDENTS(NAME, EMAIL, PHONE)
  VALUES(#{name},#{email},#{phone})
</insert>
<!--最后返回的id可以在传入的参数Student中找到,通过getXxx方法-->


<!--对于像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语句执行影响到的数据行数-->

 

上文参数含义

  • parameterType:Dao文件中的对应方法的参数类型;
  • resultType:返回值类型(基本类型)
  • resultMap:返回值类型(Bean)

问题

1、如何插入一条记录并获得该记录的ID?

通常我们在用insert往数据库中插入记录时,不会显式说明id的值,id是由数据库自行设置的自增主键,每当有新记录进来都会自动为其赋予一个id,那么如何获取这个id?

解决方法:

(41条消息) mybatis执行insert语句后,返回当前插入数据主键的方法_NmzI怼怼怼的博客-CSDN博客_mybatisinsert返回主键

(41条消息) Mybatis 在 insert 插入操作后返回主键 id_Aries66666的博客-CSDN博客_mybatisinsert返回主键

在mapper.xml中的插入项中,添加属性:

  • useGeneratedKeys = "true"
  • keyProperty="id"

这两项的含义是:

 

 获取到的主键值会赋值给Bean(这个Bean由parameterType指定)的id属性。

<insert id="insertSearch" parameterType="com.zhz.selenium.pojo.Search" useGeneratedKeys="true" keyProperty="id">
        insert into tab_search (keywordid, searchdate, successstate) VALUES(#{keyWordId}, #{searchDate}, #{successState})
    </insert>

 

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指定了返回结果为Java基本类型,而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);
}

 

posted @ 2022-06-30 21:49  ShineLe  阅读(2315)  评论(0编辑  收藏  举报