Mapper映射文件是一个xml格式文件,必须遵循相应的dtd文件规范,如 ibatis-3-mapper.dtd (http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd)。映射文件是以作为根节点,在根节点中支持9个元素,分别为 insert、update、delete、select(增删改查);cache、cache-ref、resultMap、parameterMap、sql。

 

Mapper接口开发需要遵循以下规范:

1、Mapper.xml 文件中的 namespace 与 mapper 接口的类路径相同。

2、Mapper 接口方法名和 Mapper.xml 中定义的每个 statement 的 id 相同。

3、Mapper 接口方法的输入参数类型和 mapper.xml 中定义的每个 sql 的 parameterType 的类型相同。

4、Mapper 接口方法的输出参数类型和 mapper.xml 中定义的每个 sql 的 resultType 的类型相同。

 首先对增、删、改进行描述,然后对查进行详细说明,最后对其他五个元素进行简单说明。

(1)insert、update、delete

我们先从配置文件看起: 

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

id="addUser"

parameterType="user"

flushCache="true"

statementType="PREPARED"

keyProperty=""

keyColumn=""

useGeneratedKeys="false"

timeout="20">

id="updateUser"

parameterType="user"

flushCache="true"

statementType="PREPARED"

timeout="20">

id="deleteUser"

parameterType="user"

flushCache="true"

statementType="PREPARED"

timeout="20">

 上面给出了一个比较全面的配置说明,但是在实际使用过程中并不需要都进行配置,可根据自己的需要删除部分配置项。在这里,列举出配置文件,精简之后是这样的:  

PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"

"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

insert into user(name,password,age) values(#{name},#{password},#{age})

delete from user where id = #{id}

update user set name = #{name}, password = #{password}, age = #{age} where id = #{id}

 这里的 parameterType 设置成 user 是因为如果不设置的情况下,会自动将类名首字母小写后的名称,原来的类名为User。不过,建议大家还是使用 typeAlias 进行配置。唯一需要说明的就是元素里面的 useGeneratedKeys 和 keyProperties 属性,这两个属性是用来获取数据库中的主键的。在数据库里面经常性的会给数据库表设置一个自增长的列作为主键,如果我们操作数据库后希望能够获取这个主键该怎么弄呢?正如上面所述,如果是支持自增长的数据库,如 mysql 数据库,那么只需要设置useGeneratedKeys 和 keyProperty 属性便可以了,但是对于不支持自增长的数据库(如 oracle)该怎么办呢?mybatis里面在元素下面提供了子元素用于帮助解决这个问题。来看下配置:  

keyProperty="id"

resultType="int"

order="BEFORE"

statementType="PREPARED">

 针对不能使用自增长特性的数据库(Oracle),可以使用下面的配置来实现相同的功能:

select seq_user_id.nextval as id from dual

insert into user(id, name, password, age, deleteFlag)

values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})

(2)select、resultType、resultMap我们先来看看select元素都有哪些配置可以设置:   

id="findUserById"

parameterType="int"

resultType="User"

resultMap="userResultMap"

flushCache="false"

useCache="true"

timeout="10000"

fetchSize="256"

statementType="PREPARED"

resultSetType="FORWARD_ONLY">

 我们还是从具体的示例来看看,  

这里我们根据用户id去查询这个用户的信息,resultType=User是一个别名,如果我们接触到的是这种一对一的问题,那么可以简单的定义一个实体,这个实体代表数据库表中的一条记录即可。

 

但是如果我们遇到一对多的问题呢,就拿这里的查询用户信息来说,如果每个用户有各种兴趣,需要维护每个用户的兴趣信息,那么我们可能就存在下面的数据表结构:  

CREATE TABLE `user` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(255) NOT NULL,

`password` varchar(255) NOT NULL,

`age` int(3) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

CREATE TABLE `userinterests` (

`userid` int(11) DEFAULT NULL,

`interestid` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `interests` (

`interestid` int(11) NOT NULL,

`interestname` varchar(255) DEFAULT NULL,

PRIMARY KEY (`interestid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

其中user表用于记录用户信息,interests表用于维护所有的兴趣标签,而userinterests用于维护每个用户的兴趣情况。这时候,如果我们需要根据用户id去查询用户的信息和兴趣信息该怎么做呢?这时候我们就要用到

select a.*,c.* from user as a right join userinterests as b on a.id = b.userid         right join interests as c on b.interestid = c.interestid where id = #{id}

 最后将这个映射文件添加到元素配置下:

 下面我们来写个测试代码来测试一下是否可以正常运行:  

public class UserDaoTest3 {

@Test

public void testFindUserById(){

SqlSession sqlSession = getSessionFactory().openSession(true);

UserDao2 userMapper = sqlSession.getMapper(UserDao2.class);

User2 user = userMapper.findUserById(10);

System.out.println("记录为:"+user);

}

// Mybatis 通过SqlSessionFactory获取SqlSession, 然后才能通过SqlSession与数据库进行交互

private static SqlSessionFactory getSessionFactory() {

SqlSessionFactory sessionFactory = null;

String resource = "configuration.xml";

try {

sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource));

} catch (IOException e) {

e.printStackTrace();

}

return sessionFactory;

}

}

(3)sqlsql元素的意义,在于我们可以定义一串SQL语句,其他的语句可以通过引用它而达到复用它的目的。(4)cache、cache-refREFhttps://blog.csdn.net/majinggogogo/article/details/72123185

http://www.mybatis.cn/archives/32.html

http://www.mybatis.cn/archives/33.htmlhttps://blog.csdn.net/weixin_41653442/article/details/81747746https://www.cnblogs.com/lcngu/p/5470695.htmlhttps://www.cnblogs.com/jasonboren/p/11394707.htmlhttps://blog.csdn.net/qq_39340792/article/details/112689626http://www.zzvips.com/article/124301.html

查看原文