mybatis_day01

实体类:略

接口:

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();
 }

}

 

 

 


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值