mybatis 使用resultMap实现数据库的操作
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实现数据库的操作的更多相关文章
- 搭建SpringMVC+Mybatis框架并实现数据库的操作
User类 public class User { private Integer id; private String userName; private String password; priv ...
- Mybatis的ResultMap的使用
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- 使用MyBatis的resultMap高级查询时常用的方式总结
以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...
- Mybatis的ResultMap的使用(转)
本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...
- mybatis高级(2)_数据库中的列和实体类不匹配时的两种解决方法_模糊查询_智能标签
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...
- spring+mybatis的多源数据库配置实战
前言: 关于spring+mybatis的多源数据库配置, 其实是个老生常谈的事情. 网上的方案出奇的一致, 都是借助AbstractRoutingDataSource进行动态数据源的切换. 这边再无 ...
- 根据JavaBean创建数据库的操作SQL
根据JavaBean创建数据库的操作SQL import java.lang.reflect.Field; public class GenerateSQL { public static void ...
- MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致
MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致 情景:当数据库中的列名与我们程序实体类中的字段名称不一致 使用ResultMap节点配置信息 在映射文件中 ...
- Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入
Mybatis基于代理Dao实现CRUD操作 使用要求: 1.持久层接口和持久层接口的映射配置必须在相同的包下 2.持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定 ...
随机推荐
- GEOS库的学习之一:介绍和编译
对GEOS库的学习,源于一个项目:要在c++中判断二维平面中两个多边形的关系(无论凹凸).也就是判断两个多边形是否相交.相容等.听起来很简单,可实现起来却比较难,而项目又催得紧.于是我去搜索了一下,看 ...
- 《JavaScript高级程序设计》chapter 1: javascript 简介
1.2.2 文档对象模型 DHTML的出现让开发人员无需重新加载页面就可以修改其外观了. 1.2.3 浏览器对象模型(BOM) BOM真正与众不同的地方在于他作为javascript实 ...
- gcc学习
gcc学习 预处理:gcc –E xxx.c –o xxx.i;产生预处理过的C原始程序 编 译:gcc –S xxx.i –o xxx.s;产生汇编语言原始程序 汇 编:gcc –c xxx.s – ...
- warning: LF will be replaced by CRLF
一. Git提供了一个换行符检查功能(core.safecrlf),可以在提交时检查文件是否混用了不同风格的换行符.这个功能的选项如下: false - 不做任何检查warn - 在提交时检查并警告t ...
- iOS适配HTTPS,创建一个自签名的SSL证书(x509)具体步骤
引言(创建生成的证书只能用于测试使用.如果想使用自签名证书就只能以自己为 CA机构颁发证书,进行双向认证才能使用) 使用HTTP(超文本传输)协议访问互联网上的数据是没有经过加密的.也就是说,任何人都 ...
- 关于delete 和 new
关于new和delete,有如下代码 // new_test.cpp #include<iostream> using namespace std; class A { public: A ...
- 通过WMI - Win32_Processor - ProcessorId获取到的并不是CPU的序列号,也并不唯一
现在网上不少教程,教人通过WMI - Win32_Processor - ProcessorId来获取CPU的“序列号”,典型代码如下: public static string GetCPUSeri ...
- LINQ浅析
在C# 3.0之前,我们对不同的数据源(数据集合.SQL 数据库.XML 文档等等)进行操作(查询.筛选.投影等等),会使用不同的操作方式. C# 3.0中提出了LINQ(Language Integ ...
- 第六章:Javascript对象
对象是javascript的基本数据类型.对象是一种复合值.它将很多值(原始值 或者其他对象)聚合在一起.可通过名字访问这些值.对象也可以看做是属性的无序集合,每个属性都有一个名/值.属性名是字符串, ...
- Nodejs学习笔记(三)--- 模块
目录 简介及资料 自定义模块 创建一个自定义模块 调用自定义模块 exports和module.exports 区别 exports和module.exports 覆盖 其它... 简介及资料 通过N ...