SpringBoot用XML文件的开发方式

看一看通过注解开发

首先创建一个Mapper接口并写查询所有小车方法

@Mapper
public interface CarXmlDao {

    List<Car> allCar();

}

要在启动类里加入注解是别接口

其次在resource目录下创建mapper包,并在其中创建CarMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sy.dao.CarXmlDao">
    <select id="allCar" resultType="car">
        select * from car
    </select>

</mapper>

然后,我们在Application.yml文件中配置xml文件的所在位置以及pojo类的别名扫描

mybatis:
  #Mapper.xml文件的所在位置
  mapper-locations: classpath:mapper/*Mapper.xml
  #pojo类的别名扫描
  type-aliases-package: com.sy.pojo

最后、我们在测试类调用dao接口并运行

@Autowired
    private CarXmlDao carXmlDao;

    @Test
    public void xmlTest(){
        List<Car> cars = carXmlDao.allCar();

        System.out.println(cars);

    }

我们来看打印结果

[Car{carId=1, carMoney=0}, Car{carId=2, carMoney=0}, Car{carId=3, carMoney=0}, Car{carId=4, carMoney=0}]
经验分享 程序员 微信小程序 职场和发展