实体类:略
接口:
public interface BankAccountDAO {
int bankAccountCount();
}
映射器:.xml
<mapper namespace="com.xdl.dao.BankAccountDAO">
<!-- 定义SQL语句 -->
<select id="bankAccountCount" resultType="int">
select count(*) from bank_account
</select>
</mapper>
配置数据源:
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver"
value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott" />
<property name="password" value="tiger" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/xdl/mapper/BankAccountMapper.xml" />
</mappers>
</configuration>
测试:
public class BankAccountTest {
public static void main(String[] args) {
//获取SqlSession
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
//获得流 构建inputStream 读文件 当前类得到类加载器然后得到流
InputStream inputStream = BankAccountTest.class.
getClassLoader().
getResourceAsStream("sqlmap-config.xml");
SqlSessionFactory ssf = ssfb.build(inputStream);
SqlSession sqlSession = ssf.openSession();
//查询 selectOne("") 中写sql语句中id
int count = sqlSession.selectOne("bankAccountCount");
System.out.println("count="+count);
//关闭资源
sqlSession.close();
}
}