Mybatis基础
- 添加mybatis的依赖,目前版本是3.4.6,除此之外还有junit和mysql的驱动依赖分别引入进来
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qfedu</groupId>
<artifactId>Days21Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
</project>
2.在resources目录下新增一个mybatis的配置文件,mybatis.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节点
里面有配置信息 分别是环境和映射
其中环境里有datasource,里面有我们熟悉的连接数据库的四个字符串
-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hello"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qfedu/pojo/UserMapper.xml"/>
</mappers>
</configuration>
3.在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;
}
}
4.在com/qfedu/pojo的目录下创建UserMapper.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.qfedu.pojo.UserMapper">
<select id="selectUser" resultType="com.qfedu.pojo.User">
select * from user
</select>
</mapper>
5.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<User> 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();
}
}
}
6.执行结果
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'}