连接数据库分为六步
package com.leo;
import com.leo.dao.ICardMapping;
import com.leo.entity.Card;
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 Main {
static final String res = "mybatis-config.xml";
public static void main(String[] args) throws IOException {
InputStream config = Resources.getResourceAsStream(res);
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(config);
SqlSession sqlSession = sqlSessionFactory.openSession();
ICardMapping iCardMapping = sqlSession.getMapper(ICardMapping.class);
List<Card> cards = iCardMapping.getAllCard();
for (Card card : cards) {
System.out.println(card);
}
sqlSession.close();
config.close();
}
}
输出结果