整合MyBatis

准备工作 : 数据库建表 创建javaBean

1.实现注解版MyBatis

@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")//标明自增id 
    @Insert("insert into department (departmentName) values (#{departmentName})")
    public int insertDept(Department department);

    @Update("update department set departmentName=#{departmentName} where id=#{id}")
    public int updateDept(Department department);
}

测试Controller

@RestController
public class DeptController {
    @Autowired
    DepartmentMapper departmentMapper;
    @GetMapping("/dept/{id}")
    public Department getDept(@PathVariable("id") Integer id){
        return departmentMapper.getDeptById(id);
    }
    @GetMapping("/dept")
    public Department insertDept(Department department) {
        departmentMapper.insertDept(department);
        return department;
    }
}

插入:http://localhost:8080/dept?departmentName=business

image.png

查询:http://localhost:8080/dept/4
image.png

自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;

@Configuration
public class MybatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer() {
            @Override
            public void customize(Configuration configuration) {
                ///开启驼峰命名
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

使用MapperScan批量扫描

@MapperScan(value = "com.xxx.mapper")//mapper所在的目录文件夹
@SpringBootApplication
public class JdbcdemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(JdbcdemoApplication.class, args);
    }
}

2.配置文件版

mapper:

//@Mapper 或者 @MapperScan将接口扫描装配到容器中
public interface EmployeeMapper {
    public Employee getEmployeeById(Integer id);

    public void insertEmp(Employee employee);
}

application.yml 或者application.properties里面指定全局配置文件和sql映射文件位置

mybatis:
   config‐location: classpath:mybatis/mybatis‐config.xml 指定全局配置文件的位置
   mapper‐locations: classpath:mybatis/mapper/*.xml 指定sql映射文件的位置

全局配置文件: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>

指定sql映射文件

<?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.ari.ss.jdbcdemo.mapper.EmployeeMapper">
    <!--public Employee getEmployeeById(Integer id);-->
    <!--public void insertEmp(Employee employee);-->
    <select id="getEmployeeById" resultType="com.ari.ss.jdbcdemo.bean.Employee">
        select * from employee where id = #{id}
    </select>
    <insert id="insertEmp">
        insert into employee (lastName,email,gender,d_id) VALUES (#{lastName},#{email},#{gender},#{d_id})
    </insert>
</mapper>

测试同注解版

更多配置见官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,264评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,224评论 6 342
  • 1 版本说明 springboot:2.0 jdk:1.8 2 创建springBoot项目 创建项目时勾选必要w...
    Java小铺阅读 4,187评论 2 5
  • 最近做项目用到springboot整合mybatis,security。将其中遇到的问题做一个总结 注:本项目全程...
    huoyl0410阅读 4,166评论 1 2
  • 月色朦胧,琴声悠扬于耳畔环绕,女儿鼾声悄然响起,于这样寂静的夜里,思绪飞扬,我却不愿睡去…… 回首过往...
    梦里飘香阅读 2,948评论 0 0

友情链接更多精彩内容