mapper配置文件中的动态SQL

 1 <?xml version="1.0" encoding="UTF-8" ?>   
 2 <!DOCTYPE mapper   
 3 PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
 4 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 
 5 
 6 <!-- mapper 为根元素节点, 一个namespace对应一个dao -->
 7 <mapper namespace="com.dy.dao.UserDao">
 8 
 9     <insert
10       <!-- 1. id (必须配置)
11         id是命名空间中的唯一标识符,可被用来代表这条语句。 
12         一个命名空间(namespace) 对应一个dao接口, 
13         这个id也应该对应dao里面的某个方法(相当于方法的实现),因此id 应该与方法名一致 -->
14       
15       id="insertUser"
16       
17       <!-- 2. parameterType (可选配置, 默认为mybatis自动选择处理)
18         将要传入语句的参数的完全限定类名或别名, 如果不配置,mybatis会通过ParameterHandler 根据参数类型默认选择合适的typeHandler进行处理
19         parameterType 主要指定参数类型,可以是int, short, long, string等类型,也可以是复杂类型(如对象) -->
20       
21       parameterType="com.demo.User"
22       
23       <!-- 3. resultType (resultType 与 resultMap 二选一配置)
24          resultType用以指定返回类型,指定的类型可以是基本类型,可以是java容器,也可以是javabean -->
25 
26       resultType="hashmap"
27       
28       <!-- 4. resultMap (resultType 与 resultMap 二选一配置)
29          resultMap用于引用我们通过 resultMap标签定义的映射类型,这也是mybatis组件高级复杂映射的关键 -->
30 
31       resultMap="personResultMap"
32 
33       <!-- 5. flushCache (可选配置,默认配置为true)
34         将其设置为 true,任何时候只要语句被调用,都会导致本地缓存和二级缓存都会被清空,默认值:true(对应插入、更新和删除语句) -->
35       
36       flushCache="true"
37 
38        <!-- 6. useCache (可选配置)
39          将其设置为 true,将会导致本条语句的结果被二级缓存,默认值:对 select 元素为 true -->
40 
41       useCache="true"
42       
43       <!-- 7. statementType (可选配置,默认配置为PREPARED)
44         STATEMENT,PREPARED 或 CALLABLE 的一个。这会让 MyBatis 分别使用 Statement,PreparedStatement 或 CallableStatement,默认值:PREPARED。 -->
45       
46       statementType="PREPARED"
47       
48       <!-- 8. keyProperty (可选配置, 默认为unset)
49         (仅对 insert 和 update 有用)唯一标记一个属性,MyBatis 会通过 getGeneratedKeys 的返回值或者通过 insert 语句的 selectKey 子元素设置它的键值,默认:unset。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 -->
50       
51       keyProperty=""
52       
53       <!-- 9. keyColumn     (可选配置)
54         (仅对 insert 和 update 有用)通过生成的键值设置表中的列名,这个设置仅在某些数据库(像 PostgreSQL)是必须的,当主键列不是表中的第一列的时候需要设置。如果希望得到多个生成的列,也可以是逗号分隔的属性名称列表。 -->
55       
56       keyColumn=""
57       
58       <!-- 10. useGeneratedKeys (可选配置, 默认为false)
59         (仅对 insert 和 update 有用)这会令 MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键(比如:像 MySQL 和 SQL Server 这样的关系数据库管理系统的自动递增字段),默认值:false。  -->
60       
61       useGeneratedKeys="false"
62 
63       <!-- 11. fetchSize (可选配置) 
64          这是尝试影响驱动程序每次批量返回的结果行数和这个设置值相等。默认值为 unset(依赖驱动)-->
65 
66       fetchSize="256"
67       
68       <!-- 12. timeout  (可选配置, 默认为unset, 依赖驱动)
69         这个设置是在抛出异常之前,驱动程序等待数据库返回请求结果的秒数。默认值为 unset(依赖驱动)。 -->
70       timeout="20">
71 
72     <update
73       id="updateUser"
74       parameterType="com.demo.User"
75       flushCache="true"
76       statementType="PREPARED"
77       timeout="20">
78 
79     <delete
80       id="deleteUser"
81       parameterType="com.demo.User"
82       flushCache="true"
83       statementType="PREPARED"
84       timeout="20">
85 </mapper>
View Code

项目中插入后拿到主键的例子:

 1 <!--要在插入后能拿到主键,也就是mysql自动生成的主键,需要使用useGenerateKeys属性和keyProperty属性(字段名),这个id就会自动填充到shipping的id中-->
 2   <insert id="insert" parameterType="com.mall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
 3     insert into shipping (id, user_id, receiver_name, 
 4       receiver_phone, receiver_mobile, receiver_province, 
 5       receiver_city, receiver_district, receiver_address, 
 6       receiver_zip, create_time, update_time
 7       )
 8     values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR}, 
 9       #{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR}, 
10       #{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR}, 
11       #{receiverZip,jdbcType=VARCHAR}, now(), now()
12       )
13   </insert>
View Code

普通完整例子如下:

 1 <?xml version="1.0" encoding="UTF-8" ?>   
 2 <!DOCTYPE mapper   
 3 PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
 4 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 
 5 
 6 <mapper namespace="com.dy.dao.UserDao">
 7    
 8    <!-- 对应userDao中的insertUser方法,  -->
 9    <insert id="insertUser" parameterType="com.dy.entity.User">
10            insert into user(id, name, password, age, deleteFlag) 
11                values(#{id}, #{name}, #{password}, #{age}, #{deleteFlag})
12    </insert>
13    
14    <!-- 对应userDao中的updateUser方法 -->
15    <update id="updateUser" parameterType="com.dy.entity.User">
16            update user set name = #{name}, password = #{password}, age = #{age}, deleteFlag = #{deleteFlag}
17                where id = #{id};
18    </update>
19     
20    <!-- 对应userDao中的deleteUser 方法 --> 
21    <delete id="deleteUser" parameterType="com.dy.entity.User">
22            delete from user where id = #{id};
23    </delete>
24 </mapper>
View Code

仔细观察上面parameterType,  "com.dy.entity.User",可以用上别名,在mybatis 的全局配置文件(我这儿名字是mybatis-conf.xml), 不要认为是在mapper的配置文件里面配置哈。

1   <typeAliases>
2       <!--
3       通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,
4       并且默认设置一个别名,默认的名字为: javabean 的首字母小写的非限定类名来作为它的别名。
5       也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(user) 
6       <package name="com.dy.entity"/>
7        -->
8       <typeAlias alias="user" type="com.dy.entity.User"/>
9   </typeAliases>
View Code

一对多,多对多关系配置:

 1 <!-- 
 2         1.type 对应类型,可以是javabean, 也可以是其它
 3         2.id 必须唯一, 用于标示这个resultMap的唯一性,在使用resultMap的时候,就是通过id指定
 4      -->
 5     <resultMap type="" id="">
 6     
 7         <!-- id, 唯一性,注意啦,这个id用于标示这个javabean对象的唯一性, 不一定会是数据库的主键(不要把它理解为数据库对应表的主键) 
 8             property属性对应javabean的属性名,column对应数据库表的列名
 9             (这样,当javabean的属性与数据库对应表的列名不一致的时候,就能通过指定这个保持正常映射了)
10         -->
11         <id property="" column=""/>
12         
13         <!-- result与id相比, 对应普通属性 -->    
14         <result property="" column=""/>
15         
16         <!-- 
17             constructor对应javabean中的构造方法
18          -->
19         <constructor>
20             <!-- idArg 对应构造方法中的id参数 -->
21             <idArg column=""/>
22             <!-- arg 对应构造方法中的普通参数 -->
23             <arg column=""/>
24         </constructor>
25         
26         <!-- 
27             collection,对应javabean中容器类型, 是实现一对多的关键 
28             property 为javabean中容器对应字段名
29             column 为体现在数据库中列名
30             ofType 就是指定javabean中容器指定的类型
31         -->
32         <collection property="" column="" ofType=""></collection>
33         
34         <!-- 
35             association 为关联关系,是实现N对一的关键。
36             property 为javabean中容器对应字段名
37             column 为体现在数据库中列名
38             javaType 指定关联的类型
39          -->
40         <association property="" column="" javaType=""></association>
41     </resultMap>
View Code

一个student对应多个course, 典型的一对多,咱们就来看看mybatis怎么配置这种映射吧:

studentDao.xml:

 1 <mapper namespace="com.dy.dao.StudentDao">
 2 
 3     <!-- 这儿定义一个resultMap -->
 4     <resultMap type="student" id="studentMap">
 5     
 6         <!-- 
 7             数据库中主键是id, 但是我这儿却是指定idCard为主键,为什么? 
 8             刚刚讲了,id用来表示唯一性, 我们可以认为只要idCard一样,那么他就是同一个学生。
 9             如果此处用数据库中id, 那么mybatis将会认为数据库中每条记录都是一个student, 这显然不符合逻辑
10         -->
11         <id property="idCard" column="stu_id_card"/>
12         <result property="id" column="stu_id"/>
13         <result property="name" column="stu_name"/>
14         <result property="deleteFlag" column="stu_delete_flg"/>
15         
16         <!-- 
17             这儿就是实现一对多的关键。 
18             在Student中,courseList为List<Course>, 因此,ofType也应该与之对应(当然,我用了别名,不然要蛋疼的写全名了)。
19             collection的子标签是在指定Course的映射关系(由于Course的javabean的属性名与数据库的列名不一致)
20         -->
21         <collection property="courseList" column="stu_course_id" ofType="Course">
22             <id property="id" column="course_id"/>
23             <result property="name" column="course_name"/>
24             <result property="deleteFlag" column="course_delete_flg"/>
25         </collection>
26     </resultMap>
27     
28     <!-- 这儿将返回类型设置成了上面指定的studentMap -->
29     <select id="findStudentById" resultMap="studentMap">
30         SELECT s.*, c.* FROM t_student s LEFT JOIN t_course c ON s.stu_course_id=c.course_id WHERE s.stu_id_card=#{idCard}
31     </select>
32     
33 </mapper>
View Code

讲解很详细:http://www.cnblogs.com/dongying/p/4092662.html

补充foreach标签例子:

View Code

官方mybatis配置相关:http://www.mybatis.org/mybatis-3/zh/configuration.html

原文地址:https://www.cnblogs.com/cing/p/7809684.html