mybatis步骤

1.在pom.xml中引入jar文件

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>${mybatis-v}</version>
</dependency>
<!--驱动包-->
<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.0.33</version>
</dependency>
  1. 在resources下创建mybatis.xml主配置文件
<?xml version="1.0" encoding="UTF-8" ?>
  1. 在resources下放入jdbc.properties,创建pojo实体类

  2. 在主配置文件mybatis.xml中添加代码

  3. 创建dao包,并编写BillDao接口
    /**

  • @Author: wzy
  • @Date: 2024/11/19 9:34
  • @Description: 订单Dao接口
    /
    public interface BillDao {
    /
    *
    • 根据ID 获得单个对象信息
    • @param id
    • @return
      */
      public Bill findBillById(Integer id);
      }
  1. 在resources下创建mappers文件夹,在mappers下创建BillDao.xml文件(实现类)
    复制文档中头部信息
<?xml version="1.0" encoding="UTF-8" ?>
  1. 在BillDao.xml中添加实现方法


    select id,billCode,productName,productDesc
    from smbms_bill a where id = #{id}

  2. 创建单元测试类
    try {
    //读取mybatis.xml
    InputStream is = Resources.getResourceAsStream(“mybatis.xml”);
    //创建连接工厂 SqlSessionFactory SqlSession数据库连接
    SqlSessionFactory sqlSessionFactory =
    new SqlSessionFactoryBuilder().build(is);
    //获得连接 true事务自动提交
    SqlSession sqlSession = sqlSessionFactory.openSession(true);

         //实例化dao层  调用dao接口
         BillDao billDao = sqlSession.getMapper(BillDao.class);
         Bill bill = billDao.findBillById(1);
         System.out.println(bill.toString());
         
     } catch (Exception e) {
         e.printStackTrace();
     }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值