Mybatis使用总结 混用

Mybatis使用总结 混用

1.Mybatis注解版
 1.已经具备 bean:
   Department
   Employee
   数据表
   数据库连接成功
  
 2.步骤:
   2.1 编写操作每一个表对应的 mapper 接口 (最好写一个mapper包)
   例如:
   public interface DepartmentMapper {
 }
   2.2 接口所需要的注解为:@Mapper   指定这是一个操作数据库的Mapper
   2.3 定义方法 操作数据库表 
   2.4 直接通过注解标明方法完成语句
   例如:
   @Mapper
 public interface DepartmentMapper {
 
     @Select("select * from department where id=#{id}")
     public Department getDeptById(Integer id);
 
     @Delete("delete from department where id=#{id}")
     public int deleteDeptById(Integer id);
 
     @Options(useGeneratedKeys = true,keyProperty = "id")
     @Insert("insert into department(department_name) values(#{departmentName})")
     public int insertDept(Department department);
 
     @Update("update department set department_name=#{departmentName} where id=#{id}")
     public int updateDept(Department department);
 }
 
    2.5 编写Controller进行测试 其中注入mapper文件
    例如:
    @RestController
     public class DeptController {
 
     @Autowired
     DepartmentMapper departmentMapper;
 
     @GetMapping("/dept/{id}")
     public Department getDepartment(@PathVariable("id") Integer id){
         return departmentMapper.getDeptById(id);
     }
 
     @GetMapping("/dept")
     public Department insertDept(Department department){
         departmentMapper.insertDept(department);
         return department;
     }
 }
     2.6 不需要做配置 MybatisAutoConfiguration
     2.7 可以在语句中 定义主键 
       例如:
     @Options(useGeneratedKeys = true,keyProperty = "id")
     @Insert("insert into department(department_name) values(#{departmentName})")
     public int insertDept(Department department);
     2.8 驼峰命名法 配置
     "insert into department(department_name) values(#{departmentName})"
     "insert into department(departmentName) values(#{departmentName})"
      bean中  private String departmentName;
     如何对应
     解决方案:自定义Mybatis配置规则
     MybatisConfig
       
     @org.springframework.context.annotation.Configuration
     public class MyBatisConfig {
 
     @Bean
     public ConfigurationCustomizer configurationCustomizer(){
         return new ConfigurationCustomizer(){
 
             @Override
             public void customize(
                     Configuration configuration) {
                 configuration.setMapUnderscoreToCamelCase(true);
             }
         };
     }
 }
      2.9   Mapper包中 mapper文件太多 每一个添加@Mapper太费事
        解决:
        使用MapperScan批量扫描所有的Mapper接口;
        @MapperScan(value = "com.terry.mybatisdemo.mapper")
        可以写在启动类上
 
2.Mybatis文件版
1.编写操作每一个表对应的 mapper 接口 (最好写一个mapper包)
  例如:
  public interface EmployeeMapper {
}
2.定义方法
  //@Mapper或者@MapperScan将接口扫描装配到容器中
  public interface EmployeeMapper {

    public Employee getEmpById(Integer id);

    public void insertEmp (Employee employee);
}
3.在resources文件下建立 Mybatis文件夹 Mybatis文件夹下创建 mapper文件夹
  参考文档:https://mybatis.org/mybatis-3/getting-started.html
  3.1 在resources文件下建立全局配置文件 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>
  
  3.2 在mapper文件下 创建sql映射文件 对应mapper接口类內的方法的实现 例如:
      EmployeeMapper.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.terry.mybatisdemo.mapper.EmployeeMapper">
    <select id="getEmpById" resultType="com.terry.mybatisdemo.bean.Employee">
    select * from employee where id = #{id}
    </select>

    <select id="insertEmp">
    insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{d_id})
    </select>

</mapper>
      
      在sql映射文件內 绑定接口 绑定返回值
   
  3.3 在 application.yml 配置  全局配置文件  sql映射文件 
      例如:
  mybatis:
  # 指定全局配置文件位置
   config-location: classpath:mybatis/mybatis-config.xml
  # 指定sql映射文件位置
   mapper-locations: classpath:mybatis/mapper/*.xml
   
  4 编写Controller进行测试 其中注入mapper文件
   例如:
   @RestController
   public class DeptController {

    @Autowired
    EmployeeMapper employeeMapper;

    @GetMapping("/emp/{id}")
    public Employee getEmp(@PathVariable("id") Integer id){
       return employeeMapper.getEmpById(id);
    }
}
   5.全局配置文件 配置驼峰命名设置为true
   
   <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值