• 注解版


一、 在mapper包下创建接口

import com.bean.Department;
import org.apache.ibatis.annotations.*;


public interface DepartmentMapper {
    @Select("select * from department where id = #{id}")
    Department getDepById(Integer id);

    @Delete("delete from department where id = #{id}")
    int deleteDepById(Integer id);
    
    @Options(useGeneratedKeys = true, keyProperty = "id")//配置主键,自动递增
    @Insert("insert into department(department_name) values(#{departmentName})")
    int insertDep(Department department);
    
    @Update("update department set department_name=#{departmentName} where id=#{id}")
    int updateDep(Department department);
}

二、 在controller包下创建类

import com.bean.Department;
import com.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DepController {
    @Autowired
    DepartmentMapper departmentMapper;

    @GetMapping("/getDep/{id}")
    public Department getDep(@PathVariable("id") Integer id){
        return departmentMapper.getDepById(id);
    }

    @GetMapping("/insertDep")//http://localhost:8080/insertDep?departmentName=sky
    public Department insertDep(Department department){
        departmentMapper.insertDep(department);
        return department;
    }
}

三、 配置驼峰数据库列名的方式

  • 在config包下创建MyBatisConfig.java
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;

@org.springframework.context.annotation.Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setMapUnderscoreToCamelCase(true);
    }
}

四、 配置mappper包下省略@Mapper

  • 在主程序类上添加
    @MapperScan(value = "com.mapper")
    //使用MapperScan批量扫描所有的Mapper接口;

  • 配置文件版


文件路径图片:
批注 20200212 120349.jpg

一、 在mapper包下创建接口

import com.bean.Employee;

public interface EmployeeMapper {

    Employee getEmpById(Integer id);

    int insertEmp (Employee employee);

}

二、 配置文件

  • EmployeeMapper.xml
<!--
namespace:Mapper的路径
id:mapper里的方法名
resultType:mapper里的方法返回类型
-->
<mapper namespace="com.mapper.EmployeeMapper">
    <select id="getEmpById" resultType="com.bean.Employee">
    select * from employee where id = #{id}
    </select>

    <insert id="insertEmp">
        insert into employee(name,gender) values (#{name},#{gender})
    </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>
  • application.yml
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml  #指定全局配置文件的位置
  mapper-locations: classpath:mybatis/mapper/*.xml   #指定sql映射文件的位置

三、 controller

@GetMapping("/insertEmp")
public Employee insertEmp(Employee employee) {
    employeeMapper.insertEmp(employee);
    return employee;
}

@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id) {
    return employeeMapper.getEmpById(id);
}

hhhhh