1、别名
(1)
在mapper文件中如果resultType返回值是整型等
方法1:
<select id="selectMultiParam" resultType="java.lang.Integer">
select count(*) from student
</select>
方法2:
<select id="selectMultiParam" resultType="int">
select count(*) from student
</select>
(2)
如果自定义别名
在MyBatis 主配置文件添加
<typeAliases>
<!--
第一种方式:
可以指定一个类型一个自定义别名
type:自定义类型的全限定名称
alias:别名(短小,容易记忆的)
-->
<typeAlias type="com.bjpowernode.domain.Student" alias="stu" />
<!--
第二种方式
<package> name是包名, 这个包中的所有类,类名就是别名(类名不区分大小写)
-->
<package name="com.bjpowernode.domain"/>
</typeAliases>
则mapper文件变为
<!--
方法一
-->
<select id="selectMultiParam" resultType="stu">
select id,name, email,age from student where name=#{myname} or age=#{myage}
</select>
<!--
方法二
-->
<select id="selectMultiParam" resultType="Student">
select id,name, email,age from student where name=#{myname} or age=#{myage}
</select>
2、返回map
在Dao接口中写一个方法
Map<Object,Object> testSelecMap(Integer id);
则mapper文件变为
<!--1)列名是map的key, 列值是map的value
2)只能最多返回一行记录。多余一行是错误
-->
<select id="selectMultiParam" resultType="java.util.HashMap">
select id,name, email,age from student where name=#{myname} or age=#{myage}
</select>
在测试类中实现方法
@Test
public void testSelecMap(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Map<Object,Object> map = dao.selectMapById(1001);
System.out.println("map=="+map);
}
3、使用resultMap
mapper文件变为
<!--使用resultMap
1)先定义resultMap
2)在select标签,使用resultMap来引用1定义的。
id:自定义名称,表示你定义的这个resultMap
type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.bjpowernode.domain.Student">
<!--主键列,使用id标签
column :列名
property:java类型的属性名
-->
<id column="id" property="id" />
<!--非主键列,使用result-->
<result column="name" property="name" />
<result column="email" property="email" />
<result column="age" property="age" />
</resultMap>
<select id="selectAllStudents" resultMap="studentMap">
select id,name, email , age from student
</select>
4、列名和属性名不一致
方法一:
mapper文件变为
<resultMap id="myStudentMap" type="com.bjpowernode.domain.MyStudent">
<!--列名和java属性的关系-->
<id column="id" property="stuid" />
<!--非主键列,使用result-->
<result column="name" property="stuname" />
<result column="email" property="stuemail" />
<result column="age" property="stuage" />
</resultMap>
<!--列名和属性名不一样:第一种方式-->
<select id="selectMyStudent" resultMap="myStudentMap">
select id,name, email , age from student
</select>
方法二:
mapper文件变为
<!--列名和属性名不一样:第二种方式
resultType的默认原则是 同名的列值赋值给同名的属性, 使用列别名(java对象的属性名)
-->
<select id="selectDiffColProperty" resultType="com.bjpowernode.domain.MyStudent">
select id as stuid ,name as stuname, email as stuemail , age stuage from student
</select>
5、like模糊查询
dao接口定义方法:
/*第一种模糊查询, 在java代码指定 like的内容*/
List<Student> selectLikeOne(String name);
/*name就是李值, 在mapper中拼接 like "%" 李 "%" */
List<Student> selectLikeTwo(String name);
则mapper文件变为
<!--第一种 like , java代码指定 like的内容-->
<select id="selectLikeOne" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name like #{name}
</select>
<!--第二种方式:在mapper文件中拼接 like的内容-->
<select id="selectLikeTwo" resultType="com.bjpowernode.domain.Student">
select id,name,email,age from student where name like "%" #{name} "%"
</select>
测试类改为
@Test
public void testSelectLikeOne(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//准备好like的内容
String name = "%李%";
List<Student> students = dao.selectLikeOne(name);
for(Student stu: students){
System.out.println("#######学生="+stu);
}
sqlSession.close();
}
@Test
public void testSelectLikeTwo(){
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//准备好like的内容
String name = "张";
List<Student> students = dao.selectLikeTwo(name);
for(Student stu: students){
System.out.println("*******学生="+stu);
}
sqlSession.close();
}