这篇文章主要介绍Fluent mybatis有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、啥是Fluent-Mybatis与Mybatis-Plus类似,是对Mybaits进一步的封装,使之语法简洁明了,
这篇文章主要介绍Fluent mybatis有什么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
与Mybatis-Plus类似,是对Mybaits进一步的封装,使之语法简洁明了,更重要的是不需要在自主创建Xml文件,可以只用一个实体类对象,通过代码生成器,在编译的过程中生成所需要的各类文件,简化了项目的基础构建,提高开发效率。
DROP TABLE IF EXISTS `t_user`;create table `t_user`( id bigint auto_increment comment '主键ID' primary key, name varchar(30) charset utf8 null comment '姓名', age int null comment '年龄', email varchar(50) charset utf8 null comment '邮箱', gmt_create datetime null comment '记录创建时间', gmt_modified datetime null comment '记录最后修改时间', is_deleted tinyint(2) default 0 null comment '逻辑删除标识');
创建一个springboot项目,pom文件如下
<?xml version="1.0" encoding="UTF-8"?><project xmlns="Http://Maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mybatis.fluent</groupId> <artifactId>fluent_mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>fluent_mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <fluent.mybatis.version>1.5.6</fluent.mybatis.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--JDBC驱动--> <dependency> <groupId>mysql</groupId> <artifactId>Mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <!-- fluent mybatis依赖--> <dependency> <groupId>com.GitHub.atool</groupId> <artifactId>fluent-mybatis</artifactId> <version>${fluent.mybatis.version}</version> </dependency> <dependency> <groupId>com.github.atool</groupId> <artifactId>fluent-mybatis-processor</artifactId> <version>${fluent.mybatis.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build></project>
代码生成器
import cn.org.atool.generator.FileGenerator;import cn.org.atool.generator.annotation.Table;import cn.org.atool.generator.annotation.Tables;public class AppEntityGenerator { public static void main(String[] args) { FileGenerator.build(Abc.class); } @Tables( url = "jdbc:mysql://IP:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai", username = "XXXXXX", passWord = "XXXXXX", basePack = "com.mybatis.fluent.fluent_mybatis", srcDir = "/src/main/java", daoDir = "/src/main/java", gmtCreated = "gmt_create", gmtModified = "gmt_modified", logicDeleted = "is_deleted", tables = @Table(value = {"t_user"}) ) static class Abc { }}
需要注意,默认的是Mysql数据库,如果需要其他数据库,需要手动配置dbType和driver,其他选项按照需要修改。例如oralce
import cn.org.atool.generator.FileGenerator;import cn.org.atool.generator.annotation.Table;import cn.org.atool.generator.annotation.Tables;import cn.org.atool.generator.database.DbType;public class AppEntityGenerator { public static void main(String[] args) { FileGenerator.build(Abc.class); } @Tables( dbType= DbType.oracle, driver= "oracle.jdbc.OracleDriver", url = "jdbc:oracle:thin:@IP:1521:orcl", username = "XXXXXX", password = "XXXXXX", basePack = "com.mybatis.fluent.fluent_mybatis", srcDir = "/src/main/java", daoDir = "/src/main/java", gmtCreated = "INSERT_DATE_TIME", tables = @Table(value = {"table_name"}) ) static class Abc { }}
main方法启动执行,生成的项目结构,如果在执行是出现异常
需要暂时注释
当生成好对应文件,项目目录如下
此时TUserDaoImpl会有错误
此时将需要将之前注释的provided打开,在执行
执行过后,异常消除。此时你就拥有的对数据库此表的强大操作能力。
测试CURD
import cn.org.atool.fluent.mybatis.model.IPagedList;import cn.org.atool.fluent.mybatis.model.StdPagedList;import com.mybatis.fluent.fluent_mybatis.dao.impl.HospitalinfoDaoImpl;import com.mybatis.fluent.fluent_mybatis.dao.impl.TUserDaoImpl;import com.mybatis.fluent.fluent_mybatis.entity.HospitalinfoEntity;import com.mybatis.fluent.fluent_mybatis.entity.TUserEntity;import com.mybatis.fluent.fluent_mybatis.helper.TUserWrapperHelper;import com.mybatis.fluent.fluent_mybatis.wrapper.HospitalinfoQuery;import com.mybatis.fluent.fluent_mybatis.wrapper.TUserQuery;import org.junit.jupiter.api.Test;import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;import java.io.Serializable;import java.util.List;@SpringBootTestclass FluentMybatisApplicationTests { @Resource private TUserDaoImpl dao; @Test void add() { TUserEntity entity = new TUserEntity().setAge(20).setEmail("122@163.com").setName("lisi").setIsDeleted(false); Serializable id = dao.save(entity); System.out.println("插入后返回的主键ID为:" + id); } @Test void select(){ TUserQuery query = TUserQuery.query().limit(0,1); List<TUserEntity> list = dao.listEntity(query); System.out.println(list); } @Test void upData(){ TUserEntity entity = dao.selectById(1L); System.out.println(entity); entity.setAge(2000).setEmail("120098922@qq.com").setName("lisi111").setIsDeleted(true); System.out.println(entity); boolean b = dao.updateById(entity); System.out.println(b); } @Test void delete(){ boolean b = dao.deleteById(1L); System.out.println(b); TUserQuery query = TUserQuery.query(); List<TUserEntity> list = dao.listEntity(query); System.out.println(list); }}
以上是“Fluent Mybatis有什么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网精选频道!
--结束END--
本文标题: Fluent Mybatis有什么用
本文链接: https://lsjlt.com/news/299694.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0