美文网首页Mybatis 深入源码
前话:简单搭建一个环境

前话:简单搭建一个环境

作者: 零度微笑_019c | 来源:发表于2019-06-13 19:40 被阅读0次

    参考文档:mybatis官方文档

    1.添加依赖

    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    

    2.添加mybatis配置文件

    <?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="jdbc.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="com/xm/source/mybatis/dao/mapper/BlogMapper.xml"/>
    </mappers>
    </configuration>
    

    3.添加jdbc配置

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://192.168.80.4:3306/MyTestDb?useUnicode=true&characterEncoding=UTF-8
    username=root
    password=xm123456
    

    4.测试demo

    • Blog.java
    @Data
    public class Blog {
        private Long id;
        private String name;
    }
    
    • BlogMapper.java
    public interface BlogMapper {
        /**
         * 根据id查询
         * @param id
         * @return
         */
        Blog queryById(Long id);
    }
    
    • 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.xm.source.mybatis.dao.BlogMapper">
        <select id="queryById" resultType="com.xm.source.mybatis.pojo.Blog">
        select * from blog where id = #{id}
      </select>
    </mapper>
    
    • MybatisTest.java
    public class MybatisTest {
    
        @Test
        public void test() {
            String resource = "mybatis-config.xml";
            InputStream inputStream = null;
            SqlSession session = null;
            try {
                inputStream = Resources.getResourceAsStream(resource);
                SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
                session = sqlSessionFactory.openSession();
                BlogMapper mapper = session.getMapper(BlogMapper.class);
                Blog blog = mapper.queryById(1L);
                System.out.println(blog);
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                session.close();
            }
        }
    
    }
    

    相关文章

      网友评论

        本文标题:前话:简单搭建一个环境

        本文链接:https://www.haomeiwen.com/subject/oqhefctx.html