mybatis-config.xml的编写
Mybatis工具类的编写,用于获取SqlSession对象
private static SqlSessionFactory sqlSessionFactory=null; static { try { //获取SqlSessionFactory对象 String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); }
Mapper.xml
select * from ljqdb.user
测试类的编写
SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); Listall = mapper.findAll(); for (User user : all){ System.out.println(user); } sqlSession.close();
由于maven约定大于配置,解决配置文件无法到处或者生效的问题
src/main/resources ***.xml true src/main/java ***.xml true
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)