MyBatis入门案例

MyBatis入门案例

MyBatis是服务器与数据库交互的轻量级框架,需要导入相关jar包

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>

跟着官方文档一点一点来实现与数据库的交互

第一步:通过xml配置文件创建SqlSessionFactory对象

在这里插入图片描述

配置数据库连接的信息

    public static SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory;
    }
<?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/mybatis?serverTimezone=UTC&amp;characterEncoding=utf-8&amp;useSSL=false&amp;rewriteBatchedStatements=true&amp;allowPublicKeyRetrieval=true"/>
                <property name="username" value="root"/>
                <property name="password" value="Hkx123"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="employeeMapper.xml"/>
    </mappers>
</configuration>

第二步:从sqlSessionFactory对象中获取SqlSession对象

在这里插入图片描述

    @Test
    public void test01() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisTest.getSqlSessionFactory();
        //获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //调用sqlSession.seleteOne()查询方法,第一个参数为映射的xml配置文件的名称空间+id,第二个参数为sql语句的占位符
        Employee employee = sqlSession.selectOne("com.yellowstar.mybatis.EmployeeMapper.selectEmployee", 1);
        System.out.println(employee);
        //需使用try-catch-finally关闭资源
        sqlSession.close();
    }

创建employeeMapper.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.yellowstar.mybatis.EmployeeMapper">
<!--
    namespace:名称空间
    id:唯一表示
    resultType:返回值类型
    #{id}:从传递过来的参数中取id值
-->
    <select id="selectEmployee" resultType="com.yellowstar.mybatis.bean.Employee">
        select * from employee where id = #{id}
    </select>
</mapper>

接口式编程

我们通过接口,自动匹配xml文件中的名称空间与id值

public interface EmployeeDao {
    Employee getEmployeeById(Integer id);
}

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.yellowstar.mybatis.dao.EmployeeDao">
    <select id="getEmployeeById" resultType="com.yellowstar.mybatis.bean.Employee">
        select * from employee where id = #{id}
    </select>
</mapper>

如何调用?

    @Test
    public void test02() throws IOException {
        SqlSessionFactory sqlSessionFactory = MyBatisTest.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //获取接口对象,参数为接口的类型
        EmployeeDao mapper = sqlSession.getMapper(EmployeeDao.class);
        Employee employee = mapper.getEmployeeById(2);
        System.out.println(employee);
        sqlSession.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值