mybatis示例,两种方式查询数据库(注解与xml映射)

mybatis示例,两种方式查询数据库(注解与xml映射)_第1张图片

1。搭建目录层级如上图所示:
2。配置pom文件
    这里只需要用到mysql连接,lombok日志插件以及junit单元测试包,下面贴出pom文件的全部内容
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.hangroupId>
    <artifactId>mybatisMeTestartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>mybatisMeTestname>
    <url>http://maven.apache.orgurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    properties>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.6version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.18version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.6.1version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>1.1.2version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.1.2version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.11version>
        dependency>

    dependencies>
project>

3.
示例代码


----------


package com.han.mybatisMeTest;

import java.io.IOException;
import java.io.InputStream;

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 org.junit.Test;

import com.han.mapper.CupMapper;
import com.han.po.Cup;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MybatisMeTest {
    @Test
    public void testQueryWithAnotation() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        CupMapper mapper = session.getMapper(CupMapper.class);
        Cup cup = mapper.selectCupById(1);
        log.info("cup:{}", cup);
    }

    @Test
    public void testQueryWithXml() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        Cup cup = session.selectOne("com.han.mapper.getCupByid", 3);
        log.info("cup:{}", cup);
    }

}


----------
package com.han.mapper;

import org.apache.ibatis.annotations.Select;

import com.han.po.Cup;

public interface  CupMapper {
     @Select("SELECT * FROM cup WHERE id = #{id}")
     Cup selectCupById(int id);
}


----------
package com.han.po;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
public class Cup {
    @Getter
    @Setter
    public int id;
    @Getter
    @Setter
    public int value;
}


----------


<mapper namespace="com.han.mapper">
    <select id="getCupByid" parameterType="int" resultType="cup">
        select * from cup where id = #{id}
    select>
mapper>


----------


<configuration>
<typeAliases>
<typeAlias type="com.han.po.Cup" alias="cup"/>

typeAliases>
    <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/hn?useSSL=false&characterEncoding=utf8&serverTimezone=UTC" />
                <property name="username" value="root" />
                <property name="password" value="root" />
            dataSource>
        environment>
    environments>
    <mappers>
        <mapper class="com.han.mapper.CupMapper" />
        <mapper resource="CupMapper.xml"/>
    mappers>

configuration>

4。大概说明一下,这里只写了两种查询,官网有示例,改下可以直接运行

http://www.mybatis.org/mybatis-3/zh/configuration.html

你可能感兴趣的:(mybatis)