1.什么是框架?

        软件的半成品,解决了软件开发过程中的普适性问题,我们可以在此基础上快速开发公司定制的化的软件应用

        特点:简单易学,灵活,开发速度快,提高软件质量 框架已经把关键的代码业务实现,我们只是站在巨人肩膀上

2.什么是ORM框架?

        ORM(Object Relational Mapping ) 对象关系映射,将程序中的一个对象与表中的一行数据一一对应

        读取:将数据库表中的每一行数据映射为对象

        写入:将java对象映射为数据库中的每一行数据

        mybatis就是一个orm框架,用来取代JDBC,queryRunner等

3. mybatis与hibernate特点

        mybatis:持久层和orm框架

        特点:

             1)简单易用      2)灵活      3)sql与jiava代码解耦

             4)提供高级映射功能 : 数据库列表和java属性不一致问题

                                                一对一、一对多关系

             5)动态sql语句,解决模糊查询等类似问题

         hibernate:持久层框架,已经渐渐被淘汰

         特点:功能强大,重量级,不需要写sql语句(开发者不能自定义sql语句,不能优化,不灵活)

4.持久层配置文件XXXDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--
namespace="com.wp.dao.IStudentDao"  和mybatis二级缓存相关
mybatis 根据当前xml文件生成IStudentDao对应的实现类
-->
<mapper namespace="com.wp.dao.IStudentDao">

    <!--
        id 对应dao中的方法名
        #{id} 获取方法传来的参数
        resultType : 返回值类型
        parameterType
    -->
    <!--<select id="findStudentById" resultType="com.wp.entity.Student" parameterType="int">
        select * from student_tb where id = #{id}
    </select>-->

    <select id="findAllStudent" resultType="com.wp.entity.Student">
        select * from student_tb
    </select>

</mapper>

5.mybatis配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        <!--单个类 起别名-->
       <!-- <typeAlias type="com.wp.entity.Student" alias="student"></typeAlias>-->

        <!--
            为当前包下面所有实体类 取别名 不区分大小写
            Student 别名 Student student sTudent 不区分大小写
        -->
        <package name="com.wp.entity"/>
    </typeAliases>


    <!--选择mysql环境-->
    <environments default="mysql">
        <!--配置mybatis环境-->
        <environment id="mysql">
            <!--事务管理-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置连接池数据源-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/qf2006"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>

    <!--mappers一定要在environments之后-->
    <mappers>
        <!--在配置文件声明IStudentDao,只有声明了,mybatis才会根据IStudentDao.xml创建实现类-->
        <mapper resource="com/wp/dao/IStudentDao.xml"></mapper>
    </mappers>
</configuration>

6.获取自增id的两种方式:

      1.使用selectKey方式获取

 <!--
        第一种:插入学生 获取id
        selectKey 获取自增id
        resultType="int" 自增id 类型
        keyColumn:对应数据库表自增主键列名
        keyProperty:传入的实体类中 主键对应的的属性
        order="AFTER" 插入完成之后执行   select last_insert_id()
    -->
    <insert id="addStudentGetId" parameterType="com.qfedu.entity.Student">
        insert  into student_tb(name,age,sex,height) values(#{name},#{age},#{sex},#{height})
        <selectKey resultType="int" keyColumn="id" keyProperty="id" order="AFTER">
            select last_insert_id()
        </selectKey>

        2.useGeneratedKeys="true" 设置使用自增主键;

   <!--
        第二种获取自增id
        useGeneratedKeys="true"  获取自增id
        将数据库表自增id  keyColumn="id" 设置到student对象的属性id  keyProperty="id"
    -->
    <insert id="addStudentGetId" parameterType="Student" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        insert  into student_tb(name,age,sex,height) values(#{name},#{age},#{sex},#{height})
    </insert>

7.模糊查询两种方式

 List<Student> studentList = iStudentDao.getStudentByLikeName("%小%");
 for (Student student:studentList){
    System.out.println("student:"+student);
}

方式一: #{name}

  <!--
        模糊查询
        #{name}  占位符  只是替换值  禁止sql 拼接  常用
    -->
    <select id="getStudentByLikeName" resultType="com.qfedu.entity.Student">
        select * from student_tb where name like #{name}
    </select>

方式二:

<!--
        '${value}'  mybatis 字符串的替换拼接
        ${value} 获取到值 sql 字符串会拼接进来  最大的风险是sql注入
       '${value}'  select name from student_tb  where id = 1
    -->
    <select id="getStudentByLikeName" resultMap="studentMap1">
        select * from student_tb where name like '${value}'
    </select>

8.面试题:${} #{} 区别????

  • 获取值方式不同  ${value}   #{参数名}

  • ${}字符串替换,然后sql拼接   #{}占位符

  • ${} 有sql注入风险

9.别名的配置

typeAliases标签,写在mybatis配置文件最上方位置

别名就是mybatis 给 Java实体类起一个小名(简短的名字,而不需要全限定名)

<typeAliases>
        <!--单个类 起别名-->
       <!-- <typeAlias type="com.qfedu.entity.Student" alias="student"></typeAlias>-->

        <!--
            为当前包下面所有实体类 取别名 不区分大小写
            Student 别名 Student student sTudent 不区分大小写
        -->
        <package name="com.qfedu.entity"/>
    </typeAliases>

10.resultMap属性与列名不匹配解决方式

 <!--
       定义一个resultMap
       使用resultMap 完成 数据库 列名到 实体类的映射
       <id property="id" column="id"></id> 实体类属性id 到数据库主键i列名d的映射
       <result property="name" column="name"></result>实体类属性和数据库普通列名的映射
    -->
    <resultMap id="studentMap1" type="Student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="sex" column="sex"></result>
        <result property="height" column="height"></result>
        <result property="address" column="s_address"></result>
    </resultMap>
    <select id="findStudentById" resultMap="studentMap1" parameterType="int">
        select * from student_tb where id = #{id}
    </select>

 

posted on 2020-10-14 22:48  zitian246  阅读(52)  评论(0编辑  收藏  举报