这篇文章给大家介绍springboot 中怎么整合fluent mybatis,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1.导入pom依赖
<!-- mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- 引入fluent-mybatis 运行依赖包, scope为compile -->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis</artifactId>
<version>1.6.8</version>
</dependency>
<!-- 引入fluent-mybatis-processor, scope设置为provider 编译需要,运行时不需要 -->
<dependency>
<groupId>com.github.atool</groupId>
<artifactId>fluent-mybatis-processor</artifactId>
<version>1.6.8</version>
</dependency>
2.配置数据库连接
spring.datasource.url= jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
3.创建数据库表
CREATE TABLE `student` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='学生表';
4.创建Student实体类,
①实体类添加 @FluentMybatis
②实现 IEntity 接口
@FluentMybatis
@Data
@NoArgsConstructor
public class Student implements IEntity {
private Long id;
private String name;
private Integer age;
}
5.重新构建项目
构建完成后,target目录下就会新建几个文件夹
6. 测试
@Autowired
private StudentMapper studentMapper; // target目录下
@RequestMapping("insert")
public void insert(){
Student student = new Student();
student.setName("dl");
student.setAge(25);
studentMapper.insert(student);
}
数据库已插入
************************************
如果出现Mapper文件找不到路径的异常,很可能是在之前idea中将target文件隐藏了,只需
File --> setting --> File Types 将忽视的target文件删掉就可以了
关于springboot 中怎么整合fluent mybatis就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。