mybatis入门1

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:mybatis入门记录。


提示:以下是本篇文章正文内容,下面案例可供参考

一、mybatis是什么?

示例:它是一款半自动的ORM持久层框架,具有较高的SQL灵活性,支持高级映射(一对一,一对多),动态SQL,延迟加载和缓存等特性,但它的数据库无关性较低。

二、入门案例

整体的文件布局截图如下:
在这里插入图片描述
本案例是创建的maven项目为基础。
pom.xml文件导包内容如下:

	<!--连接mysql数据库jar包-->
	<dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.10</version>
    </dependency>
    <!--mybatis相关jar包-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.6</version>
    </dependency>
    <!--减少java代码的插件工具-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>
    <!--单元测试jar包-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>compile</scope>
    </dependency>

1.编写全局配置文件:sqlMapConfig.xml

db.properties文件内容如下:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
username=root
password=root

sqlMapConfig.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>
    <properties resource="db.properties" />
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="device.xml"/>
    </mappers>
</configuration>

2.映射文件:XXXMap.xml

映射文件device.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="dao.DeviceMap">
    <select id="findById" parameterType="java.lang.Integer" resultType="model.Device">
        SELECT
        id as id,
        device_name As deviceName,
        line_status As lineStatus,
        self_status AS selfStatus,
        alarm_status AS alarmStatus
         FROM device where id=#{id};
    </select>
</mapper>

其中namespace指向对应的dao接口;resultType指向接收的结果类;id="findById"表示dao接口中的抽象方法名为findById

3.编写dao代码:XXXDao接口以及XXXImpl实现类

dao接口内容如下:

public interface DeviceMap {
    Device findById(Integer id);
}

dao接口的实现类内容如下:

public class DeviceImpl implements  DeviceMap{
    private SqlSessionFactory sqlSessionFactory;
    //注入sqlSessionFactory
    public DeviceImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory = sqlSessionFactory;
    }
    @Override
    public Device findById(Integer id) {
        //sqlSessionFactory工厂类创建session会话
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //sqlSession接口,开发人员使用它对数据库进行增删改查操作
        Device device = sqlSession.selectOne("dao.DeviceMap.findById", id);
        return device;
    }
}

4.编写POJO类

接收对象类内容如下:

@Getter
@Setter
@ToString
@NoArgsConstructor//无参构造
@AllArgsConstructor//全参构造
public class Device {
    private Integer id;
    private String deviceName;
    private Integer lineStatus;
    private Integer selfStatus;
    private Integer alarmStatus;
}

5.单元测试类

单元测试类内容如下:

public class Test1 {
    private SqlSessionFactory sqlSessionFactory;
    @Before
    public void init() throws IOException {
        String configPath = "sqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(configPath);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testFindUserId(){
        DeviceImpl deviceMap = new DeviceImpl(sqlSessionFactory);
        Device device = deviceMap.findById(1);
        System.out.println(device);
    }
}

总结

实现了对mybatis的简单运用。
参考文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值