一、参数传入:从java代码中把数据传入到mapper文件的sql语句中。
1)parameterType : 写在mapper文件中的 一个属性。 表示dao接口中方法的参数的数据类型。
它的值是java的数据类型权限定名称或mybatis定义的别名 。
可以写可以不写,一般不写,因为mybatis通过反射可以获取参数的数据类型。
2) 一个简单类型的参数:
简单类型:mybatis把java的基本数据类型和String都叫简单类型。
在mapper文件获取简单类型的一个参数的值,使用 #{任意字符}
接口:public Student selectStudentById(Integer id)
mapper:select id,name, email,age from student where id=#{studentId}
3) 多个参数,使用@Param命名参数
接口 public List<Student> selectMulitParam(@Param("myname") String name, @Param("myage") Integer age)
mapper文件:
<select>
select * from student where name=#{myname} or age=#{myage}
</select>
4) 多个参数,使用java对象
语法 #{属性名}
mapper文件:
<select>
select id,name,email,age from student where name=#{paramName}or age=#{paramAge}
</select>
二、结果集返回
1)列名和属性名一样
sql语句:select id ,name,email,age from student
2)列名和属性名不一样
1.取别名:
sql语句:select id as stuid,name as stuname,email as stu email,age as stuage from student
列名->属性名
id -> stuid name -> stuname email -> email age -> stuage
2.使用resultMap
①定义resultMap
②在select标签,使用resultMap来引用 column:Map列名 property:java类型的属性名
<resultMap id="studentMap" type="com.yuchang.domain.Student">
<!--主键用id标签-->
<id column="id" property="id"></id>
<!--其他用result-->
<result column="name" property="name"></result>
<result column="age" property="age"></result>
<result column="email" property="email"></result>
</resultMap>
<select id="selectAllStudents" resultMap="studentMap">
select id,name,email,age from student
</select>
三、resultType取别名
<!--定义resultType别名
1.typeAliases
type;全限定名称
alias:自定义别名
2.package
name:指定一个包包中的类名就是别名
-->
<typeAliases>
<!--<typeAlias type="com.yuchang.domain.Student" alias="stu"/>-->
<package name="com.yuchang.domain"/>
</typeAliases>