• 13、Mybatis框架-2


    Mapper映射器

    Mapper映射器是开发者创建绑定映射语句的接口,映射器的实例可以从SqlSession中获得,具体步骤为:

    1、首先根据Mapper映射描述文件编写一个Mapper接口,接口方法名和SQL映射描述定义的SQL-ID属性保持一致。

    2、再利用SqlSession提供的getMapper(…)方法,会自动返回一个Mapper接口实例。

    XXX mapper = session.getMapper(XXX.class);

    简化写法,可以把sql语句的xml文件映射到一个接口中,从而删除dao层

    步骤:

    1、新建一个mapper包

    在这里插入图片描述

    2、mapper包中新建一个接口,名称:实体类名称+Mapper

    在这里插入图片描述

    package com.cykj.mapper;
    
    import com.cykj.entity.Foreuser;
    
    import java.util.List;
    
    public interface ForeUserMapper {
        //添加用户
        public int addForeUser(Foreuser foreuser);
    
        //删除用户
        public int deleteForeUser(long fuId);
    
        //修改用户密码
        public int updateForeUser(long fuId,String fuPwd);
    
        //查询所有用户列表
        public List<Foreuser> selectAllForeUser();
    
        //根据用户名模糊查找,带分页
        public List<Foreuser> selectForeUserByName(String fuName,int pageNo);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3、在namespace中完成关联

    在这里插入图片描述

    4、sql的xml文件中的id要和mapper接口中的方法要一一对应

    5、通过sqlSession.getMapper(接口.class)获得一个接口的实例

    SqlSession sqlSession = MybatisDbHelper.getSession();
    //获得一个ForeUserMapper接口的实例
    ForeUserMapper mapper = sqlSession.getMapper(ForeUserMapper.class);
    
    • 1
    • 2
    • 3

    6、调用接口中的方法即可

    import com.cykj.MybatisDbHelper;
    import com.cykj.dao.ForeUserDao;
    import com.cykj.dao.ForeUserDaoImpl;
    import com.cykj.entity.Foreuser;
    import com.cykj.mapper.ForeUserMapper;
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MyTest2 {
        SqlSession sqlSession = MybatisDbHelper.getSession();
        //获得一个ForeUserMapper接口的实例
        ForeUserMapper mapper = sqlSession.getMapper(ForeUserMapper.class);
        @Test
        public void addForeUser(){
            Foreuser foreuser = new Foreuser();
            foreuser.setFuName("cykj005");
            foreuser.setFuPwd("123456");
            foreuser.setRealName("刘备2");
            foreuser.setCardType(1);
            foreuser.setCardNo("3522.......");
            foreuser.setEmail("110@qq.com");
            foreuser.setPhone("152.....");
            foreuser.setTravelType(1);
            int i = mapper.addForeUser(foreuser);
            sqlSession.commit();
            System.out.println(i);
        }
    
        @Test
        public void deleteForeUser(){
            int i = mapper.deleteForeUser(15);
            sqlSession.commit();
            System.out.println(i);
        }
    
        @Test
        public void updateForeUser(){
            Map map = new HashMap();
            map.put("fuId",1);
            map.put("fuPwd","7777777");
            int i = mapper.updateForeUser(map);
            sqlSession.commit();
            System.out.println(i);
        }
    
        @Test
        public void selectAllForeUser(){
            List<Foreuser> list =  mapper.selectAllForeUser();
            for(Foreuser foreuser:list){
                System.out.println(foreuser);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    多参数传参:

    1、使用map

     //执行sql
     Map map = new HashMap();
     map.put("fuId",fuId);
     map.put("fuPwd",fuPwd);
    
    int i = sqlSession.update("updateForeUser",map);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、使用注解

    public ForeUser login(@Param(value="account") String account,@Param(value="password") String password);
    
    • 1

    使用注解后parameterType可以不用写

    动态sql语句:

    1、if元素

    if元素是简单的条件判断逻辑,满足指定条件时追加if元素内的SQL,不满足条件时不追加,使用格式如下
    
    
    
         SQL语句1
    
       
    
         SQL语句2
    
       
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、where元素

    where元素主要是用于简化查询语句中where部分的条件判断,where元素可以在元素所在位置输出一个where关键字,而且还可以将后面条件多余的and或or关键字去除,与其他元素搭配使用。
    
    where使用格式如下:
    
    
       select 字段 from 表
       
            动态追加条件
       
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    //映射接口
    //动态sql
        public List<Foreuser> selectForeUser(@Param(value = "fuName") String fuName,
                                             @Param(value = "travelType") long travelType);
    
    //sql语句
    <select id="selectForeUser" resultType="com.cykj.entity.Foreuser">
    	select * from foreUser where 1=1
    	<if test="fuName!=null and fuName!=''">
    		and fuName like concat("%",#{fuName},"%")
    	</if>
    	<if test="travelType>0">
    		and travelType=#{travelType}
    	</if>
    </select>
            
            //测试方法
    @Test
    public void selectForeUser(){
    	List<Foreuser> list =  mapper.selectForeUser(null,0);
    	for(Foreuser foreuser:list){
    	System.out.println(foreuser);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3、foreach元素(批量操作)

    reach元素实现了循环逻辑,可以进行一个集合的迭代,主要用在构建in条件中。foreach使用示例如下:
    
    
    
    foreach元素非常强大,它允许指定一个集合,声明集合项和索引变量,这些变量可以用在元素体内。它也允许指定开放和关闭的字符串,在迭代之间放置分隔符。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    //映射接口方法
    //批量添加
    public int addForeUserList(List<Foreuser> list);
    
    
    //单元测试方法
     @Test
    public void addForeUserList(){
    	List<Foreuser> list = new ArrayList<Foreuser>();
    	for(int i=0;i<5;i++){
    		Foreuser f = new Foreuser();
    		f.setFuName("");
    	}
    }
    
    //sql语句
    <insert id="addForeUserList" parameterType="java.util.List">
            insert into foreUser
            (fuName,fuPwd,realName,cardType,cardNo,email,phone,travelType)
            values
            <foreach collection ="list" item="foreUser" separator ="," open="(" close=")">
                #{reddemCode.batchId},
                #{reddemCode.code},
                #{reddemCode.type},
                #{reddemCode.facevalue},
                #{reddemCode.createUser},
                #{reddemCode.createTime}
            </foreach >
            (#{fuName},#{fuPwd},#{realName},#{cardType},#{cardNo},#{email},#{phone},#{travelType})
        </insert>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    关联映射:

    1、一对一:

    <select id="findById" parameterType="java.lang.Integer" resultMap= "empMap">
        select  e.empno,e.ename,e.job,e.mgr,e.sal,e.comm,e.hiredate,e.deptno,
    	d.dname,d.loc from EMP e join DEPT d on(d.deptno=e.deptno)
        where e.EMPNO=#{id}
    select>
    
    <resultMap type="Emp" id="empMap">
       <id property="empno" column="EMPNO" />
       <result property="ename" column="ENAME" />
       <result property="job" column="JOB" />
       <result property="mgr" column="MGR" />
       ...
       <association property="dept" column="DEPTNO" javaType="Dept">
       association>
    resultMap> 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    //查找后台用户
       public List selectTblUser();
    
    
    
        
            
            
            
            
            
            
                
                
            
        
    
    @Test
        public void selectTblUser(){
            List list = mapper.selectTblUser();
            for(Tbluser tbluser:list){
                System.out.println(tbluser.getTblrole().getRoleName());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    2、一对多:

    当利用findById查询时,会执行定义SQL语句,将DEPT字段值封装成Dept对象,EMP字段封装成Emp对象集合,然后将Emp对象集合给Dept对象的emps属性赋值

    <select id="findById" parameterType="java.lang.Integer" resultMap= 	"deptEmpsResult">
        select  d.deptno,d.dname,d.loc,e.empno,e.ename,e.job,e.mgr,e.sal,
        e.comm,e.hiredate from EMP e join DEPT d on(d.deptno=e.deptno)
        where d.deptno = #{deptno}
    select>
    <resultMap id="deptEmpsResult" type="Dept">
         <id property="deptno" column="DEPTNO" />
         <result property="dname" column="DNAME" />
         <result property="loc" column="LOC" />
       <collection property="emps" ofType="Emp">
         <id property="empno" column="EMPNO" />
         <result property="ename" column="ENAME" />
         collection>
    resultMap> 
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    //一对多:一个用户有多个订单
        public List selectForeUser2();
    
    
    
        
            
            
            
            
            
            
            
            
            
            
                
                
                
                
                
                
                
            
        
    
    @Test
        public void selectForeUser2(){
            List list = mapper.selectForeUser2();
            for(Foreuser foreuser:list){
                System.out.println(foreuser);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    Android adb启动app方式
    云原生Kubernetes:二进制部署K8S单Master架构(二)
    【华为机试真题 JAVA】数字涂色-100
    ECE368 Programming Assignment 3
    管理人员的薪酬制度设计
    天启科技联创郭志强:趟遍教育行业信数化沟坎,创业智能赛道重塑行业生态
    有了这个库,以后再也不用写正则表达式了!
    Sui主网升级至V1.12.2版本
    阶段七-Day02-SpringMVC
    流量卡可以自己选地区吗?看完你就明白了!
  • 原文地址:https://blog.csdn.net/qq_37917691/article/details/126409783