select no,name from tb_employee mybatis写法
时间: 2023-11-22 11:54:19 浏览: 75
mybatis的用法
在 MyBatis 中,可以使用以下方式来实现 select no,name from tb_employee 的查询:
1.在 Mapper XML 文件中写 SQL:
```xml
<select id="getEmployeeList" resultType="map">
select no,name from tb_employee
</select>
```
2.在 Mapper 接口中定义方法:
```java
public interface EmployeeMapper {
List<Map<String, Object>> getEmployeeList();
}
```
3.在 XML 映射文件中配置 SQL 语句的 ID 和接口方法的映射关系:
```xml
<mapper namespace="com.example.EmployeeMapper">
<select id="getEmployeeList" resultType="map">
select no,name from tb_employee
</select>
</mapper>
```
4.在代码中调用接口方法:
```java
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
List<Map<String, Object>> employeeList = employeeMapper.getEmployeeList();
} finally {
sqlSession.close();
}
```
阅读全文