Mybatis快速上手操作(巨简单)
Mybatis是一个ORM(Object Relational Mapper)框架,说人话ORM就是数据库中的表与Java中的实体类(POJO)类一一对应。
比如你有一个Student类,内含三个私有对象:
package com.bean.pojo;
public class Student {
private int id;
private String name;
private int age;
public Student() {}
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
Student类对应的数据库就是
CREATE DATABASE mybatis;
USE mybatis;
CREATE TABLE Student ( id INT ( 2 ) PRIMARY KEY auto_increment, NAME VARCHAR ( 20 ) NOT NULL, age INT ( 3 ) NOT NULL );
INSERT INTO student
VALUES
( DEFAULT, '甄姬', 17 );
INSERT INTO student
VALUES
( DEFAULT, '孙尚香', 17 );
INSERT INTO student
VALUES
( DEFAULT, '星彩', 17 );
INSERT INTO student
VALUES
( DEFAULT, '貂蝉', 17 );
UPDATE student
SET age = 18
WHERE
id = 2;
UPDATE student
SET age = 19
WHERE
id = 3;
UPDATE student
SET age = 20
WHERE
id = 4;
然后他们之间通过什么建立连接呢?
通过一个mapper.xml进行映射建立连接:
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bean.pojo.studentMapper">
<select id="queryById" parameterType="Integer" resultType="Student">
select * from student where id = #{id}
</select>
</mapper>
通过namespace.id名称,可以唯一地确定当前的sql语句
id:区分不同sql语句
parameterType:参数类型
resultType:查询结果的返回值
然后需要有一个总配置文件conf.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>
<package name="com.bean.pojo"/>
</typeAliases>
<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/mybatis"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/bean/pojo/studentMapper.xml"/>
</mappers>
</configuration>
这个配置环境可以参考:请点我,通过官方文档可以找到XML的头信息,至于需要修改的只有dataSource中的四个连接数据库的配置,这个和JDBC的操作差不多,我就不赘述了,typeAliases(别名)是什么意思呢?通过别名配置,我们可以:不用在mapper文件中写全类名了!~
mappers中的配置信息关联着之前写到的studentMapper.xml文件,这样,全局配置文件就和POJO类中的mapper文件关联在了一起!
然后我们通过一个测试类进行测试:
package com.bean.dao;
import com.bean.pojo.Student;
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 java.io.IOException;
import java.io.Reader;
public class Test {
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
Student student = session.selectOne("com.bean.pojo.studentMapper.queryById",1);
System.out.println(student);
}
}
首先需要将配置文件的资源变成Reader文件,之后生成一个SqlSessionFactory,也就是sql会话工厂来加载这个reader,通过工厂生产SqlSession,通过session就可以执行增删改查的操作了,我在这里举例是selectOne,也就是只查询一个数据,通过mapper.xml中的namespace和id唯一确定要执行哪一条sql语句。