mybatis映射XML文件详解及实例
mybatis映射XML文件
一个简单的映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
当然这个文件中没有任何的元素
The Mapper XML files have only a few first class elements :
- cache – Configuration of the cache for a given namespace.
- cache-ref – Reference to a cache configuration from another namespace.
- resultMap – The most complicated and powerful element that describes how to load your objects from the database result sets.
- sql – A reusable chunk of SQL that can be referenced by other statements.
- insert – A mapped INSERT statement.
- update – A mapped UPDATE statement.
- delete – A mapped DELETE statement.
- select – A mapped SELECT statement.
select
简单的例子:
select也有很多属性可以让你配置:
insert, update and delete
语句:
insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} delete from Author where id = #{id}
f your database supports auto-generated key fields (e.g. MySQL and SQL Server),上面的插入语句可以写成:
insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})
如果你的数据库还支持多条记录插入,可以使用下面这个语句:
insert into Author (username, password, email, bio) values (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
sql
这个element可以定义一些sql代码的碎片,然后在多个语句中使用,降低耦合。比如:
${alias}.id,${alias}.username,${alias}.password
然后在下面的语句中使用:
Result Maps
官网给了个最最复杂的例子
大体意思呢就是一个博客系统有一个作者,很多博文,博文中有一个作者,很多评论,很多标签(包括了一对多,一对一)
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/shujuku/shujukujiqiao/101404.html