?
?
?
?
?
?ResultMap:用于类的属性和表的列名不一样时返回结果属性为空,通过ResultMap来定义类的属性和表的类的映射关系,就可以解决属性名和列值不同的问题
?
?
StudentDao接口:
package com.bjpowernode.dao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.vo.CustomObject;
import com.bjpowernode.vo.QueryParama;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface StudentDao {
//查询学生
Student selectById(Integer id);
//定义方法,查询结果是一个自定义类型对象CustomObject
CustomObject selectById2(@Param("stuid") Integer id);
//查询数目,查询结果是Long类型
long countStudnet();
//查询结果返回是一个Map
Map<Object,Object> selectMap(@Param("stuid") Integer id);
}
?StudentDao.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.StudentDao">
<!--
resultType:1.java的全限定名称 2.别名
resultType:表示mybatis执行sql后得到的java对象 规则同名的列赋值给同名的属性
-->
<select id="selectById" resultType="com.bjpowernode.domain.Student">
select id,name ,email,age from student where id=#{studentId}
</select>
<!--自定义对象属性和列名不同类型查询-->
<!-- <select id="selectById2" resultType="com.bjpowernode.vo.CustomObject">-->
<!-- select id,name ,email,age from student where id=#{stuid}-->
<!-- </select>-->
<!--
使用resultMap定义列和属性的关系
定义resultMap
id:给resultMap的映射关系起一个名称,唯一值
type:java类型的全限定名称
resultMap定义之后可以后可以重复使用
-->
<resultMap id="customMap" type="com.bjpowernode.vo.CustomObject">
<!--定义的列名和属性名的对应
主键类型使用id标签
非主键使用result标签
-->
<id column="id" property="cid" />
<result column="name" property="cname"/>
<!--列名和属性名相同不用定义,想定义也可以-->
<result column="email" property="email"/>
<result column="age" property="age"/>
</resultMap>
<!--自定义对象属性和列名不同类型查询-->
<select id="selectById2" resultMap="customMap">
select id,name ,email,age from student where id=#{stuid}
</select>
<!--执行sql语句,得到一行一列-->
<select id="countStudnet" resultType="java.lang.Long">
select count(*) from student
</select>
<!--执行sql得到一个Map结构数据,mybatis执行sql,把结果ResultSet转换为Map
sql执行结果,列名作为map的key,列值作为map的value 查询列名有几个map集合就会有几个键值对
sql执行得到的是一行记录,
dao接口返回的是一个map,sql语句最对获取一行记录,多余一行会报错
-->
<select id="selectMap" resultType="java.util.HashMap">
select id,name,email from student where id=#{stuid}
</select>
</mapper>
ProvinceDao接口:
package com.bjpowernode.dao;
import com.bjpowernode.vo.ProvinceCity;
import java.util.List;
public interface ProvinceDao {
List<ProvinceCity> selectProvinceCityLis(Integer provinceId);
}
ProvinceDao.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjpowernode.dao.ProvinceDao">
<!--使用insert,update,delete,select标签来写sql语句-->
<select id="selectProvinceCityLis" resultType="com.bjpowernode.vo.ProvinceCity">
select p.id ,p.name,c.id cid,c.name cname FROM province p INNER JOIN city c ON p.id=c.provinceid WHERE p.id=#{pid}
</select>
</mapper>
CustomObject类:
package com.bjpowernode.vo;
/*
属性名和表列明不相同,则查询后返回的属性为空
*/
public class CustomObject {
private Integer cid;
private String cname;
private String email;
private Integer age;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getName() {
return cname;
}
public void setName(String name) {
this.cname = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "CustomObject{" +
"cid=" + cid +
", cname='" + cname + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
ProvinceCity类:
package com.bjpowernode.vo;
public class ProvinceCity {
private Integer id;
private String name;
private Integer cid;
private String cname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "ProvinceCity{" +
"id=" + id +
", name='" + name + '\'' +
", cid=" + cid +
", cname='" + cname + '\'' +
'}';
}
}
QueryParama类:
package com.bjpowernode.vo;
public class QueryParama {
private Object p1;
private Object p2;
public Object getP1() {
return p1;
}
public void setP1(Object p1) {
this.p1 = p1;
}
public Object getP2() {
return p2;
}
public void setP2(Object p2) {
this.p2 = p2;
}
}
主配置文件 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>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--声明别名-->
<!--
第一种方式:
type:java类型的全限定名称
alias:自定义别名
优点:别名可以自定义
缺点:每个类型必须单独定义
第二种方式:
name:包名 mybatis会把这个包中的所有类名作为别名(不区分大小写)
优点:使用方便,一次可以给多个类定义别名
缺点:别名不能自定义,必须是类名
-->
<typeAliases>
<!-- 第一种方式-->
<!-- <typeAlias type="com.bjpowernode.domain.Student" alias="stu"></typeAlias>-->
<!-- <typeAlias type="com.bjpowernode.vo.QueryParama" alias="qp" />-->
<!-- 第二种方式-->
<package name="com.bjpowernode.domain"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- 配置数据源:创建Connection对象,连接数据库 -->
<dataSource type="POOLED">
<!--driver:驱动的内容-->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!--连接数据库的url-->
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&charaterEcoding=utf-8"/>
<!--用户名-->
<property name="username" value="root"/>
<!--密码-->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 是指定其他mapper文件的位置:
其他mapper文件的目的是找到其他文件sql语句
-->
<mappers>
<mapper resource="com/bjpowernode/dao/StudentDao.xml"/>
<!-- 新加了接口需要指定xml的位置-->
<mapper resource="com/bjpowernode/dao/ProvinceDao.xml"/>
</mappers>
</configuration>
MyTest:
package com.bjpowernode;
import com.bjpowernode.dao.ProvinceDao;
import com.bjpowernode.dao.StudentDao;
import com.bjpowernode.domain.Student;
import com.bjpowernode.utils.MybatisUtil;
import com.bjpowernode.vo.CustomObject;
import com.bjpowernode.vo.ProvinceCity;
import com.bjpowernode.vo.QueryParama;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
MyBaits dao代理使用
*/
public class MyTest {
//查询测试
@Test
public void testSelectById(){
//获取SqlSession
SqlSession session= MybatisUtil.getSqlSession();
//获取dao代理
StudentDao dao=session.getMapper(StudentDao.class);
//调用方法
Student student=dao.selectById(1005);
System.out.println("student="+student);
//4.关闭SqlSession对象
session.close();
}
//自定义对象进行查询id
@Test
public void testSelectById2(){
//获取SqlSession
SqlSession session= MybatisUtil.getSqlSession();
//获取dao代理
StudentDao dao=session.getMapper(StudentDao.class);
//调用方法
CustomObject co =dao.selectById2(1001);
System.out.println("CustomObject="+co);
//4.关闭SqlSession对象
session.close();
}
//返回记录数
@Test
public void testCountStudent(){
//获取SqlSession
SqlSession session= MybatisUtil.getSqlSession();
//获取dao代理
StudentDao dao=session.getMapper(StudentDao.class);
//调用方法
Long nums=dao.countStudnet();
System.out.println("nums="+nums);
//4.关闭SqlSession对象
session.close();
}
//返回Map集合
@Test
public void testSelectMap(){
//获取SqlSession
SqlSession session= MybatisUtil.getSqlSession();
//获取dao代理
StudentDao dao=session.getMapper(StudentDao.class);
//调用方法
Map<Object,Object> map=dao.selectMap(1006);
System.out.println("map"+map);
//4.关闭SqlSession对象
session.close();
System.out.println("name="+map.get("name"));
System.out.println("id="+map.get("id"));
System.out.println("email="+map.get("email"));
}
//
@Test
public void testSelectCity(){
//获取SqlSession
SqlSession session= MybatisUtil.getSqlSession();
//新增加的接口,需要重新获取dao代理
ProvinceDao dao=session.getMapper(ProvinceDao.class);
List<ProvinceCity> list = dao.selectProvinceCityLis(1);
//关闭连接
session.close();
list.forEach(p-> System.out.println(p));
}
}
第一个测试结果:
2.
3.
4.
5.
?
?
?
?
?
|