在映射文件中,mapper元素是映射文件的根元素,其他元素都是它的子元素。
1.select元素
select元素负责查询语句,它可以帮助我们从数据库中读取数据,并且组装数据返回
<select id="findCustomerById" parameterType= " Integer" resultType="com.itheima.po .Customer">
select * from t customer where id = #{id}
</select>
2 insert元素
insert元素用于映射插入语句,在执行元素定义的SQL语句后,会返回一个表示插入记录数的整数
<insert id="insertAccount" parameterType="com.wx.domain.Account">
insert into account values(sys_guid(),#{username},#{balance})
</insert>
insert元素的配置示例如下:
<insert id=" addCustomer" parameterType= " com . itheima.po .Customer" flushCache= "true" statementType="PREPARED" keyProperty="" keyColumn="" useGenerateKeys="" timeout="20">
insert 元素的属性与select元素的属性大部分相同,但还包含3个特有的属性。
如果使用的数据库支持主键自动增长(如 MySQL ,那么可以通过 keyProperty 属性指定PO 类的某个属性接收主键返回值 通常会设置到 id 属性上 ,然后将 useGeneratedKeys 的属 性值设置为 true,其使用示例如下:
<insert id=" addCustomer " parameterType=" com . itheima . po.Customer" keyProperty="id" useGeneratedKeys="true" >
insert into customer (username , jobs pho e) values(#{username} , #{jobs} ,#{phone})
</insert>
使用上述配置执行插入后,会返 插入成功的行数,以及插入行的主键值
public void addCustomerTest() {
//获取 SqlSession
Customer customer = new Customer()i
customer.setUsername( " rose") ;
customer.setJobs (" student ");
customer .setPhone( " 13333533092 " ) ;
int rows = sq Session nsert("com.itheima.mapper.CustomerMapper.addCustomer " , customer) ;
//输出插入数据的主键 id
System.out.println(customer.getld()) ;
SqlSession sqlSession = MybatisUtils.getSession() ;
if (rows > 0) {
System out println( 您成功插入了 +rows+" 条数据 ");
}else{
System out .p rintln( 执行插入操作失败! ! ! ");
}
sqlSession.commit();
sqlSession.close();
}
如果使用的数据库不支持主键自动增长(如 Oracle ),或者支持增长的数据库取消了主键自增的规则时,也可以使用 MyBatis 提供的另一种方式来自定义生成主键,具体配置示例如下
<insert id=" insertCustomer " parameterType="com.itheima.po.Customer">
<selectKey keyProperty="id" resultType="Integer" order="BEFORE">
select if(max(id) is null , 1 , max(id) +1) as newld from customer
</selecKey>
insert into t_customer(id, username , jobs, phone) values(#{id},#{username} , #{jobs} , #{phone})
</insert>
在上述代码中,selectKey元素会首先运行,他会通过自定义的语句来设置数据表中的主键(如果customer表中没有数据,则将id设置为1,否则就将id的最大值加1,来作为新的主键),然后再调用插入语句。
<selectKey> 元素在使用时可以设置以下几种属性:
<selectKey keyProperty="id" resultType="Integer" order="BEFORE" statementType="PREPARED">
在selectKey属性中,KeyProperty(该元素中的属性对应javaBean类中的元素,也就是你要从数据库中查询的键值对应的java类的属性)、resultType和statementType的作用和前面的相同,这里order可以设置为:BEFORE或者AFTER
3 update和delete元素
<update id="update" parameterType="com.wx.domain" flushCache="true" statementType = "PREPARED" timeout = "20">
<delete id="update" parameterType="com.wx.domain" flushCache="true" statementType = "PREPARED" timeout = "20">
4. sql 元素
在一个映射文件中,通常会执行多条sql语句,这个时候就会出现相同的代码,这些代码大多重复,为了解决代码重复的问题,使用sql元素来解决。
定义sql代码片段,在其他语句中引用这一代码片段
<sql id = "sqlAccount">id,username,jobs,phone</sql>
<select id = "findAccountLikeName" resultType="com.wx.domain.Account" parameterType="String">
select <include refid="sqlAccount"/> from account where username like #{username}
</select>
5. resultMap元素
resultMap主要是定义映射规则,定义一对一、一对多和多对多
在之前使用mybatis查询数据库的时候,将查询的数据与返回的对象的属性进行匹配赋值。(这就要求数据库中的字段名必须和对象中的属性名相同)。但实际开发中,不可能完全一致,这个时候就需要resultMap出手了。
<resu1tMap type="" id="">
<constructor> <!一类在实例化时,用来注入结果到构选}j法中一〉
<idArg/> <!-- 10 参数;标记结果作为 10-->
<arg/> <!一注入到构造方法的 个普通结果一〉
</constructor>
<id/> <!一用于表示哪个列是主键一〉
<resu1t/> <!一注入到字段或 JavaBean 属性的普通结果一〉
<association property="" /> <!一用于一对一关联一〉
<collection property="" /> <!一用于一对多关联一〉
<discriminator javaType=""> <!一使用结果值米决定使用哪个结果映射一〉
<case value="" /> <!一基于某些值的结果映射一〉
</discriminator>
</resultMap>
resultMap元素的type属性表示需要映射的POJO,id属性是这个resultMap的唯一标识。它的子元素<constructor>用于配置构造方法(当一个pojo对象未定义无参的构造方法时,就可以使用construcotr元素进行配置)。子元素id用于表示哪个列是主键,result用于表示pojo和数据中普通列的映射关系。<association> 和 <collection> 用于处理多表时的关联关系,discriminator元素主要用于处理一个单独的数据库查询返回很多不同数据类型结果集的情况