前言:不结合spring,只有 mybatis+maven。数据库使用 oracle。不尝试永远不知道会发生什么事,其中遇到两个小问题,也记录下来了。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9429807.html

一、创建用户表,用于这次的demo

-- Create table
create table t_user
(
id number not null,
name varchar2(1000),
age number
)
;
-- Add comments to the table
comment on table t_user
is '用户表';
-- Add comments to the columns
comment on column t_user.id
is '主键';
comment on column t_user.name
is '用户名';
comment on column t_user.age
is '年龄';
-- Create/Recreate primary, unique and foreign key constraints
alter table t_user
add constraint t_user_id primary key (ID);

  创建完表结构,就可以新增数据了

insert into T_USER (ID, NAME, AGE)
values (1, 'yule', 18);
insert into T_USER (ID, NAME, AGE)
values (2, 'xiaohua', 24);
insert into T_USER (ID, NAME, AGE)
values (3, '小明', 30);
insert into T_USER (ID, NAME, AGE)
values (4, '小小', 24);

二、pom.xml 中引入 MyBatis

    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>

三、创建实体

package com.yule.user.entity;

/**
* 用户实体
* Created by yule on 2018/8/6 21:51.
*/
public class User {
private String id;
private String name;
private String age; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
}
}

四、创建映射器和 sql

  dao 接口

package com.yule.user.dao;

import com.yule.user.entity.User;

import java.util.List;

/**
* 用户 Dao 层
* Created by yule on 2018/8/6 22:06.
*/
public interface UserDao {
List<User> queryUserList();
}

  sql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.yule.user.dao.UserDao"> <select id="queryUserList" resultType="com.yule.user.entity.User" >
select t.id, t.name, t.age from t.user t
</select> </mapper>

五、配置 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> <!--配置环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="username" value="testdev"/>
<property name="password" value="test1234"/>
</dataSource>
</environment>
</environments> <!-- 引入映射器 -->
<mappers>
<!--<mapper class="com.yule.user.dao.UserDao"/>-->
<mapper resource="com/yule/user/dao/UserDao.xml"/>
</mappers> </configuration>

六、写个单测来看看

package com.yule.user.dao;

import com.yule.user.entity.User;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List; import static org.junit.Assert.*; public class UserDaoTest { private static SqlSessionFactory sqlSessionFactory; @Test
public void test() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession sqlSession = sqlSessionFactory.openSession(); UserDao userDao = sqlSession.getMapper(UserDao.class);
List<User> userList = userDao.queryUserList(); for (User user : userList) {
System.out.println(user.getName());
} sqlSession.close();
} private static SqlSessionFactory getSqlSessionFactory() {
//单例
if (sqlSessionFactory == null) {
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("configuration.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getCause());
}
}
return sqlSessionFactory;
} }

七、运行结果

八、错误1:pom 中需要加如下代码

  运行单测报错

D:\Java\jdk1.8.0_144\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar=51056:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\plugins\junit\lib\junit-rt.jar;D:\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Java\jdk1.8.0_144\jre\lib\rt.jar;F:\IDEAworkspace\sdemo\target\test-classes;F:\IDEAworkspace\sdemo\target\classes;F:\mavenRepository\junit\junit\4.12\junit-4.12.jar;F:\mavenRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\mavenRepository\org\springframework\spring-test\4.3.14.RELEASE\spring-test-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-context\4.3.14.RELEASE\spring-context-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-aop\4.3.14.RELEASE\spring-aop-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-expression\4.3.14.RELEASE\spring-expression-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-beans\4.3.14.RELEASE\spring-beans-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-core\4.3.14.RELEASE\spring-core-4.3.14.RELEASE.jar;F:\mavenRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;F:\mavenRepository\org\springframework\spring-web\4.3.14.RELEASE\spring-web-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-webmvc\4.3.14.RELEASE\spring-webmvc-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-jdbc\4.3.14.RELEASE\spring-jdbc-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-tx\4.3.14.RELEASE\spring-tx-4.3.14.RELEASE.jar;F:\mavenRepository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;F:\mavenRepository\javax\servlet\jsp\jsp-api\2.2\jsp-api-2.2.jar;F:\mavenRepository\javax\servlet\jstl\1.2\jstl-1.2.jar;F:\mavenRepository\commons-fileupload\commons-fileupload\1.2.1\commons-fileupload-1.2.1.jar;F:\mavenRepository\commons-io\commons-io\2.4\commons-io-2.4.jar;F:\mavenRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;F:\mavenRepository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;F:\mavenRepository\log4j\log4j\1.2.17\log4j-1.2.17.jar;F:\mavenRepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\mavenRepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\mavenRepository\org\mybatis\mybatis\3.4.5\mybatis-3.4.5.jar;F:\mavenRepository\com\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.yule.user.dao.UserDaoTest,test
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/F:/mavenRepository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/F:/mavenRepository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/yule/user/dao/UserDao.xml
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/yule/user/dao/UserDao.xml at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at com.yule.user.dao.UserDaoTest.getSqlSessionFactory(UserDaoTest.java:42)
at com.yule.user.dao.UserDaoTest.test(UserDaoTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/yule/user/dao/UserDao.xml
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:99)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 25 more
Caused by: java.io.IOException: Could not find resource com/yule/user/dao/UserDao.xml
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:114)
at org.apache.ibatis.io.Resources.getResourceAsStream(Resources.java:100)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:371)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:119)
... 27 more Process finished with exit code -1

  解决方案:为了编译之后,能读取到 xml 文件,所以在 pom.xml 中 <build></build>加如下代码。

<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>

九、错误2:添加 pom 的 oracle 依赖

  运行单测报错:报找不到驱动;java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

  解决方案:https://www.cnblogs.com/yuxiaole/p/9479536.html

转载请注明出处: https://www.cnblogs.com/yuxiaole/p/9429807.html

  

深入理解MyBatis的原理(一): 独立的入门demo的更多相关文章

  1. 深入理解MyBatis的原理:整个体系

    前言:工作中虽然用到了 MyBatis,可完全不知道为什么,再不学习就晚了,这里将记录我的学习笔记,整个 MyBatis 的体系. 一.简介 1.传统的JDBC JDBC 是一种典型的桥接模式. 使用 ...

  2. 深入理解MyBatis的原理(四):映射器的用法

    前言:继续深入学习 mybatis 的用法及原理,还是先会用再学习原理. 映射器的主要元素有:select.insert.update.delete.parameterMap(即将被删除,不建议使用) ...

  3. 深入理解MyBatis的原理(三):配置文件用法(续)

    前言:前文讲解了 MyBatis 的配置文件一部分用法,本文将继续讲解 MyBatis 的配置文件的用法. 目录 1.typeHandler 类型处理器 2.ObjectFactory 3.插件 4. ...

  4. 深入理解MyBatis的原理(三):配置文件(上)

    前言:前文提到一个入门的demo,从这里开始,会了解深入 MyBatis 的配置,本文讲解 MyBatis 的配置文件的用法. 目录 1.properties 元素 2.设置(settings) 3. ...

  5. 《深入理解mybatis原理》 Mybatis初始化机制具体解释

    对于不论什么框架而言.在使用前都要进行一系列的初始化,MyBatis也不例外. 本章将通过下面几点具体介绍MyBatis的初始化过程. 1.MyBatis的初始化做了什么 2. MyBatis基于XM ...

  6. 深入理解Mybatis技术与原理

    目录 第1章 Mybatis简介 1.1 传统的JDBC编程 1.2 ORM模型 1.4 MyBatis 1.5 什么时候用MyBatis 第2章 MyBatis入门 2.2 MyBatis构成 2. ...

  7. 深入理解mybatis原理, Mybatis初始化SqlSessionFactory机制详解(转)

    文章转自http://blog.csdn.net/l454822901/article/details/51829785 对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章 ...

  8. 《深入理解mybatis原理2》 Mybatis初始化机制详解

    <深入理解mybatis原理> Mybatis初始化机制详解 对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外.本章将通过以下几点详细介绍MyBatis的初始化过程 ...

  9. 《深入理解mybatis原理》 MyBatis事务管理机制

    MyBatis作为Java语言的数据库框架,对数据库的事务管理是其很重要的一个方面.本文将讲述MyBatis的事务管理的实现机制. 首先介绍MyBatis的事务Transaction的接口设计以及其不 ...

随机推荐

  1. poj1017----模拟

    题目大意: 现有1*1,2*2,3*3,4*4,5*5,6*6规格的产品若干个(高度都为h),问最少需要多少个 6*6*h的箱子把这些产品都装完 输入:每组测试数据共6个整数,分别代表1*1,...6 ...

  2. Windows下TensorFlow安装指南(图文版)

    随着深度学习概念火起来,TensorFlow也跟着成为业界流行的深度学习框架.它采用CPU+GPU的并行计算模式,使得神经网络可以有效的并行计算,从以前的三层网络到现在的深层网络,深度学习+tenso ...

  3. 一次对webshell的后门的查看

    本文作者i春秋作家——非主流 昨天晚上突发奇想的想去看看github上面tennc的webshell收集项目中的shell有没有漏洞,比如未授权啊啥的,结果找半天都没找到...但是机缘巧合下,居然给我 ...

  4. [JavaScript] 将字符串数组转化为整型数组

    var dataStr="1,2,3,4,5";//原始字符串 var dataStrArr=dataStr.split(",");//分割成字符串数组 var ...

  5. layer_mobile的简单使用

    layer mobile弹层组件是为移动设备(手机.平板等webkit内核浏览器/webview)量身定做的弹层UI. 由于是采用原生 JavaScript编写,所以并不依赖任何第三方库. layer ...

  6. laydata 点击日期闪现

    因项目需求需要多个日期,然后点击日期就会出现闪现的情况,导致选择不了日期 html代码 <table class="form"> <tr> <th c ...

  7. Python基础部分的疑惑解析——pycharm(4)

    PyCharm部分设置: 1.安装后破解    2.创建的项目project实际上就是文件夹,可以在右键--show in explorer显示文件夹 3.右键-new--directory是建文件夹 ...

  8. linux初始

    运维 IT运维,指IT公司中,运行和维护服务器的工作 核心工作: ​ 数据不能丢失 ​ 7*24小时运行 ​ 提高用户访问效率 一句换 管服务器的 服务器 要管服务器那就得先了解服务器 服务器也称为伺 ...

  9. centos 6.8 解决ibus输入法不正常显示的问题

    今天发现 ibus输入法打字时不正常显示,如下图

  10. strcpy,memcpy,memset函数实现

    strcpy 实现,只能拷贝字符串 char* strcpy(char* des,const char* source) { char* r=des; assert((des != NULL) &am ...