极客营框架教学之mybatis.docx

极客营框架教学之mybatis剖析

极客营框架教学之mybatis mybatis快速入门(环境搭建) 参考程序:201MyBatis_helloworld 主要演示环境搭建过程,实现基本的查询操作。 重点讲解mapper文件中的select标签的id,parameterTyep,resultType属性。 重点讲解conf.xml中的相关配置。 建库建表。 创建实体类,主要属性名和表的字段对应。 添加jar文件。 添加mybatis的配置文件conf.xml。 添加studentMapper.xml配置文件。 编写测试类。 Mybatis实现基本的增删改查(xml) 参考程序:202MyBatis_curd 主要讲解简单的增删改查操作,注意增删改操作的parameterType。 当parameterType为一个实体类型时,#{属性名称}。 返回值为一个集合时,resultType只需要指明集合中的类型即可,不需要指明集合。 Sql映射文件:studentMapper.xml ?xml version=1.0 encoding=UTF-8? !DOCTYPE mapper PUBLIC -////DTD Mapper 3.0//EN /dtd/mybatis-3-mapper.dtd !-- mapper标签的namespace相当于id是用来唯一表示当前的mapper。 一般为了避免重复都采用:包名.文件名 -- mapper namespace=com.igeekhome.mybatis.po.studentMapper !-- 配置查询的sql语句,用来实现通过id查询学生信息 1、属性id 用来唯一表示当前select标签。 2、属性parameterType 用来表明sql语句所需要的参数类型。 3、属性resultType 用来表明查询之后组装的对象类型。这里要注意,即使这里返回一个集合,也要写集合中的实体类数据类型,而不能写集合类型。 4、sql语句中使用 #{xx} 作为参数的占位符 -- select id=getStuByID parameterType=int resultType=com.igeekhome.mybatis.po.Student select * from student where stuId = #{id} /select !-- 保存用户的slq -- insert id=save parameterType=com.igeekhome.mybatis.po.Student insert into Student(stuName,stuAge) values(#{stuName},#{stuAge}) /insert !-- 编辑用户的sql -- update id=update parameterType=com.igeekhome.mybatis.po.Student update student set stuname=#{stuName},stuAge=#{stuAge} where stuId = #{stuId} /update delete id=delete parameterType=int delete from student where stuId = #{stuId} /delete select id=selectAll resultType=com.igeekhome.mybatis.po.Student select * from student /select /mapperConf.xml文件中添加mapper mappers !-- 注意这里的resource 属性中写的文件路径,而不是类名 -- mapper resource=com/igeekhome/mybatis/po/studentMapper.xml/ /mappersMybatis实现基本的增删改查(annotation) 参考程序:203MyBatis_noImpl mybatis可以在接口的方法上方注解sql语句,这样就免于编写mapper文件。而且不需要编写dao的实现类,mybatis会自动生成对应的实现类。 具体操作如下: 编写IStudentDAO接口。 在对应的方法上方添加对应的sql注解。 mybatis会自动匹配对应的参数和返回值。 具体代码如下: package com.igeekhome.mybatis.dao; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.

文档评论(0)

1亿VIP精品文档

相关文档