1.新建Java Web或Java工程
2.导入相应的jar包(oracle驱动jar及Mybatis所需jar)
3.创建类,测试类,映射文件,配置文件
4.总体结构如下图
其中类为:
package me.gacl.domain;
public class User {
//实体类的属性和表的字段名称一一对应
private int id;
private String name;
private int 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 "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
测试类为
package me.gacl.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import me.gacl.domain.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;
public class Test {
public static void main(String[] args) throws IOException {
//mybatis的配置文件
//String resource = "conf.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
//InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
Reader reader = Resources.getResourceAsReader("conf.xml");
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
//Reader reader = Resources.getResourceAsReader(resource);
//构建sqlSession的工厂
//SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
String statement = "me.gacl.mapping.userMapper.getUser";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
User wser1 = new User();
wser1.setId(2);
wser1.setName("孤傲苍狼");
User user = (User)session.selectOne(statement, wser1);
// List<User> list = (List)session.selectList(statement);
// for(User user:list){
// System.out.println(user);
// }
// int user = (Integer)session.selectOne(statement);
System.out.println(user);
}
}
映射文件为
<?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="me.gacl.mapping.userMapper">
<select id="getUser" parameterType="me.gacl.domain.User"
resultType="me.gacl.domain.User">
select * from users where id=#{id} and name=#{name}
</select>
<select id="getUserList"
resultType="me.gacl.domain.User">
select * from users
</select>
</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="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.0.212:1521:orcl" />
<property name="username" value="newjw_zgzf141024" />
<property name="password" value="newjw_zgzf141024" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="me/gacl/mapping/userMapper.xml"/>
</mappers>
</configuration>
--创建序列
create sequence users_sequence
increment by 1
start with 1
maxvalue 999999999;
increment by 1
start with 1
maxvalue 999999999;
--建表
CREATE TABLE users(id number PRIMARY KEY , NAME VARCHAR2(20), age number);
INSERT INTO users(id,NAME, age) VALUES(users_sequence.nextval,'孤傲苍狼', 27);
INSERT INTO users(id,NAME, age) VALUES(users_sequence.nextval,'白虎神皇', 27);
INSERT INTO users(id,NAME, age) VALUES(users_sequence.nextval,'孤傲苍狼', 27);
INSERT INTO users(id,NAME, age) VALUES(users_sequence.nextval,'白虎神皇', 27);
如图所示结果即为成功