resultType:直接表示返回类型

resultMap:对外部resultMap的引用

二者不能同时使用

创建一个实体类Role和User

public class Role {
private Integer id;
private String roleCode;
private String roleName;
//省略set、get方法

创建User类(在User中有roleId   1对多关系)

public class User {
private Integer id;
private String userName;
private String userCode;
private String userPassword;
private Integer roleId;
private String roleName;
  //省略set、get方法

创建RoleMapper接口

public interface RoleMapper {

    public void  add(Role role);

    public void update(Role role);

    public void delete(Role role);

    public List<Role> getRoleList();

}

创建UserMapper接口

//接口的名字和xml的名字一样,这样xml中的namespace就不用改
public interface UserMapper {
//接口名的方法名一定要和xml中的id名一样
public int count(); public void add(User user); public void update(User user); public void delete(User user); public List<User> getUserList(); //根据roleid获取用户列表
public List<User> getUserByRoleId(Role role);
}

创建RoleMapper.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="cn.bdqn.dao.RoleMapper">
<select id="getRoleList" resultType="Role">
select * from role
</select>
<insert id="add" parameterType="Role">
insert into role (roleCode,roleName)
values (#{roleCode},#{roleName})
</insert>
<update id="update" parameterType="Role">
update role set roleCode=#{roleCode},roleName=#{roleName} where id=#{id}
</update>
<delete id="delete" parameterType="Role">
delete from role where id=#{id}
</delete>
</mapper>

创建UserMapper.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+id(方法名)的名字就可以访问 -->
<mapper namespace="cn.bdqn.dao.UserMapper">
<!-- id要唯一的,一般是下拉类的方法名 -->
<!-- 返回的是什么类型int -->
<select id="count" resultType="int">
select count(1) from user
</select>
<!-- 增加 -->
<insert id="add" parameterType="User">
insert into user(userCode,userName,userPassword)
values (#{userCode},#{userName},#{userPassword})
</insert>
<!-- 修改 -->
<update id="update" parameterType="User">
update user set userCode=#{userCode},userName=#{userName},
userPassword=#{userPassword} where id=#{id}
</update>
<!-- 删除 -->
<delete id="delete" parameterType="User">
delete from user where id=#{id}
</delete>
<!-- 查询 -->
<select id="getUserList" resultType="User">
select * from user
</select>
</mapper>

加入这个方法用resultMap

 <!-- resultMap中的id随便取,但要保证id唯一就行 -->
<!-- type指的是后台的javabean的类名 映射到哪里 -->
<resultMap type="User" id="userMap">
<!-- 显示的字段 -->
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleName" column="roleName"/> </resultMap>
<!-- 拿roleID -->
<select id="getUserByRoleId" parameterType="User" resultMap="userMap">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId=#{id}
</select>

在mybatis-config.xml中添加<mapper resource="cn/bdqn/dao/RoleMapper.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"> <!-- 通过这个配置文件完成mybatis与数据库的连接 -->
<configuration>
<!-- 引入 jdbc.properties 文件-->
<properties resource="jdbc.properties"/>
<!-- alias别名 --> <!-- 配置mybatis的log实现LOG4J -->
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<typeAliases>
<!-- <typeAlias type="cn.bdqn.pojo.User" alias="User"/> -->
<!-- 用这个比较方便,不用一个一个写。包下的就是他的类名 -->
<package name="cn.bdqn.pojo"/>
</typeAliases> <environments default="development">
<environment id="development">
<!--配置事务管理,采用JDBC的事务管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments> <!-- 将mapper文件加入到配置文件中 将来mapper文件很多所以是mappers -->
<mappers>
<mapper resource="cn/bdqn/dao/UserMapper.xml"/>
<mapper resource="cn/bdqn/dao/RoleMapper.xml"/>
</mappers>
</configuration>

测试

//查询
@Test
public void getUserListTest(){
SqlSession sqlSession = null;
try {
List<User> userList = new ArrayList<User>();
sqlSession = MyBatisUtil.createSqlSession();
userList = sqlSession.selectList("cn.bdqn.dao.UserMapper.getUserList");
for(User user:userList){
logger.debug("user的id==="+user.getId());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
sqlSession.rollback();
}finally{
MyBatisUtil.closeSqlSession(sqlSession);
}
}

mybatis 使用resultMap实现数据库的操作的更多相关文章

  1. 搭建SpringMVC+Mybatis框架并实现数据库的操作

    User类 public class User { private Integer id; private String userName; private String password; priv ...

  2. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  3. 使用MyBatis的resultMap高级查询时常用的方式总结

    以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...

  4. Mybatis的ResultMap的使用(转)

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  5. mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  6. spring+mybatis的多源数据库配置实战

    前言: 关于spring+mybatis的多源数据库配置, 其实是个老生常谈的事情. 网上的方案出奇的一致, 都是借助AbstractRoutingDataSource进行动态数据源的切换. 这边再无 ...

  7. 根据JavaBean创建数据库的操作SQL

    根据JavaBean创建数据库的操作SQL import java.lang.reflect.Field; public class GenerateSQL { public static void ...

  8. MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致

    MyBatis系列二  之   数据库列名于程序实体类中字段名称不一致 情景:当数据库中的列名与我们程序实体类中的字段名称不一致         使用ResultMap节点配置信息  在映射文件中  ...

  9. Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入

    Mybatis基于代理Dao实现CRUD操作 使用要求: 1.持久层接口和持久层接口的映射配置必须在相同的包下 2.持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定 ...

随机推荐

  1. Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle

    写了个最简单的aop例子 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  2. 虚拟机安装Mac OS X ----- VM12安装Mac OS X

    Windows下虚拟机安装Mac OS X -– VM12安装Mac OS X 10.11 随着Iphone在国内大行其道,越来越多的开发者涌入iOS开发大军 中,但都苦于没有苹果机,本文即将介绍WI ...

  3. [Android] 安卓模拟器临时文件相关问题

    今天生产环境有台机器的硬盘满了,排查发现我的模块在/tmp/android-username目录下留了一堆形如“emulator-1tpH5l”的文件,占用了很大的空间. 这个模块会反复启停好几个安卓 ...

  4. windows命令行

    [drive:] Change the current driver, without changing its current directory cd [drive:] [path] Just c ...

  5. Canvas 教程

    在本文章中 在你开始之前 教程内容 相关资料 A note to contributors <canvas> 是一种可以通过编写脚本(通常是JavaScript)来实现绘制图形的HTML元 ...

  6. CAS ticket过期策略

    CAS提供可扩展的ticket过期策略,支持ticket-granting tickets (TGT)和service tickets (ST)的配置. CAS客户端存储用户信息一般使用session ...

  7. [转]细说MySQL Explain和Optimizer Trace简介

    在开发过程中,对每个上线的SQL查询指纹(query figerprint)的质量都应有估算:而估算DB查询质量最直接的方法,就是分析其查询执行计划( Query Execution Plan ,即Q ...

  8. 开源搜索引擎Iveely 0.7.0发布,不一样,那就让他不一样!

    2012年08月05日,Iveely Search Engine 0.1.0发布,今天,怀着对于未来的追求,终于,0.7.0如期和大家见面了,7个版本,历时2年4个月,感谢大家的支持,感谢我不离不弃的 ...

  9. Java学习笔记(四)——google java编程风格指南(上)

    [前面的话] 年后开始正式上班,计划着想做很多事情,但是总会有这样那样的打扰,不知道是自己要求太高还是自我的奋斗意识不够?接下来好好加油.好好学学技术,好好学习英语,好好学习做点自己喜欢的事情,趁着自 ...

  10. Java学习笔记(十五)——javadoc学习笔记和可能的注意细节

    [前面的话] 这次开发项目使用jenkins做持续集成,PMD检查代码,Junit做单元测试,还会自动发邮件通知编译情况,会将javadoc生成的文档自动发到一个专门的服务器上面,每个人都可以看,所以 ...