映射参数

1、参数
a、当只有一个参数时,则在xml映射文件中获取改参数数据时没有要求
b、当有多个参数时,则默认将这些参数放到Map集合中,key为arg0…argn或param1…paramn;为了在获取参数值时xml映射文件中的key有意义,可以通过@Param注解的方式指定key的值
c、当参数为自定义引用类型时,则直接使用#{成员变量名}
d、{}与#{}区别:{}直接将数据和sql进行拼接,无法防止SQL注入;#{}以?方式作为占位符,可以防止SQL注入

对数据进行添加,修改或者删除时方法返回值可以是布尔,int,long,及其包装类,如果为Boolean或其包装类则表示是否修改成功,如果为int或long类型,及其包装类,则返回受影响的行数

1、
有两个参数实质是用map实现的,mybatis自动转换(转下面的2)

package com.lq.userinfo;

import org.apache.ibatis.annotations.Param;

import com.lq.vo.UserInfo;

public interface IUserInfoDao{

    UserInfo selectById(@Param("id")String id,@Param("name")String name);
}
package com.lq.userinfo;

import com.lq.vo.UserInfo;

public interface IUserInfoService {

    UserInfo selectById(String id,String name);
}


<mapper namespace="com.lq.userinfo.IUserInfoDao">
    <select id="selectById" resultType="com.lq.vo.UserInfo">
        select * from employee where id=#{id} and user_name=#{name}
    select>
mapper>
package com.lq.userinfo.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lq.userinfo.IUserInfoDao;
import com.lq.userinfo.IUserInfoService;
import com.lq.vo.UserInfo;

@Service
public class UserInfoService implements IUserInfoService{

    @Autowired
    IUserInfoDao userInfoDao;

    @Override
    public UserInfo selectById(String id,String name) {
        return userInfoDao.selectById(id,name);
    }
}

2、

package com.lq.test;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lq.userinfo.IUserInfoDao;
import com.lq.vo.UserInfo;

public class Test {

    public static void main(String[] args){

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        IUserInfoDao userInfoDao = applicationContext.getBean(IUserInfoDao.class);
        Map map = new HashMap<>();
        map.put("param1", "9c702712-cc24-4573-accf-c3c9bcd9daf7");
        map.put("param2", "丹");
        UserInfo userInfo= userInfoDao.selectById(map);
        System.out.println(userInfo);
        applicationContext.close();
    }
}
package com.lq.userinfo;

import java.util.Map;


import com.lq.vo.UserInfo;

public interface IUserInfoDao{

    UserInfo selectById(Map map);
}


<mapper namespace="com.lq.userinfo.IUserInfoDao">
    <select id="selectById" resultType="com.lq.vo.UserInfo">
        select * from employee where id=#{param1} and user_name=#{param2}
    select>
mapper>

3、
参数为自定义类型

package com.lq.userinfo;

import com.lq.vo.UserInfo;

public interface IUserInfoDao{

    UserInfo selectById(UserInfo userInfo);
}


<mapper namespace="com.lq.userinfo.IUserInfoDao">
    <select id="selectById" resultType="com.lq.vo.UserInfo">
        select * from employee where id=#{id} and user_name=#{userName}
    select>
mapper>
package com.lq.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lq.userinfo.IUserInfoDao;
import com.lq.vo.UserInfo;

public class Test {

    public static void main(String[] args){

        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
        IUserInfoDao userInfoDao = applicationContext.getBean(IUserInfoDao.class);
        UserInfo userInfo = new UserInfo();
        userInfo.setId("9c702712-cc24-4573-accf-c3c9bcd9daf7");
        userInfo.setUserName("丹");
        userInfo = userInfoDao.selectById(userInfo);
        System.out.println(userInfo);
        applicationContext.close();
    }
}

你可能感兴趣的:(mybatis)