您的位置:

JavaMapper -- 基于MyBatis的ORM框架

JavaMapper是一款基于MyBatis的ORM框架,主要用于简化数据源的操作。本篇文章将从多个方面对JavaMapper进行详细阐述,为读者解析其核心特性和使用方法。

一、快速入门

JavaMapper主要提供了两个核心接口:Mapper和MapperFactory。Mapper是抽象的Mapper接口,表征着对数据源的操作。MapperFactory是Mapper的生产工厂,用于创建Mapper实例。

public interface Mapper<T> {
    T getById(Long id);
    List<T> getAll();
    void insert(T entity);
    void update(T entity);
    void deleteById(Long id);
}

public interface MapperFactory {
    <T> Mapper<T> create(Class<T> entityType);
}

定义一个简单的实体类:

public class User {
    private Long id;
    private String name;
    private Integer age;
    // getter and setter methods
}

使用JavaMapper对User进行操作:

MapperFactory mapperFactory = new DefaultMapperFactory();
Mapper<User> userMapper = mapperFactory.create(User.class);

// 插入数据
User user = new User();
user.setName("John");
user.setAge(20);
userMapper.insert(user);

// 更新数据
user.setAge(21);
userMapper.update(user);

// 查询数据
List<User> users = userMapper.getAll();

// 删除数据
userMapper.deleteById(user.getId());

二、注解方式

除了使用Mapper接口的方式,JavaMapper还支持使用注解的方式进行操作。只需要在实体类上标注@Table、@Column、@PrimaryKey注解,Mapper接口中使用@Select、@Insert、@Update、@Delete等注解即可。下面是一个示例:

@Table(name = "tb_user")
public class User {
    @PrimaryKey
    private Long id;
    @Column(name = "user_name")
    private String name;
    @Column
    private Integer age;
    // getter and setter methods
}

public interface UserMapper extends Mapper<User> {
    @Select("SELECT * FROM tb_user WHERE user_name = #{name}")
    List<User> findByName(String name);

    @Delete("DELETE FROM tb_user WHERE age > #{age}")
    void deleteByAge(Integer age);
}

// 使用
MapperFactory mapperFactory = new DefaultMapperFactory();
UserMapper userMapper = mapperFactory.create(UserMapper.class);

List<User> users = userMapper.findByName("John");
userMapper.deleteByAge(20);

三、高级查询

JavaMapper支持MyBatis的动态SQL功能,可以进行高级的数据查询操作。例如,构造一个动态查询条件:

public class UserQuery {
    private String name;
    private Integer minAge;
    private Integer maxAge;
    // getter and setter methods
}

public interface UserMapper extends Mapper<User> {
    @Select("<script>SELECT * FROM tb_user WHERE 1=1 "
            + "<if test='name != null'> AND user_name = #{name} </if>"
            + "<if test='minAge != null'> AND age >= #{minAge} </if>"
            + "<if test='maxAge != null'> AND age <= #{maxAge} </if>"
            + "</script>")
    List<User> findByQuery(UserQuery query);
}

// 使用
UserQuery query = new UserQuery();
query.setName("John");
query.setMinAge(20);
query.setMaxAge(25);
List<User> users = userMapper.findByQuery(query);

四、多数据源支持

JavaMapper支持多数据源操作,只需要在创建MapperFactory时指定数据源即可。例如,使用Druid数据源配置:

public class DataSourceUtil {
    public static DataSource getDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }
}

// 数据源1
DataSource dataSource1 = DataSourceUtil.getDataSource();
MapperFactory mapperFactory1 = new DefaultMapperFactory(dataSource1);
UserMapper userMapper1 = mapperFactory1.create(UserMapper.class);

// 数据源2
DataSource dataSource2 = DataSourceUtil.getDataSource();
MapperFactory mapperFactory2 = new DefaultMapperFactory(dataSource2);
UserMapper userMapper2 = mapperFactory2.create(UserMapper.class);
JavaMapper提供了丰富的API和快速便捷的操作方式,可以帮助开发者简化数据源的操作,提高开发效率。希望本篇文章的介绍能给有需要的开发者提供帮助。