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属性取值必须是持久层接口的全限定 ...
随机推荐
- .Net中的异步编程总结
一直以来很想梳理下我在开发过程中使用异步编程的心得和体会,但是由于我是APM异步编程模式的死忠,当TAP模式和TPL模式出现的时候我并未真正的去接纳这两种模式,所以导致我一直没有花太多心思去整理这两部 ...
- Asp.net通过Jquery操作WebService进行Ajax读写
一说到开始,我们当然需要项目. 首先,创建一个Asp.net Web 应用,然后新增一个名称为“Web 服务”的文件,也就是后缀名为".asmx"的文件,然后需要进行配置一下,在W ...
- 获取技能的成功经验和关于C语言学习的调查 2015528
内容提要 你有什么技能比大多人(超过90%以上)更好?针对这个技能的获取你有什么成功的经验?与老师博客中的学习经验有什么共通之处? 有关C语言学习的调查 你是怎么学习C语言的?(作业,实验,教材,其他 ...
- 浩瀚先森(guohao1206.com)
博客搬家啦,新博客地址:浩瀚先森 http://www.guohao1206.com
- php mysqli扩展之预处理
在前一篇 mysqli基础知识中谈到mysqli的安装及基础操作(主要是单条sql语句的查询操作),今天介绍的是mysqli中很重要的一个部分:预处理. 在mysqli操作中常常涉及到它的三个主要类: ...
- [USACO 3.1.4]rect1(漂浮法/矩形切割)
描述 N个不同的颜色的不透明的长方形(1 <= N <= 1000)被放置在一张横宽为A竖长为B的白纸上. 这些长方形被放置时,保证了它们的边与白纸的边缘平行. 所有的长方形都放置在白纸内 ...
- redis学习笔记——(4)
一.概述: 在该系列的前几篇博客中,主要讲述的是与Redis数据类型相关的命令,如String.List.Set.Hashes和Sorted-Set.这些命令都具有一个共同点,即所有的操作都是针对与K ...
- android学习——error opening trace file: No such file or directory (2)
1.疑惑: 程序运行起来的时候日志总是显示下面这个错误,但是不影响程序的正常进行,我是用真机来测试的,android4.4.4(API17). 02-11 14:55:03.629 15525-155 ...
- java web名词解释
来源于:http://www.cnblogs.com/yxnchinahlj/archive/2012/02/24/2366110.html PO(persistant object) 持久对象 在o ...
- Sping MVC-创建HelloWeb项目(一)
下面的例子显示怎样使用Spring MVC框架写一个简单的基于Web的应用程序,使用Eclipse IDE作为开发工具,按照下面的步骤使用Spring Web框架来开发一个动态的Web应用 步骤 描述 ...