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属性取值必须是持久层接口的全限定 ...
随机推荐
- 使用ajax跨域withCredentials的作用
默认情况下,跨源请求不提供凭据(cookie.HTTP认证及客户端SSL证明等).通过将withCredentials属性设置为true,可以指定某个请求应该发送凭据.如果服务器接收带凭据的请求,会用 ...
- Android 动画之TranslateAnimation应用详解
TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现, 通过TranslateAnimation(float fromXDel ...
- Linux常用指令---定时任务
linux定时任务crontab命令选项基本只有对用户操作选项:-u 指定用户-l 列出某用户任务计划-r 删除某用户任务-e 编辑某用户任务 查看某一用户的定时任务crontab -u root - ...
- 学习笔记——Maven实战(五)自动化Web应用集成测试
自动化集成测试的角色 本专栏的上一篇文章讲述了Maven与持续集成的一些关系及具体实践,我们都知道,自动化测试是持续集成必不可少的一部分,基本上,没有自动化测试的持续集成,都很难称之为真正的持续集成. ...
- 3.SQLAlchemy文档-SQLAlchemy Core(中文版)
这里的文描述了关于SQLAlchemy的的SQL渲染引擎的相关内容,包括数据库API的集成,事务的集成和数据架构描述服务.与以领域为中心的ORM使用模式相反,SQL表达式语言提供了一个数据构架为中心的 ...
- node 大牛的blog
node一些基本的核心包的使用 http://cnodejs.org/topic/548e53f157fd3ae46b2334fd node的基本的三种框架的比较 http://cnodejs.o ...
- 编写高质量代码改善C#程序的157个建议[泛型集合、选择集合、集合的安全]
前言 软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...
- PHP乱码问题,UTF-8(乱码)
一.HTML页面转UTF-8编码问题 1.在head后,title前加入一行: <meta http-equiv='Content-Type' content='text/html; chars ...
- navigationBar设置透明度
将NavigationBar设置透明(仅将指定视图控制器进行透明处理),步骤如下:1.在视图控制器的头文件中实现UINavigationControllerDelegate,例如:@interface ...
- 50个提高PHP编程效率的方法
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...