使用接口结合xml文件
1)创建dao包,在包下创建接口,该接口要和映射文件匹配
1 package com.zhiyou.clg.dao; 2 import java.util.List; 3 import org.apache.ibatis.annotations.Param; 4 import com.zhiyou.clg.bean.User; 5 public interface UserDao { 6 List<User> selectUser2(int id); 7 }
2)映射文件中的namespace一定要和接口所在的包以及接口的名字一样
3)标签中的id一定和接口中方法的名字对照
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.zhiyou.clg.dao.UserDao"> 5 <select id="selectUser2" parameterType="int" 6 resultType="com.zhiyou.clg.bean.User"> 7 select * from user where id=#{id} 8 </select> 9 </mapper>
4)测试:.
1 @Test 2 void testselectUser2() { 3 UserDao userDao = session.getMapper(UserDao.class); 4 List<User> list = userDao.selectUser2(1); 5 System.out.println(list); 6 }