mybaits 学习1-helloword

学习连接

https://github.com/mybatis/mybatis-3
http://www.mybatis.org/mybatis-3/zh/getting-started.html
https://www.bilibili.com/video/av34875242?p=3

数据库mysql

image.png

字段应该与你的java对象属性命名保持一致,否则xml中sql语句使用别名保持一致

全局配置文件

image.png

mybatis-mysql.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>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=CTT&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true"/>
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </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="EmployeeMapper.xml"/>
    </mappers>
</configuration>

Mapper SQL语句映射文件

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.mybatis.bean.EmployeeMapper">
<!--
namespace: 命名空间
id: 位移标识
resultType: 返回数据类型
-->
    <select id="selectEmp" resultType="com.mybatis.bean.Employee">
        select * from tbl_employee where id = #{id}
    </select>
</mapper>

通过SqlSessionFactory 获取session

package com.mybatis;

import com.mybatis.bean.Employee;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author : kean
 * @version V1.0
 * @Project: springboot2
 * @Package com.mybatis
 * @Description: TODO
 * @date Date : 2019-05-03 22:59
 */

public class MybatisTest {

    private static final Logger logger = LoggerFactory.getLogger(MybatisTest.class);

    /**
     * xml SqlSessionFactory 创建
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        String resource = "mybatis-mysql.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            Employee employee = (Employee) sqlSession.selectOne("com.mybatis.bean.EmployeeMapper.selectEmp", 101);
            logger.info("{} {} {}", employee.getId(), employee.getName(), employee.getSex());
        } finally {
            sqlSession.close();
            logger.info("查询完成!");
        }
    }
}

测试结果

D:\apps\jdk\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\apps\IntelliJ IDEA 2019.1\lib\idea_rt.jar=11113:D:\apps\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\apps\IntelliJ IDEA 2019.1\lib\idea_rt.jar;D:\apps\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;D:\apps\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;D:\apps\jdk\jre\lib\charsets.jar;D:\apps\jdk\jre\lib\deploy.jar;D:\apps\jdk\jre\lib\ext\access-bridge-64.jar;D:\apps\jdk\jre\lib\ext\cldrdata.jar;D:\apps\jdk\jre\lib\ext\dnsns.jar;D:\apps\jdk\jre\lib\ext\jaccess.jar;D:\apps\jdk\jre\lib\ext\jfxrt.jar;D:\apps\jdk\jre\lib\ext\localedata.jar;D:\apps\jdk\jre\lib\ext\nashorn.jar;D:\apps\jdk\jre\lib\ext\sunec.jar;D:\apps\jdk\jre\lib\ext\sunjce_provider.jar;D:\apps\jdk\jre\lib\ext\sunmscapi.jar;D:\apps\jdk\jre\lib\ext\sunpkcs11.jar;D:\apps\jdk\jre\lib\ext\zipfs.jar;D:\apps\jdk\jre\lib\javaws.jar;D:\apps\jdk\jre\lib\jce.jar;D:\apps\jdk\jre\lib\jfr.jar;D:\apps\jdk\jre\lib\jfxswt.jar;D:\apps\jdk\jre\lib\jsse.jar;D:\apps\jdk\jre\lib\management-agent.jar;D:\apps\jdk\jre\lib\plugin.jar;D:\apps\jdk\jre\lib\resources.jar;D:\apps\jdk\jre\lib\rt.jar;D:\java_workspace\springboot2\target\test-classes;D:\java_workspace\springboot2\target\classes;D:\repository\org\springframework\boot\spring-boot-starter\2.1.0.RELEASE\spring-boot-starter-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot\2.1.0.RELEASE\spring-boot-2.1.0.RELEASE.jar;D:\repository\org\springframework\spring-context\5.1.2.RELEASE\spring-context-5.1.2.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.0.RELEASE\spring-boot-autoconfigure-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-logging\2.1.0.RELEASE\spring-boot-starter-logging-2.1.0.RELEASE.jar;D:\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\repository\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\repository\org\springframework\spring-core\5.1.2.RELEASE\spring-core-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-jcl\5.1.2.RELEASE\spring-jcl-5.1.2.RELEASE.jar;D:\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\repository\org\springframework\boot\spring-boot-starter-test\2.1.0.RELEASE\spring-boot-starter-test-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-test\2.1.0.RELEASE\spring-boot-test-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.1.0.RELEASE\spring-boot-test-autoconfigure-2.1.0.RELEASE.jar;D:\repository\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\repository\org\assertj\assertj-core\3.11.1\assertj-core-3.11.1.jar;D:\repository\org\mockito\mockito-core\2.23.0\mockito-core-2.23.0.jar;D:\repository\net\bytebuddy\byte-buddy\1.9.3\byte-buddy-1.9.3.jar;D:\repository\net\bytebuddy\byte-buddy-agent\1.9.3\byte-buddy-agent-1.9.3.jar;D:\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;D:\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\repository\org\springframework\spring-test\5.1.2.RELEASE\spring-test-5.1.2.RELEASE.jar;D:\repository\org\xmlunit\xmlunit-core\2.6.2\xmlunit-core-2.6.2.jar;D:\repository\org\projectlombok\lombok\1.18.4\lombok-1.18.4.jar;D:\repository\org\springframework\boot\spring-boot-starter-web\2.1.0.RELEASE\spring-boot-starter-web-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-json\2.1.0.RELEASE\spring-boot-starter-json-2.1.0.RELEASE.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;D:\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;D:\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.0.RELEASE\spring-boot-starter-tomcat-2.1.0.RELEASE.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.12\tomcat-embed-core-9.0.12.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.12\tomcat-embed-el-9.0.12.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.12\tomcat-embed-websocket-9.0.12.jar;D:\repository\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;D:\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\repository\org\springframework\spring-web\5.1.2.RELEASE\spring-web-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-beans\5.1.2.RELEASE\spring-beans-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-webmvc\5.1.2.RELEASE\spring-webmvc-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-expression\5.1.2.RELEASE\spring-expression-5.1.2.RELEASE.jar;D:\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar;D:\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.0.RELEASE\spring-boot-starter-jdbc-2.1.0.RELEASE.jar;D:\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;D:\repository\org\springframework\spring-jdbc\5.1.2.RELEASE\spring-jdbc-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-tx\5.1.2.RELEASE\spring-tx-5.1.2.RELEASE.jar;D:\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;D:\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;D:\repository\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\repository\org\apache\commons\commons-dbcp2\2.5.0\commons-dbcp2-2.5.0.jar;D:\repository\org\apache\commons\commons-pool2\2.6.0\commons-pool2-2.6.0.jar;D:\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\repository\org\springframework\boot\spring-boot-configuration-processor\2.1.0.RELEASE\spring-boot-configuration-processor-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-aop\2.1.0.RELEASE\spring-boot-starter-aop-2.1.0.RELEASE.jar;D:\repository\org\springframework\spring-aop\5.1.2.RELEASE\spring-aop-5.1.2.RELEASE.jar;D:\repository\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;D:\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\repository\junit\junit\4.12\junit-4.12.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.mybatis.MybatisTest,test
23:41:45.006 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
23:41:45.183 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
23:41:45.184 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
23:41:45.184 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
23:41:45.184 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
23:41:45.311 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
23:41:50.931 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 2030036700.
23:41:50.932 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@78ffe6dc]
23:41:50.933 [main] DEBUG com.mybatis.bean.EmployeeMapper.selectEmp - ==>  Preparing: select * from tbl_employee where id = ? 
23:41:50.962 [main] DEBUG com.mybatis.bean.EmployeeMapper.selectEmp - ==> Parameters: 101(Integer)
23:41:50.990 [main] DEBUG com.mybatis.bean.EmployeeMapper.selectEmp - <==      Total: 1
23:41:50.992 [main] INFO com.mybatis.MybatisTest - 101 jack man
23:41:50.994 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@78ffe6dc]
23:41:50.995 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@78ffe6dc]
23:41:50.995 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 2030036700 to pool.
23:41:50.995 [main] INFO com.mybatis.MybatisTest - 查询完成!

Process finished with exit code 0

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,456评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,370评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,337评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,583评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,596评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,572评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,936评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,595评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,850评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,601评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,685评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,371评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,951评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,934评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,167评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,636评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,411评论 2 342

推荐阅读更多精彩内容

  • 1-------- 走进前端 2-------- jQuery 3-------- CSS 4-------- A...
    依依玖玥阅读 2,315评论 0 34
  • 今天早上吃了饭我就去上班了,让大宝在家做两套测试题。中午回家,已经写完了,很不错!下午一点的口才试听课,大宝...
    轩轩皓皓阅读 156评论 0 0
  • 因为刚才有些事,所以坐公车到街上去买东西。上了车之后一直低头玩手机,没有注意身后的人,后来我嫌挤就换了个方向站...
    林瑾曦阅读 489评论 0 0
  • 趁着这个假日,远离城市的喧嚣,在这无限接近自然的山寨村落...
    KKling阅读 224评论 0 1
  • 原谅我以前的无知,今日听朋友介绍,才发现如此这般的平台,畅所欲言哉。待日后慢慢叙来,与众好友分享生活点滴。
    AAnastasia阅读 111评论 0 1