myBatis实例

mybatis 是一款优秀的支持自定义sql 查询、存储过程和高级映射的持久层框架,myBatis可以
使用xml或者注解进行配置和映射,myBatis通过代码和参数映射到配置的sql形成的最终执行的sql
语句
Mybatis并没有将java对象与数据库关联起来,而是将java方法与sql语句关联
myBatis支持数据缓存
create database mybatis Default character set utf8;
show databases; 查看数据库是否创建成功
use mybatis;
create table country(id int not null auto_increment,countryname varchar(255) null,countrycode varchar(255) null ,primary key(id));
desc country;查看表的列

insert into country (countryname,countrycode) VALUES ('爱尔兰','al');

错误: Error building SqlSession.

         The error may exist in /mybatis-config.xml   看看resource的mapper文件路径

首先创建一个maven工程,接下来给出pom文件以及基础代码。

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>

</build>

=======================pom文件========================================

在resources 文件夹下创建 mybatis-config.xml,countryMapper.xml以及log4j的配置文件

public class MyCountryTest {
private static SqlSessionFactory sql; 
    @BeforeClass
    public static void init() throws IOException{
    //读取配置文件
    Reader read =Resources.getResourceAsReader("mybatis-config.xml");
    //构建sqlsession工厂
    sql=new SqlSessionFactoryBuilder().build(read);
    read.close();
    }
    @Test
    public void testSelectAll(){
    SqlSession openSession = sql.openSession();
    List<Country> selectList = openSession.selectList("selectAll");
    System.out.println(selectList);
    openSession.close();
    }

}

======================测试类======================================

<?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>
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>
<typeAliases>
<package name="com.mybatis.demo" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 当前xml命名空间-->
<mappers>
   <mapper resource="CountryMapper.xml"/>
</mappers>

</configuration>

=======================config文件================================

<?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="CountryMapper.xml">
    <!-- 当前查询的唯一id  resultType返回值类型 -->
       <select id="selectAll" resultType="Country">
          select id,countryname,countrycode from country
       </select>
    </mapper>

   =====================mapper==========================================

log4j.rootLogger = ERROR,stdout//想要什么属性自己配置,哈哈

==================log4j.properties==========================================

package com.mybatis.demo;


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;
}
@Override
public String toString() {
return "Country [id=" + id + ", countryname=" + countryname + ", countrycode=" + countrycode + "]";

}

猜你喜欢

转载自blog.csdn.net/yl_hahha/article/details/80208161