欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Springboot整合MyBatis(配置文件版)

程序员文章站 2022-07-12 22:50:50
...

官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

接口
Springboot整合MyBatis(配置文件版)

Springboot整合MyBatis(配置文件版)

application.yml

Springboot整合MyBatis(配置文件版)

EmployeeMapper.xml

Springboot整合MyBatis(配置文件版)

<?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.example.demomybatis.mapper.EmployeeMapper">
    <!--    public Employee getEmpById(Integer id);

     public void insertEmp(Employee employee);-->
    <select id="getEmpById" resultType="com.example.demomybatis.bean.Employee">
        SELECT * FROM employee WHERE id=#{id}
    </select>

    <insert id="insertEmp">
        INSERT INTO employee(lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{dId})
    </insert>
</mapper>

全局配置文件
mybatis-config.xml

<?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>
</configuration>

添加数据

Springboot整合MyBatis(配置文件版)

Springboot整合MyBatis(配置文件版)

相关标签: springboot