(说明:ctrl+p是查看函数参数,ctrl+h查看接口实现类)
mvc---web开发架构模型,m是数据,v是视图,c是控制器
==三层架构==:界面层(jsp,serverlet,html,controller包),业务逻辑层(service调用dao)、数据访问层(增删改查,dao)
三层中类的交互:界面层-->业务逻辑层-->数据访问层(持久层)-->数据库(mysql)
三层对应的处理框架:
- 界面层---servlet--springmvc
- 业务逻辑层--service--spring
- 数据访问层--dao--mybatis
mybatis的动态代理:就是内部对dao接口进行类实现
多个参数:
使用@Param
使用对象
按位置(了解)
使用Map(了解)
$和#的区别
#是占位符 $底层是字符串拼接,可能会出现sql注入:==id=+"'李四'";drop table student if exists;==
resultType的值,1、类型的全限定名称 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>
Map (用的比较少)
返回Map,列名是map的key,列值是map的value
返回Map最多只能返回一行记录,多于一行就会错误
指定列名和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不要一起用,二选一
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。