时间:2021-07-01 10:21:17 帮助过:3人阅读
除了那些mapper,因为已经指定了路径。因此还需要在pom的resources标签里指定,为了maven复制过去。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
7. 最后使用
String resouces = "mybatis_config.xml";
InputStream in = Resources.getResourceAsStream(resouces);
//读取配置文件的配置信息,利用SqlSessionFactoryBuilder创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//利用sqlSessionFactory打开与数据库的会话
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UsersEntityMapper ueMapper = sqlSession.getMapper(UsersEntityMapper.class);//通过sqlSession得到mapper
String no = "13299999999";
UsersEntity user = ueMapper.getUsersByPhoneNo(no);//调用mapper的方法
//只读操作不需要sqlSession.commit()
System.out.println("查询到用户...");
System.out.println(user.getUsername());
} finally {
sqlSession.close();
}
maven项目使用mybatis+mysql
标签:ack pac tran ret tst return group 打开 version