Mybatis的出现

针对上期所说JDBC存在的问题,这期讨论Mybatis。

Mybatis是对JDBC进行封装的一个框架,它能避免所有JDBC代码和手动设置参数以及获取结果集,避免了硬编码的局限性。它将更多的关注点放在SQL上,通过Mapper.xml配置文件,将java中的POJO(普通的java对象,实际上就是Javabean,里面有属性和getter和setter方法的类,没有业务逻辑)与数据库表中字段进行ORM映射。将代码和sql语句进行分离解耦,提高了可维护性,而且将sql放在xml文件中便于统一管理和优化,不必像之前写在java文件中重新编译执行。但是我们都知道,每款框架产品都是要通过xml等配置文件进行配置与管理。Mybatis也不例外,它有两个配置文件mybatis-config.xml和*Mapper.xml,其中mybatis-config.xml是mybatis的全局配置文件,用于配置数据源,事务管理设置实体类别名等,*Mapper.xml就是进行ORM映射的映射文件,在这里实现数据库表字段和对象实体映射,对各个表进行数据库操作,包括对单表、多表操作。

介绍*Mapper.xml的常用标签及属性:

1.select标签     (select常用属性:id\parametType\resultType\resultMap  )

2.resultMap标签(id、type)配合association标签(两表是一对一关系,常用属性有property \javaType\colum)和collection标签(两表是一对多、多对多关系,常用属性有property \ofType\colum)进行多表关联操作

3.sql标签(id)用来定义可重用的sql代码片段

4.insert update delete标签(常用属性:id\parameterType\flushCache\statementType)

示例:

(1)含select标签     (select常用属性:id\parametType\resultType\resultMap  )

通过id查找用户:

<mapper namespace=test>
    <select id="getById" parameterType="int" resultType="com.ssm.bean.User">
select * from user where id=#{id}
    </select>
</mapper>

namespace:就是对sql进行分类化管理,理解sql隔离

id:标识映射文件中的sql

parameterType="int":指定输入参数的数据类型,例如从前台接收的参数是什么类型parameterType就对应什么类型

resultType="com.ssm.bean.User":指定sql输出结果所映射的java对象类型,就是经过查询操作后,返回数据要以什么类型返回

select * from user where id=#{id}:是查询语句,与普通sql语句一样写法,只是这里不用占位符?而是用#{}就相当于一个占位符,之前我们在jdbc中java文件中写sql语句是string sql=select * from user where id=?PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, id);

这里说下#{}和${}:

#{}:用于接收输入参数的内容而${}表示拼接sql串,将接收到的参数不加修饰的直接拼接在sql中,容易引起sql注入

(2)resultMap标签(id、type)配合级联查询association标签(两表是一对一关系,常用属性有property \javaType\colum)和collection标签(两表是一对多、多对多关系查出来的结果是集合,常用属性有property \ofType\colum)进行多表关联操作

示例:

<resultMap id="userResultMap" type="User">

<id property="id" column="user_id" />

<result property="username" column="username"/>

<result property="password" column="password"/>

</resultMap>

引用它的语句使用 resultMap 属性就行了

<select id="selectUsers" resultMap="userResultMap">

select user_id, user_name, hashed_password from some_table where id = #{id}

</select>

<resultMap id="给ID名" type="实体类" >
<result column="数据库字段名" property="实体类属性名" jdbcType="数据库字段类型" />
</resultMap>

<resultMap type="student" id="studentMap">    

  <id property="id" column="id"/>  

    <result property="name" column="name"/>

    <result property="score" column="score"/>  

    <!-- 映射classes属性 -->  

    <association property="classes" javaType="com.ssm.bean.Classes">      

    <id property="id" column="c_id"/>        

  <result property="name" column="c_name"/>  

    </association>

</resultMap>  

<select id="getById" parameterType="int" resultMap="studentMap">  
select * from student as s,classes as c where s.cid = c.c_id and s.id = #{id};
----------------------------------------------------------------------------------------------------------------‘

<resultMap type="classes" id="classesMap">  

<id property="id" column="c_id"/>  

<result property="name" column="c_name"/>  

<!-- 映射students属性 -->  

<collection property="students" ofType="student">      

    <id property="id" column="id"/>    

      <result property="name" column="name"/>    

    </collection>

</resultMap>  

<select id="getById" parameterType="int" resultMap="classesMap">  

select * from classes as c,student as s where c.c_id = s.cid and c.c_id = #{id}; </select>’

(3)sql标签(id)用来定义可重用的sql代码片段

<mapper namespace="dulk.learn.mybatis.dao.GirlDao"> 

<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">

      <id property="id" column="id" />

    <result property="age" column="age" />

    <result property="cupSize" column="cup_size" />

</resultMap>

<!--定义可重用的sql片段-->

    <sql id="girlColumn">

    id, age

    </sql>

  <select id="findById" parameterType="long" resultMap="girlResultMap">

        <!--通过include引用外部sql片段-->

    SELECT <include refid="girlColumn" />

        FROM girl WHERE id = #{id}

    </select>

</mapper>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值