(37条消息) Mybatis中mapper的实现原理_叩丁狼的博客-CSDN博客_mybatis中的mapper
mybatis mapper详解 - 简书
Mybatis是一种利用Mapper接口来进行数据库查询和Java开发的ORM。
其中的Mapper接口(有时也称作Dao)中,只写了方法定义而没有具体的实现类,那它是如何实现具体的业务呢?
接口文件中只是定义了方法,并没有具体的实现:
public interface UserMapper { void save(User u); }
mapper只是个接口,在其中只是说了有哪些方法;
在映射文件mapper.xml中,写这些方法对应的SQL语句。
<mapper namespace="cn.wolfcode.mybatis.mapper.UserMapper"> <insert id="save"> INSERT INTO user (id, username, password) VALUES (NULL, #{username}, #{password}) </insert> </mapper>
mapper映射文件如何与mapper接口的方法相对应?
本例中只是用到了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语句执行影响到的数据行数
当我们的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间的映射关系。
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>
在配置文件中,需要写明确mapper类及其对应的xml文件:
mybatis.typeAliasesPackage: com.yy.springbootmybatisxml.dao mybatis.mapperLocations: classpath:mapper/*.xml
在Service类中调用mapper接口方法进行数据库操作。
@Service public class UserServicesImpl implements UserServices { @Autowired private UserMapper userMapper; ... }
public Student save(User u){ userMapper.save(u); }