出自《MyBatis从入门到精通》刘增辉,精简
1.设置源码编码方式为 UTF -8 2.设置编译源代码的 JDK 版本 3.添加mybatis依赖 4.还需要添加会用到的 Log4j JUnit ySql 驱动的依赖。
4.0.0 org.example One_MyBatisPrimary 1.0-SNAPSHOT UTF-8 maven-compiler-plugin 1.8 1.8 org.mybatis mybatis 3.3.0 junit junit 4.12 test org.mybatis mybatis 3.3.0 mysql mysql-connector-java 5.1.38 org.slf4j slf4j-api 1.7.12 org.slf4j slf4j-log4j12 1.7.12 log4j log4j 1.2.17


插入数据
INSERT INTO country(countryname,countrycode) VALUES ('中国','CN'),('美国','us'),('俄罗斯','RU'),('英国','GB'),('法国', 'FR');
package tk.mybatis.simple.model;
public class Country {private Long id;private String countryname;private String countrycode;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getCountryname() {return countryname;}public void setCountryname(String countryname) {this.countryname = countryname;}public String getCountrycode() {return countrycode;}public void setCountrycode(String countrycode) {this.countrycode = countrycode;}
}
我的存放路径:

#全局面配置
log4j.rootLogger=ERROR, stdout
#MyBatis 日志配置
log4j.logger.tk.mybatis.simple.mapper=TRACE
#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversonPattern = %5p [%t] - %m%n#用过 Log4j 日志组件的人可能都会知道,配直中的 log4j.logger.tk.mybatis.simple.mapper 对应的是 tk mybatis simple .mapper包,
# 但是在这个例子中,Java目录下并没有这个包名,只在资源目录下有 mapper目录#MyBatis 的日志实现中,所谓的包名实际上是 XML 配直中的 namespace 属性值的一部分#由于namespace性值必须和接口全限定类名相同 ,
# 因此才会真正对应到Java 中的包 当使用纯注解方式时,使用的就是纯粹的包名#MyBatis 日志的 最低级 TRACE ,
# 在这个日志级别下,MyBatis会输出执行 SQL 过程中的详细信息,这个级别特别适合在开发时使用
package tk.mybatis.simple.mapper;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.BeforeClass;
import org.junit.Test;
import tk.mybatis.simple.model.Country;import java.io.IOException;
import java.io.Reader;
import java.util.List;public class CountryMapperTest {private static SqlSessionFactory sqlSessionFactory;@BeforeClasspublic static void init() {try {
// 通过 Resources 工具类将 ti -config.xm 配置文件读入 ReaderReader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 再通过 SqlSessionFactoryBuilder 建造类使用 Reader 创建 SqlSessionFactory工厂对象。
// 在创建 SqlSessionFactory 对象的过程中
// 首先解 mybatis-config.xml 配置文件,读取配置文件中的 mappers 配置后会读取全部的 Mapper xml 进行具体方法的解析,
// 在这些解析完成后, SqlSessionFactory 就包含了所有的属性配置和执行 SQL 的信息。sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 使用时通过 SqlSessionFactory 工厂对象获取 splSessionreader.close();} catch (IOException ignore) {ignore.printStackTrace();}}@Testpublic void testSelectAll() {
// 通过 SqlSession selectList 方法查 Coun Mapper nl id selectAll的方法,执行 查询SqlSession sqlSession = sqlSessionFactory.openSession();
// mybaatis底层使用 JDBC 执行 SQL ,获得查询结果集 ResultSet 后,根据 resultType的配置将结果映射为 Country 类型的集合 返回查询结果。try {List countryList = sqlSession.selectList("selectAll");
// 这样就得到了最后的查询结果 countryList ,简单将结果输出到控制台。printCountryList(countryList);} finally {//最后 定不要忘记关闭 SqlSession ,否 会因为连接没有关闭导致数据库连接数过多,造成系统崩旗。sqlSession.close();}}private void printCountryList(List countryList) {for (Country country : countryList) {System.out.printf("%-4d%4s%4s\n", country.getId(), country.getCountryname(), country.getCountrycode());}}
}

解决:
(1)这个我勾选了但还是有没有成功IDEA提示java: 程序包org.apache.ibatis.session不存在_小白学CS的博客-CSDN博客_程序包org.apache.ibatis.session不存在
(2)成功解决error:java :程序包org.apache.ibatis.io不存在org.apache.ibatis.session不存在 解决方法_北哑的博客-CSDN博客_程序包org.apache.ibatis.session不存在


解决:复制的路径是\,改成/
