• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

mybatis-ext: MyBatis-Ext是MyBatis的增强扩展,简化了MyBatis对单表增删改查的操作, ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

mybatis-ext

开源软件地址:

https://gitee.com/cjbi/mybatis-ext

开源软件介绍:

MyBatis-Ext - 方便易用的MyBatis增强扩展

概述

MyBatis-Ext是MyBatis的增强扩展,简化了MyBatis对单表增删改查的操作,提供通用的增删改查,支持函数式编程,支持分页查询,支持用户自定义通用方法,SQL无注入,集成简单,只做增强不做修改。

原理解析

快速入门

1. 引入Maven依赖

<dependencys>    <!-- mybatis-ext核心模块 -->    <dependency>        <groupId>tech.wetech.mybatis</groupId>        <artifactId>mybatis-ext-core</artifactId>        <!--  Use the latest released version:        https://repo1.maven.org/maven2/tech/wetech/mybatis/         -->        <version>LATEST_VERSION</version>    </dependency>    <!-- springframework请引入此模块(已包含以上依赖) -->    <dependency>        <groupId>tech.wetech.mybatis</groupId>        <artifactId>mybatis-ext-spring</artifactId>        <version>LATEST_VERSION</version>    </dependency>    <!-- spring-boot请引入此模块(已包含以上依赖) -->    <!-- 请注意:引入mybatis-ext-spring-boot-starter无需再引入mybatis-spring-boot-starter -->    <dependency>        <groupId>tech.wetech.mybatis</groupId>        <artifactId>mybatis-ext-spring-boot-starter</artifactId>        <version>LATEST_VERSION</version>    </dependency></dependencys>

2. 在实体类添加注解

//Mybatis-ext使用了Jpa的注解,目前实现了@Table、@Id、@Column、@Transient、@Version,未来考虑支持更多Jpa特性@Table(name = "weshop_user")//指定表名,必须@Where(clause = "del_flag = 0 ")//删除条件public class User {    @Id//指定Primary Key,必须    @GeneratedValue // 指定返回主键    private Integer id;    private String username;    private String password;    @Column(name = "gender")//指定指定字段映射,非必须,不指定字段名驼峰转下划线    private String gender;    private Date birthday;    @Column(name = "register_time")    private Date registerTime;    @Column(name = "last_login_time")    private Date lastLoginTime;    @Column(name = "last_login_ip")    private String lastLoginIp;    @Column(name = "user_level_id")    private Byte userLevelId;    private String nickname;    private String mobile;    @Column(name = "register_ip")    private String registerIp;    private String avatar;    @Column(name = "wechat_open_id")    private String wechatOpenId;    @Transient//忽略该属性    private String pageSize;    @Transient    private String pageNumber;    private Integer delFlag;    //此处省略getter,setter}

3. 继承BaseMapper接口

public interface UserMapper extends BaseMapper<User> {}

Mybatis-ext提供了对原生Java、Springframework以及SpringBoot的支持。

Ⅰ.在Java中使用

public class MybatisExtTests {    private static SqlSession sqlSession;    private final Logger log = LoggerFactory.getLogger(MybatisExtTests.class);    @BeforeClass    public static void beforeClass() {        //新建一个连接池方式的数据源工厂        PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory();        //设置数据源        Properties properties = new Properties();        properties.setProperty("driver", "<JDBC驱动>");        properties.setProperty("url", "<JDBC URL>");        properties.setProperty("username", "<用户名>");        properties.setProperty("password", "<密码>");        pooledDataSourceFactory.setProperties(properties);        DataSource dataSource = pooledDataSourceFactory.getDataSource();        //新建会话工厂        TransactionFactory transactionFactory = new JdbcTransactionFactory();        Environment environment = new Environment("development", transactionFactory, dataSource);        Configuration configuration = new ExtConfiguration(environment); //此处使用ExtConfiguration        configuration.setLogImpl(Log4jImpl.class);        //添加Mapper映射        configuration.addMapper(UserMapper.class);        //此处使用ExtSqlSessionFactoryBuilder        SqlSessionFactory sqlSessionFactory = new ExtSqlSessionFactoryBuilder().build(configuration);        sqlSession = sqlSessionFactory.openSession();    }    @Test    public void testSelectByPrimaryKey() {        //根据主键查询        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        User user = mapper.selectByPrimaryKey(1);        log.info("selectByPrimaryKey result: {}", user);    }}

Ⅱ.在Springframework中使用

1、在XML中申明需要的bean,也可以使用@Bean注解来配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">    <!-- 配置要扫描的bean路径 -->    <context:component-scan base-package="tech.wetech.mybatis.spring.mapper"/>     <!-- 配置数据源 -->    <bean id="dataSource" class="org.apache.ibatis.datasource.pooled.PooledDataSource">        <constructor-arg value="<JDBC驱动>"/>        <constructor-arg value="<JDBC URL>"/>        <constructor-arg value="<JDBC用户名>"/>        <constructor-arg value="<JDBC密码>"/>    </bean>    <!-- 配置会话工厂,这里用的ExtSqlSessionFactoryBean是继承了SqlSessionFactoryBean,增加了BaseMapper方法的注册 -->    <bean id="sqlSessionFactory" class="tech.wetech.mybatis.spring.ExtSqlSessionFactoryBean">        <property name="dataSource" ref="dataSource"/>        <property name="mapperLocations" value="classpath*:tech/wetech/mybatis/spring/mapper/*Mapper.xml"/>    </bean>    <!-- 配置Mapper扫描的类 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>        <property name="basePackage" value="tech.wetech.mybatis.spring.mapper"/>    </bean></beans>

2、开发调用

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("/beans2.xml")public class MybatisExtSpringTests {    private Logger log = LoggerFactory.getLogger(MybatisSpringTests.class);    @Autowired    private UserMapper mapper;    @Test    public void testSelectByExample() {        //支持Example查询        Example<User> example = Example.of(User.class);        example.createCriteria()                .andEqualTo(User::getId, 1)                .orEqualTo(User::getUsername, "张三")                .orNotLike(User::getAvatar, "aaa")                .orIsNull(User::getBirthday)                .orBetween(User::getRegisterTime, new Date(), new Date())                .orIn(User::getMobile, Arrays.asList(111, "aaa", 222))                .andLike(User::getAvatar, "bbb");        example.setDistinct(true);        example.setLimit(1);        example.setOffset(2);        List<User> users = mapper.selectByExample(example);        log.info("selectByExample result: {}", users);    }}

Ⅲ.在SpringBoot中使用

1、在 application.properties配置文件里面添加数据源和mapper扫描路径的配置

# 此处配置数据源spring.datasource.driver-class-name=<JDBC驱动>spring.datasource.url=<JDBC URL>spring.datasource.username=<用户名>spring.datasource.password=<密码># 此处配置mybatis扫描路径mybatis.mapper-locations=classpath:tech.wetech.mybatis.mapper/*Mapper.xml

2、在启动类上添加@MapperScan注解,指定要扫描的Mapper接口路径。

@SpringBootApplication@MapperScan(basePackages = "tech.wetech.mybatis.mapper")public class MybatisExtSpringBootApplication {  //此处省略main方法...}

3、开发调用

@RunWith(SpringRunner.class)@SpringBootTestpublic class MybatisExtSpringBootTests {    @Autowired    private UserMapper mapper;    private final Logger log = LoggerFactory.getLogger(MybatisExtSpringBootTests.class);    @Test    public void testSelectAll() {        log.info("log: {}", mapper.selectAll());    }}

Ⅳ.更多示例

public class MybatisExtTests {    //根据主键查询    @Test    public void testSelectByPrimaryKey() {        User user = mapper.selectByPrimaryKey(1);    }    //根据主键查询,使用Optional包裹    @Test    public void testSelectByPrimaryKeyWithOptional() {        User user = mapper.selectByPrimaryKeyWithOptional(1).orElseThrow(() -> new RuntimeException("未查到数据"));    }    //插入(包行null的值)    @Test    public void testInsert() {        User user = new User();        user.setUsername("222张三");        user.setPassword("aagewrwer");        user.setNickname("zhangsan");        user.setUserLevelId((byte) 22);        user.setMobile("180xxxxxxxx");        user.setRegisterTime(new Date());        user.setLastLoginTime(new Date());        user.setLastLoginIp("127.0.0.1");        user.setRegisterIp("127.0.0.1");        user.setAvatar("aaaa");        user.setWechatOpenId("222");        int rows = mapper.insert(user);    }    //插入(只包含非null的值)    @Test    public void testInsertSelective() {        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        User user = new User();        user.setUsername("张三");        user.setPassword("aagewrwer");        user.setNickname("zhangsan");        user.setUserLevelId((byte) 22);        user.setMobile("180xxxxxxxx");        user.setRegisterTime(new Date());        user.setLastLoginTime(new Date());        user.setLastLoginIp("127.0.0.1");        user.setRegisterIp("127.0.0.1");        user.setAvatar("aaaa");        user.setWechatOpenId("222");        int rows = mapper.insertSelective(user);    }    //根据主键更新(包行null的值)    @Test    public void testUpdateByPrimaryKey() {        User user = new User();        user.setId(111);        user.setUsername("张三2223333");        user.setPassword("aagewrwer");        user.setNickname("zhangsan");        user.setUserLevelId((byte) 22);        user.setMobile("180xxxxxxxx");        user.setRegisterTime(new Date());        user.setLastLoginTime(new Date());        user.setLastLoginIp("127.0.0.1");        user.setRegisterIp("127.0.0.1");        user.setAvatar("aaaa");        user.setWechatOpenId("222");        int rows = mapper.updateByPrimaryKey(user);    }    //根据主键更新(只包行非null的值)    @Test    public void testUpdateByPrimaryKeySelective() {        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        User user = new User();        user.setId(111);        user.setUsername("张三");        user.setPassword("aagewrwer");        user.setNickname("zhangsan");        user.setUserLevelId((byte) 22);        int rows = mapper.updateByPrimaryKeySelective(user);    }    //查询所有    @Test    public void testSelectAll() {        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        List<User> users = mapper.selectAll();    }    //查询多条数据    @Test    public void testSelectList() {        User user = new User();        user.setId(111);        List<User> users = mapper.selectList(user);    }    //查询一条数据    @Test    public void testSelectOne() {        User user = new User();        user.setId(111);        User user1 = mapper.selectOne(user);    }    //查询一条数据,Optional包裹    @Test    public void testSelectOneWithOptional() {        User user = new User();        user.setId(1);        User user1 = mapper.selectOneWithOptional(user).orElseGet(() -> new User());    }    //根据主键判断是否存在数据    @Test    public void testExistsByPrimaryKey() {        UserMapper mapper = sqlSession.getMapper(UserMapper.class);        Boolean exists = mapper.existsByPrimaryKey(111);    }     //根据主键删除     @Test     public void testDeleteByPrimaryKey() {        int rows = mapper.deleteByPrimaryKey(1);     }    //批量插入    @Test    public void testInsertAll() {        User user = new User();        user.setUsername("张三111333");        user.setPassword("aagewrwer");        user.setNickname("zhangsan");        user.setUserLevelId((byte) 22);        user.setMobile("180xxxxxxxx");        user.setRegisterTime(new Date());        user.setLastLoginTime(new Date());        user.setLastLoginIp("127.0.0.1");        user.setRegisterIp("127.0.0.1");        user.setAvatar("aaaa");        user.setWechatOpenId("222");                User user2 = new User();        user2.setUsername("张三222333");        user2.setPassword("aagewrwer");        user2.setNickname("zhangsan");        user2.setUserLevelId((byte) 22);        user2.setMobile("180xxxxxxxx");        user2.setRegisterTime(new Date());        user2.setLastLoginTime(new Date());        user2.setLastLoginIp("127.0.0.1");        user2.setRegisterIp("127.0.0.1");        user2.setAvatar("aaaa");        user2.setWechatOpenId("222");        int rows = mapper.insertAll(Arrays.asList(user, user2));    }    //根据条件统计    @Test    public void testCount() {        User user = new User();        user.setId(1);        int count = mapper.count(user);    }    //Criteria函数式查询    @Test    public void testCreateCriteria() {        List<User> users = mapper.createCriteria()                .andEqualTo(User::getId, 122)                .orEqualTo(User::getUsername, "张三")                .orNotLike(User::getAvatar, "aaa")                .orIsNull(User::getBirthday)                .orBetween(User::getRegisterTime, new Date(), new Date())                .orIn(User::getMobile, Arrays.asList(111, "aaa", 222))                .andLike(User::getAvatar, "aaa")                .selectList< 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
nsg-dao: java DAO框架:jdbc、hibernate 、mybatis等集成发布时间:2022-03-24
下一篇:
red: GO语言的ORM,只支持postgresql数据库发布时间:2022-03-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap