1.创建一个maven项目,在pom文件中添加mybatis和mysql-connector-java模块
2.创建domain包,包内实体类Student,定义name,sex,age属性,重写构造方法
package org.example.domain;
public class Student {
private String name;
private String sex;
private int age;
public Student(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
}
3.在resource目录下创建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>
<!-- 设置实体类的别名-->
<typeAliases>
<!-- 对于每个实体类单独配置别名(不区分大小写)-->
<!-- <typeAlias alias="student" type="org.example.domain.Student"/>-->
<!--对于整个包进行配置,所有的实体类别名为类名(不区分大小写-->
<package name="org.example.domain"/>
</typeAliases>
<!-- 设置环境配置,default为不指定时的默认配置-->
<environments default="development">
<environment id="development">
<!-- 事务管理JDBC-->
<transactionManager type="JDBC"/>
<!-- 连接池配置-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 指定mybatis加载mapper映射文件的地址-->
<mappers>
<!-- 对于整个包进行配置,加载包内所有的mapper文件-->
<package name="org.example.dao"/>
<!-- 对于单个xml文件的相对路径进行配置-->
<!-- <mapper resource="org/example/dao/StudentDao.xml"/>-->
</mappers>
</configuration>
4.创建dao包,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">
<!--命名空间绑定StudentDao.java-->
<mapper namespace="org.example.dao.StudentDao">
<!-- select操作语句-->
<select id="selectStudents" resultType="Student">
select * from student order by id
</select>
<!-- insert操作语句,#{}占位-->
<insert id="insertStudent">
insert into student values (#{name},#{sex},#{age})
</insert>
</mapper>
5.dao包下创建StudentDao.java接口
package org.example.dao;
import org.example.domain.Student;
import java.util.List;
public interface StudentDao {
// 定义方法,方法名与mapper映射文件中的id一一对应
Student queryStudentByName(); // 通过姓名搜索学生
List<Student> queryStudents(); // 获取全部学生信息
int insertStudent(); // 插入学生
}
6.配置静态资源文件
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
7.调用
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.dao.StudentDao;
import org.example.domain.Student;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class App
{
public static void main( String[] args )
{
// 设置配置文件名
String config = "mybatis.xml";
try {
// 读取配置文件为输入流
InputStream in = Resources.getResourceAsStream(config);
// 创建factory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 获取sqlSession
SqlSession sqlSession = factory.openSession();
// 创建实体类Student
Student student = new Student("liu","nan",17);
// 第一种操作方法
// String insertSqlId = "org.example.dao.StudentDao.insertStudent";
// int res = sqlSession.insert(insertSqlId,student);
// System.out.println(res);
// 第二种操作方式
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
int res = studentDao.insertStudent(student);
System.out.println(res);
// 调用queryStudents方法
List<Student> results = studentDao.queryStudents();
results.forEach(stu -> System.out.println(stu));
// 使用commit提交数据
sqlSession.commit();
// 关闭sqlSession
sqlSession.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
结果目录为