日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

mybatis 使用resultMap实现数据库的操作

發(fā)布時(shí)間:2023/11/27 生活经验 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mybatis 使用resultMap实现数据库的操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

resultType:直接表示返回類(lèi)型

resultMap:對(duì)外部resultMap的引用

二者不能同時(shí)使用

創(chuàng)建一個(gè)實(shí)體類(lèi)Role和User

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

創(chuàng)建User類(lèi)(在User中有roleId?? 1對(duì)多關(guān)系)

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

創(chuàng)建RoleMapper接口

public interface RoleMapper {public void  add(Role role);public void update(Role role);public void delete(Role role);public List<Role> getRoleList();}

創(chuàng)建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();//根據(jù)roleid獲取用戶(hù)列表public List<User> getUserByRoleId(Role role);
}

?

創(chuàng)建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>

?

創(chuàng)建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">
<!-- 如果我要調(diào)用mapper文件,就去拿到namespace+id(方法名)的名字就可以訪(fǎng)問(wèn) -->
<mapper namespace="cn.bdqn.dao.UserMapper"><!-- id要唯一的,一般是下拉類(lèi)的方法名 --><!-- 返回的是什么類(lèi)型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><!-- 查詢(xún) --><select id="getUserList" resultType="User">select * from user</select>
</mapper>

加入這個(gè)方法用resultMap

 <!-- resultMap中的id隨便取,但要保證id唯一就行 --><!-- type指的是后臺(tái)的javabean的類(lèi)名 映射到哪里 --><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"><!-- 通過(guò)這個(gè)配置文件完成mybatis與數(shù)據(jù)庫(kù)的連接 -->
<configuration><!-- 引入 jdbc.properties 文件--><properties resource="jdbc.properties"/><!-- alias別名 --><!-- 配置mybatis的log實(shí)現(xiàn)LOG4J -->
<settings><setting name="logImpl" value="LOG4J" />
</settings><typeAliases><!-- <typeAlias type="cn.bdqn.pojo.User" alias="User"/> --><!-- 用這個(gè)比較方便,不用一個(gè)一個(gè)寫(xiě)。包下的就是他的類(lèi)名 --><package name="cn.bdqn.pojo"/></typeAliases><environments default="development"><environment id="development"><!--配置事務(wù)管理,采用JDBC的事務(wù)管理  --><transactionManager type="JDBC"></transactionManager><!-- POOLED:mybatis自帶的數(shù)據(jù)源,JNDI:基于tomcat的數(shù)據(jù)源 --><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文件加入到配置文件中  將來(lái)mapper文件很多所以是mappers --><mappers><mapper resource="cn/bdqn/dao/UserMapper.xml"/><mapper resource="cn/bdqn/dao/RoleMapper.xml"/></mappers>
</configuration>

測(cè)試

//查詢(xún)
    @Testpublic 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);}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/xuerong/p/4959539.html

總結(jié)

以上是生活随笔為你收集整理的mybatis 使用resultMap实现数据库的操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。