mybatis动态标签——sql标签
mapper接口
Emp getEmpById(@Param("id") Integer id);
mapper.xml
<!-- sql片段:可以记录一段sql,在需要用的地方使用include标签进行引用 --> <sql id="empColumns"> emp_id,emp_name,age,gender,dept_id </sql> <select id="getEmpById" resultType="com.xxx.dynamicSQL.entity.Emp"> select <include refid="empColumns"></include> from t_emp where emp_id = #{id} </select>
测试代码:
@Test public void testGetEmpByid(){ SqlSession sqlSession = SqlSessionUtil.getSqlSession(); EmpMapper mapper = sqlSession.getMapper(EmpMapper.class); Emp emp = mapper.getEmpById(2); System.out.println(emp); }