Mybatis基础
添加mybatis的依赖,目前版本是3.4.6,除此之外还有junit和mysql的驱动依赖分别引入进来
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
com.qfedu
Days21Mybatis
1.0-SNAPSHOT
mysql
mysql-connector-java
5.1.44
junit
junit
4.12
org.mybatis
mybatis
3.4.6
在resources目录下新增一个mybatis的配置文件,mybatis.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
在com.qfedu.pojo的包下创建User.java文件
package com.qfedu.pojo;
public class User {
private int uid;
private String username;
private String password;
private int age;
private String addr;
@Override
public String toString() {
return "User{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", addr='" + addr + '\'' +
'}';
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
在com/qfedu/pojo的目录下创建UserMapper.xml文件
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
TestUser.java
package com.qfedu.test;
import com.qfedu.pojo.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.sql.Connection;
import java.util.List;
public class TestUser {
@Test
public void testGetAllUsers(){
/**
* 1. 使用mybatis的配置文件以及SqlSessionFactoryBuilder建造者模式创建SqlSessionFactory对象
* 2. 使用SqlSessionFactory对象的openSession()方法来得到一个SqlSession对象,用该对象即可完成对象的所有的crud操作
* 3. 使用SqlSession对象来完成crud操作
* 4. 关闭资源
*
*/
try {
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml"));
SqlSession session = sf.openSession();
/**
* session的crud方法("namespace.id");整个项目中的namespace.id必须唯一
*/
List users = session.selectList("com.qfedu.pojo.UserMapper.selectUser");
for (User u : users) {
System.out.println(u);
}
if(session != null){
session.close();
session = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
执行结果
User{uid=1, username='sunwukong', password='111111', age=500, addr='huaguoshan'}
User{uid=2, username='zhubajie', password='222222', age=2000, addr='gaolaozhuang'}
User{uid=3, username='shaheshang', password='333333', age=500, addr='liushahe'}
User{uid=4, username='tangtang', password='444444', age=20, addr='datangwangchao'}
User{uid=5, username='baikongma', password='555555', age=100, addr='longgong'}
User{uid=6, username='1111', password='', age=20, addr=''}
User{uid=7, username='baigujing', password='888888', age=800, addr='baigudong'}
User{uid=8, username='nverguowang', password='888888', age=18, addr='nuerguo'}
User{uid=9, username='heixiongjing', password='888888', age=1800, addr='heifengdong'}
User{uid=10, username='tieshan', password='888888', age=900, addr='huoyanshan'}
User{uid=11, username='lishushu', password='888888', age=18, addr='beijing'}
User{uid=12, username='change', password='888888', age=1000, addr='guanghangong'}