开源项目 `mapper` 使用教程

开源项目 mapper 使用教程

mapperA simple and easy go tools for auto mapper map to struct, struct to map, struct to struct, slice to slice, map to slice, map to json.项目地址:https://gitcode.com/gh_mirrors/mapper/mapper

项目介绍

mapper 是一个开源的 MyBatis 通用 Mapper 项目,旨在简化 MyBatis 的使用。通过提供一系列通用的 Mapper 接口和方法,mapper 使得开发者能够更快速地进行数据库操作,减少样板代码的编写。

项目快速启动

环境准备

  1. Java 开发环境:确保你已经安装了 JDK 8 或更高版本。
  2. Maven:确保你已经安装了 Maven 构建工具。
  3. 数据库:确保你有一个可用的数据库,并已配置好相应的数据库连接。

快速启动步骤

  1. 克隆项目

    git clone https://github.com/devfeel/mapper.git
    cd mapper
    
  2. 添加依赖: 在你的 pom.xml 文件中添加以下依赖:

    <dependency>
        <groupId>com.github.abel533</groupId>
        <artifactId>mapper</artifactId>
        <version>最新版本</version>
    </dependency>
    
  3. 配置 MyBatis: 在 mybatis-config.xml 文件中进行基本配置:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC"/>
                <dataSource type="POOLED">
                    <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                    <property name="url" value="jdbc:mysql://localhost:3306/yourdb"/>
                    <property name="username" value="yourusername"/>
                    <property name="password" value="yourpassword"/>
                </dataSource>
            </environment>
        </environments>
        <mappers>
            <mapper resource="com/yourpackage/mapper/YourMapper.xml"/>
        </mappers>
    </configuration>
    
  4. 创建实体类和 Mapper 接口: 创建一个简单的实体类 User.java

    public class User {
        private Integer id;
        private String name;
        // 省略 getter 和 setter 方法
    }
    

    创建一个 Mapper 接口 UserMapper.java

    import tk.mybatis.mapper.common.Mapper;
    
    public interface UserMapper extends Mapper<User> {
    }
    
  5. 编写 Mapper XML 文件: 创建 UserMapper.xml 文件:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.yourpackage.mapper.UserMapper">
        <select id="selectAll" resultType="com.yourpackage.entity.User">
            SELECT * FROM user
        </select>
    </mapper>
    
  6. 测试代码: 编写一个简单的测试类 UserMapperTest.java

    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.InputStream;
    import java.util.List;
    
    public class UserMapperTest {
        public static void main(String[] args) throws Exception {
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            try {
                UserMapper userMapper = session.getMapper
    

mapperA simple and easy go tools for auto mapper map to struct, struct to map, struct to struct, slice to slice, map to slice, map to json.项目地址:https://gitcode.com/gh_mirrors/mapper/mapper

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍薇樱Quintessa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值