文章骨架:
1、resultType的用法
表示sql语句执行的结果,转为Java对象的类型
1) 返回java对象(没有限制,也可以新建一个对象,把结果封装为这个对象)
<select id="selectStudentByMap" resultType="com.mybatis.domain.Student">
select id,name,email,age from student where age = #{mapage} or id = #{mapid}
</select>
2)如果返回值是需要Java类型(包括简单类型)的值:
1)起别名 (mybatis规定好的别名)
<select id="selectCount" resultType="int">
select count(*) from student
</select>
java简单类型别名对照表:
2)全限定名称; 推荐全限定名称
- 全限定名称:
<select id="selectCount" resultType="java.lang.Integer">
select count(*) from student
</select>
3)**自定义的类型,**定义类型使用别名(推荐全限定名称):
在mybatis主配置文件中定义,使用定义别名
typeAliases定义别名:
- typeAlias :可以指定一个类型一个自定义的别名
语法:- type:自定义类型的全限定名称
- alias:别名
mybatis主配置文件中的配置:
<!--自定义别名-->
<typeAliases>
<!--type:你想要起别名的Java数据类型的全限定名称
alias:别名-->
<typeAlias type="com.mybatis.damain.Student" alias="Student"></typeAlias>
</typeAliases>
sql映射文件:
<!--起别名-->
<select id="selectStudentByOtherName" resultType="Student">
select id,name,email,age from student where id = #{studentid}
</select>
处理方式:
1、mybatis执行sql语句,然后mybatis调用类的无参构造方法,创建对象
2、mybatis把ResultSet指定列值托付给同名的属性
mybatis与jdbc的对等操作:
<select id="selectStudentByMap" resultType="com.mybatis.domain.Student">
select id,name,email,age from student where age = #{mapage} or id = #{mapid}
</select>
用jdbc:的实现
ResultSet rs = executeQuery("select id,name,email,age from student where age = #{mapage} or id = #{mapid} ");
while(rs.next){
student student = new Student();
student.setId(rs.getInt(id));
….
}
- 使用package
这个包中的所有类的别名就是类名(类名不区分大小写,也不用管包中有多少类,类名就是别名)
属性意义:
name:该类所在的包的全限定名称
<typeAliases>
<package name="com.mybatis.damain"></package>
</typeAliases>
- 查询结果返回Map集合
注意:返回map集合时只能查询一个结果
2、resultMap 结果映射,指定列名和Java对象的属性对应关系
1、你自定义列值赋值给哪个属性
-
resultType使用map时 默认赋值给同名的属性
-
resultMap可以自定义赋值
1、使用resultMap步骤:1)先定义redultMap 2)使用select标签,使用redultMap来引用1的定义
2、 定义redultMap
id:表示你定义的这个resultMap,方便使用select标签时引用这个resultMap type:java类型的全限定名称
3、 列名和Java属性的关系
主键列:使用id标签 column:表示列名 property:Java类型的属性名 非主键类:使用result
<resultMap id="MyStudentId" type="com.mybatis.damain.MyStudent">
<!--
<id>:主键使用
<result>:非主键使用
column:列名
property:属性名
-->
<id column="id" property="stuid"></id>
<result column="name" property="stuname"></result>
</resultMap>
<!--列名与属性名不一致-->
<select id="selectMyStudent" resultMap="MyStudentId">
select id,name,email,age from student where age > #{age}
</select>
2、当列名和属性名不一样时,解决方案
-
使用resultMap
-
sql语句中使用别名
<!--列名与属性名不一致-->
<select id="selectMyStudent" resultMap="MyStudentId">
select id as stuid,name as stuname,email as stuemail,age from student where age > #{age}
</select>
3、模糊查询,Like的使用
1、在Java代码中指定like的内容
//模糊查询
@Test
public void selectStudentlike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> l = studentDao.selectStudentlike("%l%");
l.forEach(stu -> System.out.println(stu));
}
<!--模糊查询-->
<select id="selectStudentlike" resultType="Student">
select id,name,email,age from student where name like #{name}
</select>
2、xml文件中进行拼接:
<!--模糊查询 xml文件中进行拼接-->
<select id="selectStudentLinke" resultType="Student">
select * from student where name like "%" #{name} "%"
</select>
//模糊查询
@Test
public void selectStudentLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> l = studentDao.selectStudentLinke("l");
l.forEach(stu -> System.out.println(stu));
}