SpringBoot整合Mybatis -- 注解版

1、引入mybatis-spring-boot-starter

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

2、编写实体类Department

使用注解@Data可以自动生成getter、setter、toString()等方法

前提是:需要引入lombok , 同时安装插件Lombok

<!--引入lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.4</version>
            <scope>provided</scope>
        </dependency>
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department implements Serializable {
    private Integer id;
    private String departmentName;

}

 

3、注解的方式编写mapper

@Mapper注解:为了把mapper交給Spring管理,不再写mapper映射文件。

@Mapper //指定这是操作数据库的mapper
public interface DepartmentMapper {
    @Select("select * from department where id = #{id}")
    Department queryDepById(Integer id);

    @Delete("delete from department where id = #{id}")
    int deleteDepById(Integer id);

    @Insert("insert into department(departmentName) values(#{departmentName})")
    int insert(Department dept);

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

}

 

不过,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦。

可以使用@MapperScan注解:使用MapperScan批量扫描所有的Mapper接口

       通过使用@MapperScan可以指定要扫描的Mapper类的包的路径

@SpringBootApplication
@MapperScan(basePackages = "com.dhu.mapper")
public class SpringbootDataMybatisApplication {

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

}

4、编写controller

@Controller
public class DeptController {

    @Autowired
    private DepartmentMapper mapper;

    @GetMapping("/dept/{id}")
    @ResponseBody
    public Department findDepById(@PathVariable("id") Integer id){
        Department department = mapper.queryDepById(id);
        return department;
    }

    @GetMapping("/dept")
    @ResponseBody
    public Department add(Department dept) {
        int out = mapper.insert(dept);
        return dept;
    }

}

5、访问

6、显示插入数据的id

添加@Options注解

@Insert("insert into department(departmentName) values(#{departmentName})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Department dept);

7、mybatis驼峰命名(注解版)

(1)若我们是xml配置

<configuration>
    <settings>
      <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
(2)现在是注解配置

@Insert("insert into department(department_name) values(#{departmentName})")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    int insert(Department dept);

需要编写一个配置文件,自己设置

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值