• 整合MyBatis(springboot)


    pom文件:

    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
    </dependency>

    步骤:

    ​ 1)、配置数据源相关属性(见上一节Druid)

    ​ 2)、给数据库建表

    ​ 3)、创建JavaBean

    注解版:

    //指定这是一个操作数据库的mapper
    @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(departmentName) values(#{departmentName})")
        public int insertDept(Department department);
    
        @Update("update department set departmentName=#{departmentName} where id=#{id}")
        public int updateDept(Department department);
    }

    问题:

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

     
    @org.springframework.context.annotation.Configuration
    public class MyBatisConfig {
    ​
        @Bean
        public ConfigurationCustomizer configurationCustomizer(){
            return new ConfigurationCustomizer(){
    ​
                @Override
                public void customize(Configuration configuration) {
                    configuration.setMapUnderscoreToCamelCase(true);
                }
            };
        }
    }

    配置文件版:

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

     
     
     
     
     
     
     
     
     
  • 相关阅读:
    ex01 温度转换1
    12 字典的遍历在 Python2 与 Python3 中区别
    11 序列中有多个最值时会索引出哪个
    10 isalpha() 对于字母的定义
    09 Python3 的深拷贝与浅拷贝
    08 in
    07 len()
    06 “杠零”与空字符
    导航点击字体变色
    清除浮动
  • 原文地址:https://www.cnblogs.com/MagicAsa/p/10718900.html
Copyright © 2020-2023  润新知