1 Star 2 Fork 0

王泽熙/java学习笔记_markdown

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mybatis.md 4.97 KB
一键复制 编辑 原始数据 按行查看 历史
王泽熙 提交于 3年前 . new

框架概述

(说明:ctrl+p是查看函数参数,ctrl+h查看接口实现类)

mvc---web开发架构模型,m是数据,v是视图,c是控制器

==三层架构==:界面层(jsp,serverlet,html,controller包),业务逻辑层(service调用dao)、数据访问层(增删改查,dao)

三层中类的交互:界面层-->业务逻辑层-->数据访问层(持久层)-->数据库(mysql)

三层对应的处理框架:

  • 界面层---servlet--springmvc
  • 业务逻辑层--service--spring
  • 数据访问层--dao--mybatis
image-20220111154751057 image-20220111155503069 image-20220111155617633 image-20220111155733156

Mybatis框架快速入门

image-20220111162334492 image-20220111213134498 image-20220112170255354 image-20220112211606382

mybatis的动态代理:就是内部对dao接口进行类实现

Mybatis框架Dao代理

深入理解参数

多个参数:

  1. 使用@Param

    image-20220116174102775
  2. 使用对象

  3. 按位置(了解)

    image-20220116182412700
  4. 使用Map(了解)

    image-20220116184807747
  5. $和#的区别

    image-20220116190931501 image-20220116192234498

    #是占位符 $底层是字符串拼接,可能会出现sql注入:==id=+"'李四'";drop table student if exists;==

封装MyBatis输出结果

resultType

resultType的值,1、类型的全限定名称 2、类型的别名

  1. 简单类型

    image-20220116200041809
  2. 对象类型

    resultType指结果类型,指sql语句执行完毕后,数据转化为的java对象

    ==在mybatis主配置文件中定义,使用定义别名==,之后就可以在resultType中直接使用别名

    方式1、

    <typeAliases>
        <!--type是全限定名称  alias是别名-->
        <typeAlias type="com.bjpowernode.domain.Student" alias="Student"></typeAlias>
    </typeAliases>
    

    方式2、(让对应的类名称为别名)

    <typeAliases>
        <package name="com.bjpowernode.domain"/>
        <package name="com.bjpowernode.vo"/>
    </typeAliases>
    
  3. Map (用的比较少)

返回Map,列名是map的key,列值是map的value

返回Map最多只能返回一行记录,多于一行就会错误

resultMap(结果映射)

指定列名和java对象的属性之间的对应关系,以下几种情况需要使用resultType

  • 如果我偏要把id给name属性呢,就要用着==结果映射==
  • 列名属性名不同的时候需要使用

==在mapper文件中的配置==

<!--如何使用resultMap
	1)先定义resultMap
	2)在select标签,使用resultMap来引用1)定义的
-->
<resultMap id="studentMap" type="com.bjpowernode.domain.Student">
<!--标签内 定义列和java对象属性的关系-->
<!--column是列名(sql的)
	property是属性名(java对象的)
	主键列使用id标签
	对于非主键列使用result标签
-->
   这是第一种方式 
	<id column="id" property="id"></id>
    <result column="name" property="name"></result>
    <result column="email" property="email"></result>
    <result column="age" property="age"></result>
</resultMap>
<select id="selectAllStudent" resultMap="studentMap">
	select id,name,email,age from student
</select>

	这是第二种方式:在sql语句中使用别名
 <select id="selectAllStudent" resultMap="studentMap">
	select id as 名,name as 名,email as 名,age as 名 from student
  </select>
	
//测试程序
class Test{
    @Test
    public void selectAllStudentTest(){
        SqlSession sqlSession=MyBatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);
        List<Student> students=dao.selectAllStudent();
        students.forEach(student -> System.out.println(student));
    }
}

resultType和resultMap不要一起用,二选一

Like模糊查询

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzx1914/java-learning-notes-markdown.git
git@gitee.com:wzx1914/java-learning-notes-markdown.git
wzx1914
java-learning-notes-markdown
java学习笔记_markdown
master

搜索帮助