整合MyBatis(springboot)
2024-08-28 13:39:29
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映射文件的位置
最新文章
- Eclipse创建Maven工程报错
- cocos初认识
- Java设计模式(一)——代理模式
- Linux的一些常用快捷键和基本命令
- Xml序列化UTF-8格式错误
- [ html canvas getImageData Object.data.length ] canvas绘图属性 getImageData Object.data.length 属性讲解
- npm 打包 electron app 报错问题
- C#启动进程之Process
- mysql deadlock
- C++语法细节笔记
- butternife Zelezny自动注入插件
- 通过flask实现web页面简单的增删改查bootstrap美化版
- 22.LinkedList
- Luogu1081 NOIP2012 开车旅行 倍增
- iOS 11开发教程(二十)iOS11应用视图美化按钮之设置按钮的状态
- event.stopPropagation(),event.preventDefault()和return false的区别
- OpenCV 图形变换
- CSS 实例之打开大门
- Python3:if __name__ == &#39;__main__&#39; 详解
- VS Code 折腾记 - (5) Angular 2+ &;&; Typescript 2 + 必备插件推荐