1、在数据库中创建User表
2、创建Maven项目
3、导入依赖
org.mybatis mybatis3.5.7 mysql mysql-connector-java5.1.47 com.alibaba druid1.2.6 org.projectlombok lombok1.18.6 src/main/java ** public static SqlSession openSqlSession() throws IOException { return sqlSessionFactory.openSession(); } public static void commitAndClose(SqlSession sqlSession) { if (sqlSession != null) { sqlSession.commit(); sqlSession.close(); } } public static void rollbackAndClose(SqlSession sqlSession) { if (sqlSession != null) { sqlSession.rollback(); sqlSession.close(); } } }
6、创建UserMapper接口
public interface UserMapper { ListfindAll(); }
7、创建UserMapper.xml
select * from user
8、进行测试
public static void main(String[] args) { SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); Listall = mapper.findAll(); for (User user : all) { System.out.println(user); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)