玩玩mybatis(一)--mybatis

是什么

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

怎么用

集合mysql做个小demo

  • POM
	<dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
  • 配置文件 src/main/resources/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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.213.5:3306/boot_test"/>
                <property name="username" value="root"/>
                <property name="password" value="Mysql123456!"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/haha/BlogMapper.xml"/>
    </mappers>
</configuration>
  • mapper.xml文件 src/main/resources/com/haha/BlogMapper.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.haha.BlogMapper">
    <select id="selectBlog" resultType="com.haha.Blog">
        select * from Blog where id = #{id}
    </select>
</mapper>
  • mapper
package com.haha;

public interface BlogMapper {

    Blog selectBlog(String id);
}

@Data
public class Blog implements Serializable {

    private String id;
    private String name;
    private String content;
}

  • 测试
public class Test1 {

    private SqlSessionFactory sqlSessionFactory;

    @Before
    public void init() {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }

    @Test
    public void test1() {
        SqlSession session = sqlSessionFactory.openSession();
        BlogMapper mapper = session.getMapper(BlogMapper.class);
        Blog blog = mapper.selectBlog("10");
        System.out.println(blog);
    }
}
  • 结果
Wed May 05 15:50:45 CST 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
15:50:46.326 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1766145591.
15:50:46.326 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@69453e37]
15:50:46.330 [main] DEBUG com.haha.BlogMapper.selectBlog - ==>  Preparing: select * from Blog where id = ?
15:50:46.355 [main] DEBUG com.haha.BlogMapper.selectBlog - ==> Parameters: 10(String)
15:50:46.372 [main] DEBUG com.haha.BlogMapper.selectBlog - <==      Total: 1
Blog(id=10, name=xx, content=测试一个玩玩)

总结

mybatis 入门使用,后续精进!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你是真的皮005

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

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

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

打赏作者

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

抵扣说明:

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

余额充值