1.添加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.zy</groupId>
<artifactId>NZMybatisTest</artifactId>
<version>1.0.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/*"/>
<property name="username" value="root"/>
<property name="password" value="***"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zy/pojo/UserMapper.xml"/>
</mappers>
</configuration>
3.在com.zy.pojo的包下创建User.java文件
package com.zy.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/zy/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.zy.pojo.UserMapper">
<select id="selectUser" resultType="com.zy.pojo.User">
select * from user
</select>
</mapper>
5.TestUser.java
package com.zy.test;
import com.zy.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.zy.pojo.UserMapper.selectUser");
for (User u : users) {
System.out.println(u);
}
if(session != null){
session.close();
session = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}