当前位置: 首页 > news >正文

网站和域名的关系陕西科强建设工程有限公司官方网站

网站和域名的关系,陕西科强建设工程有限公司官方网站,网络服务的重要性?,服务好的赣州网站建设1、参数获取方式 MyBatis可以通过以下两种方式获取参数值: #{变量名} 本质是占位符赋值 ${变量名} 本质是字符串拼接#xff0c;如果拼接的是字符串类型或日期类型#xff0c;则需要手动添加单引号 2、参数获取的几种情况#xff1a; 2.1 mapper接口方法的参数为单个字…1、参数获取方式 MyBatis可以通过以下两种方式获取参数值: #{变量名} 本质是占位符赋值 ${变量名} 本质是字符串拼接如果拼接的是字符串类型或日期类型则需要手动添加单引号 2、参数获取的几种情况 2.1 mapper接口方法的参数为单个字面量类型 可以通过${}和#{}以任意名称获取变量值需注意${}的单引号问题 如下 public interface UserMapper {User selectUserByUserName(String username);}select idselectUserByUserName resultTypeUser!-- select * from t_user where username ${username} --select * from t_user where username #{username}/select2.2 mapper接口方法的参数为多个时 mybatis会将多个参数封装到map集合中并以两种方式存储 方式一以arg0,arg1,arg2,…为键以参数为值方式二以param1,param2,param3,…为键以参数为值 可以通过#{}和${}两种方式以键的方式获取值即可需注意${}的单引号问题 如下 ListUser selectUserByUserNameAndAge(String username, Integer age, String sex);select idselectUserByUserNameAndAge resultTypeUser!-- select * from t_user where username ${arg0} and age ${arg1} and sex ${arg2} --select * from t_user where username #{arg0} and age #{arg1} and sex #{arg2}/select2.3 mapper接口方法有多个参数时手动封装map参数 可以通过#{}和${}两种方式以键的方式获取值即可需注意${}的单引号问题 如下 Testpublic void testQueryByMapParam(){SqlSession sqlSession SqlSessionUtil.getSqlSession();UserMapper userMapper sqlSession.getMapper(UserMapper.class);MapString, Object map new HashMap();map.put(username, 李斯);map.put(age, 23);map.put(sex, 女);ListUser admin userMapper.selectUserByMap(map);admin.stream().forEach(System.out::println);}ListUser selectUserByMap(MapString, Object map);select idselectUserByMap resultTypeUserselect * from t_user where username #{username} and age #{age} and sex #{sex}/select2.4 mapper接口方法的参数为实体类类型的参数 可以通过${}和#{}以任意名称获取变量值需注意${}的单引号问题 如下 Testpublic void testInsertUser(){SqlSession sqlSession SqlSessionUtil.getSqlSession();UserMapper userMapper sqlSession.getMapper(UserMapper.class);User user new User();user.setUsername(中华);user.setPassword(123456);user.setAge(32);user.setSex(男);int i userMapper.insertUser(user);System.out.println(i);}int insertUser(User user);insert idinsertUserinsert into t_user values(null, #{username}, #{password}, #{age}, #{sex})/insert2.5 使用Param注解命名参数 mybatis会将多个参数封装到map集合中并以两种方式存储 方式一以Param注解的值为键以参数为值方式二以param1,param2,param3,…为键以参数为值 因此可以通过${}和#{}以任意名称获取变量值需注意${}的单引号问题 Testpublic void testQueryByParam(){SqlSession sqlSession SqlSessionUtil.getSqlSession();UserMapper userMapper sqlSession.getMapper(UserMapper.class);ListUser admin userMapper.selectUserByParam(李斯, 23, 女);admin.stream().forEach(System.out::println);}ListUser selectUserByParam(Param(username) String username, Param(age) Integer age, Param(sex) String sex);select idselectUserByParam resultTypeUserselect * from t_user where username #{username} and age #{age} and sex #{sex}/select3、MyBatis参数解析原理 3.1 参数封装 主要分析MyBatis对接口参数的处理如参数的封装、参数值的获取原理。 org.apache.ibatis.reflection.ParamNameResolver#ParamNameResolverorg.apache.ibatis.reflection.ParamNameResolver#getNamedParamsParamNameResolver构造 构造方法中处理方法参数的映射建立参数的索引和参数名的映射关系。 // 参数名称处理public ParamNameResolver(Configuration config, Method method) {// 是否使用真实的参数名默认为truethis.useActualParamName config.isUseActualParamName();// 获取方法的参数类型数组final Class?[] paramTypes method.getParameterTypes();// 获取方法参数注解final Annotation[][] paramAnnotations method.getParameterAnnotations();// 存储参数信息临时对象final SortedMapInteger, String map new TreeMap();int paramCount paramAnnotations.length;// get names from Param annotationsfor (int paramIndex 0; paramIndex paramCount; paramIndex) {/**The key is the index and the value is the name of the parameter. The name is obtained from Param if specified. When Param is not specified, the parameter index is used. Note that this index could be different from the actual index when the method has special parameters (i.e. RowBounds or ResultHandler).aMethod(Param(M) int a, Param(N) int b) - {{0, M}, {1, N}}aMethod(int a, int b) - {{0, 0}, {1, 1}}aMethod(int a, RowBounds rb, int b) - {{0, 0}, {2, 1}}*/if (isSpecialParameter(paramTypes[paramIndex])) {// 跳过特殊参数 如RowBounds 或 ResultHandlercontinue;}String name null;for (Annotation annotation : paramAnnotations[paramIndex]) {if (annotation instanceof Param) {// 当前参数存在Param注解时参数名设置为注解的value值hasParamAnnotation true;name ((Param) annotation).value();break;}}if (name null) {// 如果存在Param注解且没有指定参数名if (useActualParamName) {// 取出方法参数名如果时单一普通参数则返回arg0name getActualParamName(method, paramIndex);}if (name null) {// use the parameter index as the name (0, 1, ...)// gcode issue #71// 此时将索引作为参数名name String.valueOf(map.size());}}map.put(paramIndex, name);}names Collections.unmodifiableSortedMap(map);}getNamedParams 建立参数名和参数值的映射关系并返回参数值 /*** p* A single non-special parameter is returned without a name. Multiple parameters are named using the naming rule. In* addition to the default names, this method also adds the generic names (param1, param2, ...).* /p** param args* the args** return the named params*/public Object getNamedParams(Object[] args) {final int paramCount names.size();if (args null || paramCount 0) {return null;}// 单一参数且没有Param注解时if (!hasParamAnnotation paramCount 1) {Object value args[names.firstKey()];// 此处会对集合类型的参数进行包装处理return wrapToMapIfCollection(value, useActualParamName ? names.get(names.firstKey()) : null);} else {// 建立参数名和值的映射key:参数名 value:参数值final MapString, Object param new ParamMap();int i 0;for (Map.EntryInteger, String entry : names.entrySet()) {// 1.添加命名参数和参数值的映射关系param.put(entry.getValue(), args[entry.getKey()]);// add generic param names (param1, param2, ...)final String genericParamName GENERIC_NAME_PREFIX (i 1);// ensure not to overwrite parameter named with Paramif (!names.containsValue(genericParamName)) {// 2.同时添加一个以param1,param2...为参数名 和 参数值的映射关系param.put(genericParamName, args[entry.getKey()]);}i;}return param;}}wrapToMapIfCollection /*** Wrap to a {link ParamMap} if object is {link Collection} or array.** param object* a parameter object* param actualParamName* an actual parameter name (If specify a name, set an object to {link ParamMap} with specified name)** return a {link ParamMap}** since 3.5.5*/public static Object wrapToMapIfCollection(Object object, String actualParamName) {// 如果是集合类型则包装key为collection、list的map返回if (object instanceof Collection) {ParamMapObject map new ParamMap();map.put(collection, object);if (object instanceof List) {map.put(list, object);}Optional.ofNullable(actualParamName).ifPresent(name - map.put(name, object));return map;}// 如果是数据组类型则包装key为array的map返回if (object ! null object.getClass().isArray()) {ParamMapObject map new ParamMap();map.put(array, object);Optional.ofNullable(actualParamName).ifPresent(name - map.put(name, object));return map;}// 如果不是集合类型或数组类型则直接返回当前值return object;}3.2 参数调用 在org.apache.ibatis.binding.MapperMethod#execute方法中通过convertArgsToSqlCommandParam获取参数值。 public Object execute(SqlSession sqlSession, Object[] args) {Object result;switch (command.getType()) {case INSERT: {Object param method.convertArgsToSqlCommandParam(args);result rowCountResult(sqlSession.insert(command.getName(), param));break;}case UPDATE: {Object param method.convertArgsToSqlCommandParam(args);result rowCountResult(sqlSession.update(command.getName(), param));break;}case DELETE: {Object param method.convertArgsToSqlCommandParam(args);result rowCountResult(sqlSession.delete(command.getName(), param));break;}case SELECT:if (method.returnsVoid() method.hasResultHandler()) {executeWithResultHandler(sqlSession, args);result null;} else if (method.returnsMany()) {result executeForMany(sqlSession, args);} else if (method.returnsMap()) {result executeForMap(sqlSession, args);} else if (method.returnsCursor()) {result executeForCursor(sqlSession, args);} else {// 获取参数值Object param method.convertArgsToSqlCommandParam(args);result sqlSession.selectOne(command.getName(), param);if (method.returnsOptional() (result null || !method.getReturnType().equals(result.getClass()))) {result Optional.ofNullable(result);}}break;case FLUSH:result sqlSession.flushStatements();break;default:throw new BindingException(Unknown execution method for: command.getName());}if (result null method.getReturnType().isPrimitive() !method.returnsVoid()) {throw new BindingException(Mapper method command.getName() attempted to return null from a method with a primitive return type ( method.getReturnType() ).);}return result;}
http://www.pierceye.com/news/810124/

相关文章:

  • 网站下一步工作怎么做网上最好购物网站
  • OA 公司网站 铁道建设报自驾游网站建设
  • wordpress建站网站根目录短视频怎么赚钱
  • 亳州网站开发公司wordpress 添加分享
  • 如何查询网站接入信息移动网站开发框架
  • 河南做网站的百度竞价推广收费标准
  • 深圳的深圳的网站建设公司校园网站建设方向
  • 电商网站建设 解决方案的设计营销策略都有哪些方面
  • 菏泽网站建设兼职凡科网制作网站教程
  • 实验一 电子商务网站建设与维护北京网站设计培训学校
  • 周到的网站建设合肥建筑网站大全
  • 国外互联网资讯网站南宁网站制作费用
  • 建设公司网站要注意哪些蜜雪冰城推广软文
  • 做信息安全的网站博客网站的建设
  • 门户网站建设项目书提升学历是什么意思
  • 上海网站建设极简慕枫塘沽有哪些互联网公司
  • 社区网站如何做官方网站建设哪儿有
  • 做兼职的网站策划书大连中山网站建设
  • 中国摄影网站深圳网站建设龙华
  • 个人网站怎么建立深圳网站建站费用
  • 笔趣阁建站教程网页设计 网站建设啥意思
  • 海门网站开发西安响应式网站建设服务提供商
  • 自适应网站建站哈尔滨市建设安全监察网站
  • nas服务器可以做网站吗电商类网站开发方案
  • 免费的个人的网站网站建设 考虑
  • 医院网站建设的目的高端网站有哪些优势
  • 佛山网站建设首选如何备份wordpress
  • 优化稳定网站排名网站建设需要学什么语言
  • 可以做设计私单的网站硬件开发工程师面试
  • 竞价网站单页网页设计师中级证书有用吗