MyBatis入门案列
开发Mybatis程序的步骤:
1、配置mybatis
conf.xml
: 配置数据库信息 和 需要加载的映射文件
2、表 - 类
映射文件xxMapper.xml
: 增删改查标签<select>
3、测试类:
session.selectOne
(“需要查询的SQL的namespace.id”, “SQL的参数值”);
运行环境:数据库 Oracle19c,Java8,数据库连接驱动 ojdbc8.jar,mybatis-3.4.6.jar 、eclipse
具体实现类:
1、建表:
CREATE TABLE person(id NUMBER, name VARCHAR2(20), age NUMBER);
INSERT into person VALUES(1,'zs', 23);
commit;
2、配置mybatis
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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!-- 配置数据库信息 -->
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:ORCL" />
<property name="username" value="scott" />
<property name="password" value="tiger" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 加载映射文件 -->
<mapper resource="org/lanqiao/entity/personMapper.xml" />
</mappers>
</configuration>
3、表 - 类
:新建与数据库对应的Bean类,同时构建映射文件
。
package org.lanqiao.entity;
public class Person {
private int id;
private String name;
private int age;
public Person() {
}
public Person(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
// getter and setter
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 "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
映射文件personMapper.xml:增删改查标签<select>
<?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.lanqiao.entity.personMapper">
<select id="queryPersonById" resultType="org.lanqiao.entity.Person" parameterType="int">
select * from person where id = #{id}
</select>
</mapper>
4、编写测试类TestMybatis.java
:
session.selectOne(“需要查询的SQL的namespace.id”, “SQL的参数值”);
package org.lanqiao.entity;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMybatis {
public static void main(String[] args) throws IOException {
// 加载myBatis配置文件(为了访问数据库)
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
// SqlSessionFactory connection
SqlSession session = sessionFactory.openSession();
String statement = "org.lanqiao.entity.personMapper.queryPersonById";
Person person = session.selectOne(statement,1);
System.out.println(person);
session.close();
}
}
5、执行结果
6、小结
- parameterType=“int”> 的
int
对应 select 语句的 #{id
}
select * from person where id = #{id}
- namespace:映射文件的路径