SpringBoot之mybatis配置版

本文详细介绍了如何在SpringBoot项目中集成MyBatis,包括添加依赖、编写mapper接口、XML映射文件、主配置文件配置、以及Controller测试。通过实例演示了从配置到调用的全过程。

SpringBoot之mybatis配置版

1.导入pom依赖
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
2.编写mapper接口

使用@Mapper注解标明mapper接口, 也可以不使用Mapper注解, 可以在任何springboot的配类上添加mapper扫描注解@MapperScan, 然后指定扫描包路径@MapperScan("com.ml.druid.mapper")

//@Mapper
public interface DepartmentMapper {

    public Integer insertDept(Department department);

    public Integer deleteDeptById(@Param("id") Integer id);

    public Integer updataDept(Department department);

    public Department selectDeptById(@Param("id") Integer id);
}
3.编写mapper接口的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.ml.druid.mapper.DepartmentMapper">

    <select id="selectDeptById" resultType="department" parameterType="integer">
        select * from department where id=#{id}
    </select>

    <insert id="insertDept" parameterType="department" useGeneratedKeys="true" keyProperty="id">
        Insert into department(department_name) values(#{departmentName})
    </insert>

    <delete id="deleteDeptById" parameterType="integer">
        delete from department where id=#{id}
    </delete>

    <update id="updataDept" parameterType="department">
        updata department set department_name=#{departmentName} where id=#{id}
    </update>
</mapper>
4.编写mybatis的主配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <package name="com.ml.druid.bean"/>
    </typeAliases>

    <mappers>
<!--        <mapper resource="mybatis/mapper/DepartmentMapper.xml"/>-->
        <package name="com.ml.druid.mapper"/>
    </mappers>

</configuration>

可以在mybatis的配置文件中指定mapper映射, 使用resource或者package的方法

但是注意如果使用package的方法, 需要保证mapper接口和mapper接口的xml映射需要在同一目录下

也可以在springboot的配置文件中配置mybatis的配置文件和mapper接口的xml映射文件的位置

mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
5.编写controller进行测试
@RestController
public class DeptController {

    @Autowired
    DepartmentMapper departmentMapper;

    @GetMapping("/dept/{id}")
    public Department getDeptById(@PathVariable("id") Integer id){
        return departmentMapper.selectDeptById(id);
    }

    @GetMapping("/dept")
    public Department insertDept(Department department){
        departmentMapper.insertDept(department);
        return department;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值