广东企业网站模板推荐,天津工商网上办事大厅,个性化网站建设公司电话,网站建设排行#{}#xff1a;可以获取map中的值或者pojo对象属性的值${}#xff1a;可以获取map中的值或者pojo对象属性的值 区别#xff1a; #{}#xff1a;是以预编译的形式#xff0c;将参数设置到sql语句中#xff0c;相当于原生jdbc的PreparedStatement#xff1b;防止sql注入${…#{}可以获取map中的值或者pojo对象属性的值${}可以获取map中的值或者pojo对象属性的值 区别 #{}是以预编译的形式将参数设置到sql语句中相当于原生jdbc的PreparedStatement防止sql注入${}取出的值直接拼接在sql语句中会有安全问题大多情况下我们取参数的值都应该去使用#{} DEBUG 01-23 09:00:50,805 Preparing: select * from tb1_employee where id 1 and last_name ? (BaseJdbcLogger.java:159)
DEBUG 01-23 09:00:50,835 Parameters: Tom(String) (BaseJdbcLogger.java:159)
DEBUG 01-23 09:00:50,852 Total: 0 (BaseJdbcLogger.java:159) 原生jdbc不支持占位符的地方我们就可以使用${}进行取值 比如分表按照年份分表拆分排序等
select * from 2017_salary where xxx;写成
select * from ${year}_salary where xxx;
select * from tb1_employee order by ${f_name} ${order}example !--public Employee getEmpByMap(MapString,Object map);--select idgetEmpByMap resultTypecom.atguigu.mybatis.bean.Employeeselect * from ${tableName} where id #{id} and last_name #{lastName}/selectTestpublic void test03() throws IOException {SqlSessionFactory sqlSessionFactory getSqlSessionFactory();SqlSession sqlSession sqlSessionFactory.openSession();try{EmployeeMapper mapper sqlSession.getMapper(EmployeeMapper.class);MapString,Object map new HashMap();map.put(id,1);map.put(lastName,Tom);map.put(tableName,tb1_employee);Employee employee mapper.getEmpByMap(map);System.out.println(employee);}finally {sqlSession.close();}}