1、使Mapper接口和SQL映射文件放置在同一文件夹下,可以在resources文件夹下新建一个和SQL映射文件相同的文件夹(例如:com/avb/Mapper)不能用.分割文件夹,用/
2、把SQL映射表里的namespace属性为Mapper接口的全限定名
3、在Mapper中定义方法,方法名是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
List<User> selectAll();
修改resource
4、编码
import com.avb.Mapper.UserMapper; import com.avb.pojo.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; import java.util.List; public class MyBatisDemo { public static void main(String[] args) throws IOException { //1、加载mybatis的核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2、获取SqlSession对象,用它来执行sql SqlSession sqlSession = sqlSessionFactory.openSession(); //3、执行sql /* List<User> users = sqlSession.selectList("test.selectAll"); System.out.println(users);*/ //Class<UserMapper> userMapperClass = UserMapper.class; UserMapper usermapper = sqlSession.getMapper(UserMapper.class); List<User> users = usermapper.selectAll(); System.out.println(users); //释放资源 sqlSession.close(); } }
标签:Mapper,javaweb,--,org,List,SQL,apache,import From: https://www.cnblogs.com/L-1906/p/17803505.html