初学MyBatis,有哪些入门技巧分享?

更新于
2026-10-12 12:16:08
1阅读来源:SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计454个文字,预计阅读时间需要2分钟。

初学MyBatis,有哪些入门技巧分享?

javapublic class Test { static SqlSessionFactory sqlSessionFactory;

public static void main(String[] args) { SqlSession session1=getSqlSession(); // 通过接口实现,避免硬编码 IUserInfoDao dao=session1.getMapper(IUserInfoDao.class); }}

初学MyBatis,有哪些入门技巧分享?

gistfile1.txt

public class Test { static SqlSessionFactory sqlSessionFactory; public static void main(String[] args) { SqlSession session1 = getSqlSession(); //面向接口,必须这样实现 IUserInfoDao dao = session1.getMapper(IUserInfoDao.class); System.out.println(dao.getUserById(1)); } private static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } static{ try { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { throw new RuntimeException(e); } } } mybatis.xml

UserInfoMapper.xml

select seq.nextval from dual insert into userinfo values(#{id},#{name},#{pwd},#{rolesid}) 包结构.PNG

本文共计454个文字,预计阅读时间需要2分钟。

初学MyBatis,有哪些入门技巧分享?

javapublic class Test { static SqlSessionFactory sqlSessionFactory;

public static void main(String[] args) { SqlSession session1=getSqlSession(); // 通过接口实现,避免硬编码 IUserInfoDao dao=session1.getMapper(IUserInfoDao.class); }}

初学MyBatis,有哪些入门技巧分享?

gistfile1.txt

public class Test { static SqlSessionFactory sqlSessionFactory; public static void main(String[] args) { SqlSession session1 = getSqlSession(); //面向接口,必须这样实现 IUserInfoDao dao = session1.getMapper(IUserInfoDao.class); System.out.println(dao.getUserById(1)); } private static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); } static{ try { String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (Exception e) { throw new RuntimeException(e); } } } mybatis.xml

UserInfoMapper.xml

select seq.nextval from dual insert into userinfo values(#{id},#{name},#{pwd},#{rolesid}) 包结构.PNG