1、resultType结果类型
指sql语句执行完毕后, 数据转为的java对象, java类型是任意的。
resultType结果类型的值 1. 类型的全限定名称 2. 类型的别名, 例如 java.lang.Integer别名是int
处理方式:
1. mybatis执行sql语句, 然后mybatis调用类的无参数构造方法,创建对象。
2. mybatis把ResultSet指定列值付给同名的属性。
<select id="selectMultiPosition" resultType="com.bjpowernode.domain.Student"> select id,name, email,age from student </select> 对等的jdbc ResultSet rs = executeQuery(" select id,name, email,age from student" ) while(rs.next()){ Student student = new Student(); student.setId(rs.getInt("id")); student.setName(rs.getString("name")) }
2、定义自定义类型的别名
1)在mybatis主配置文件中定义,使<typeAlias>定义别名
2)可以在resultType中使用自定义别名方式一:可以指定一个类型一个自定义别名
type:自定义类型的全限定名称 alias:别名(短小,容易记忆的)
<typeAlias type="com.bjpowernode.domain.Student" alias="stu" />
方式二:<package> name是包名, 这个包中的所有类,类名就是别名(类名不区分大小写)
<package name="com.bjpowernode.domain"/>
mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--settings:控制mybatis全局行为-->
<settings>
<!--设置mybatis输出日志-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--定义别名-->
<typeAliases>
<!--<typeAlias type="com.bjpowernode.domain.Student" alias="stu" />
<typeAlias type="com.bjpowernode.vo.ViewStudent" alias="vstu" />-->
<package name="com.bjpowernode.domain"/>
<package name="com.bjpowernode.vo"/>
</typeAliases>
<environments default="mydev">
<environment id="mydev">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库的驱动类名-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库的url字符串-->
<property name="url" value="jdbc:mysql://localhost:3306/springdb"/>
<!--访问数据库的用户名-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- sql mapper(sql映射文件)的位置-->
<mappers>
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
<!--<mapper resource="com/bjpowernode/dao/SchoolDao.xml" />-->
</mappers>
</configuration>
3、resultMap结果映射
1)你自定义列值赋值给哪个属性
2)当你的列名和属性名不一样时,一定使用resultMap
<!--使用resultMap
1)先定义resultMap
2)在select标签,使用resultMap来引用1定义的。
-->
<!--定义resultMap
id:自定义名称,表示你定义的这个resultMap
type:java类型的全限定名称
-->
<resultMap id="studentMap" type="com.bjpowernode.domain.Student">
<!--列名和java属性的关系-->
<!--注解列,使用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>
当查询的列名(数据库中)和属性名(java对象)不一样时,采取的方式是,
方式一:使用 resultMap
<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>
方式二:使用列名
<select id="selectDiffColProperty" resultType="com.bjpowernode.domain.MyStudent">
select id as stuid ,name as stuname, email as stuemail , age stuage from student
</select>
4、like 模糊查询
第一种:java代码中,指定like的查询内容,查询语句中 name = ‘%李%’
接口:
/*第一种模糊查询, 在java代码指定 like的内容*/ List<Student> selectLikeOne(String name);
mapper:
<select id="selectLikeOne" resultType="com.bjpowernode.domain.Student"> select id,name,email,age from student where name like #{name} </select>
第二种,在mapper拼接
接口:
/*name就是李值, 在mapper中拼接 like "%" 李 "%" */ List<Student> selectLikeTwo(String name);
mapper:
<!--第二种方式:在mapper文件中拼接 like的内容--> <select id="selectLikeTwo" resultType="com.bjpowernode.domain.Student"> select id,name,email,age from student where name like "%" #{name} "%" </select>