一、创建数据库
数据库结构如下
student表结构如下
插入数据如下
二、利用maven创建一个普通的Java项目
项目结构如下
如果创建的项目下不带有resources目录,需要在main下手动创建resources目录并给它标记为Resources根
配置它的pom文件
首先需要导入mysql、junit、mybatis的相关依赖,并导入资源插件resources
<?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>org.example</groupId>
<artifactId>ch03-proxy-dao</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ch03-proxy-dao</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--mybatis依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
</dependencies>
<build>
<!-- 资源插件-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
配置完pom文件后重新加载改maven模块
三、创建mybatis的主配置文件
在resources目录下创建一个名为mybatis的xml文件
配置它的数据库驱动和mapper文件的所在位置
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="数据库驱动"/>
<property name="url" value="数据库url"/>
<property name="username" value="数据库名称"/>
<property name="password" value="数据库密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml(mapper文件的路径)"/>
</mappers>
</configuration>
四、创建学生类Student
在demain文件夹下创建学生类并生成他的getter和setter方法,并且重写他的toString方法便于测试,代码如下:
package org.example.domain;
public class Student {
//属性名和列名保持一致
private Integer id;
private String name;
private String email;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "学生实体信息{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", age=" + age +
'}';
}
}
五、创建并配置Mapper文件
首先在dao文件夹下创建一个StudentDao接口,具体代码如下
package org.example.dao;
import org.example.domain.Student;
public interface StudentDao {
//查询一个学生
Student selectStudentById(Integer id);
}
然后创建一个名为StudentDao的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="dao接口的全限定名称">
<!-- 需要执行的sql语句,例如select,insert,update,delete,-->
</mapper>
配置这个mapper文件,具体代码如下
<?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="org.example.dao.StudentDao">
<select id="selectStudentById" resultType="org.example.domain.Student">
SELECT id,name,email,age FROM student WHERE id=1001
</select>
</mapper>
并且还要在主配置文件mybatis.xml下加上这个mapper文件的地址
即:
<mappers>
<mapper resource="org\example\dao\StudentDao.xml"/>
</mappers>
六、测试
在测试文件夹下创建MyTest类用于测试
代码和操作步骤如下:
package org.example;
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.example.domain.Student;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class MyTest {
@Test
//测试mybatis执行sql语句
public void testselectStudentbyid() throws IOException {
//定义mybatis主配置文件的位置
String config="mybatis.xml";
//读取主配置文件,使用mybatis框架中的Resources类
InputStream inputStream = Resources.getResourceAsStream(config);
//创建SqlSessionFactory,使用SqlSessionFactoryBuilder类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
//获取SqlSession对象
SqlSession session=factory.openSession();
//指定要执行sql语句的id
String sqlId="org.example.dao.StudentDao"+"."+"selectStudentById";
//通过SqlSession的方法执行sql语句
Student stu=session.selectOne(sqlId);
System.out.println(stu);
//释放资源
session.close();
}
}
运行测试类,结果如下
至此,我们完成了mybatis的入门程序的实现