UsersMapper.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="com.nantian.mybatis.model.UsersMapper"> <resultMap type="Users" id="user"> <id property="id" column="id" javaType="int" jdbcType="INTEGER"/> <result property="name" column="name" javaType="string" jdbcType="VARCHAR"/> <result property="sex" column="sex" javaType="string" jdbcType="VARCHAR"/> </resultMap> <!-- 查询单一对象所有属性 --> <select id="selectUsers" parameterType="int" resultType="Users"> select id,name,age,sex from users where id = #{id} </select> <!-- 选择查询单一对象属性 --> <select id="selectUser" parameterType="int" resultMap="user"> select id,name,sex from users where id = #{id} </select> <!-- 主键自增插入mysql --> <insert id="insertUsersMysql" parameterType="Users"> insert into users values(null,#{name},#{age},#{sex}) </insert> <!-- 主键自增插入oracle --> <insert id="insertUsers" parameterType="Users"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select seq_users.nextval from dual </selectKey> insert into users values(#{id},#{name},#{age},#{sex}) </insert> </mapper> UsersMapper package com.nantian.mybatis.model; public interface UsersMapper { public Users selectUsers(int id); public void insertUsers(Users users); public Users selectUser(int id); } Users package com.nantian.mybatis.model; public class Users { private int id; private String name; private int age; private String sex; 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } } Test package com.nantian.mybatis.test; import junit.framework.TestCase; import org.apache.ibatis.session.SqlSession; import com.nantian.mybatis.data.DBUtil; import com.nantian.mybatis.model.Users; import com.nantian.mybatis.model.UsersMapper; public class Test extends TestCase { public void selectUser() { SqlSession sqlSession = DBUtil.getInstance().getSqlSessionFactory() .openSession(); try { UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); Users user = usersMapper.selectUser(3); System.out.println(user.getId()); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getSex()); } finally { sqlSession.close(); } } public void selectUsers() { SqlSession sqlSession = DBUtil.getInstance().getSqlSessionFactory() .openSession(); try { UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); Users users = usersMapper.selectUsers(1); System.out.println(users.getId()); System.out.println(users.getName()); System.out.println(users.getAge()); System.out.println(users.getSex()); } finally { sqlSession.close(); } } public void insertUsers() { SqlSession sqlSession = DBUtil.getInstance().getSqlSessionFactory() .openSession(); try { UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); Users users = new Users(); users.setName("王五"); users.setAge(32); users.setSex("神"); usersMapper.insertUsers(users); sqlSession.commit(); } finally { sqlSession.close(); } } } configuration.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="Users" type="com.nantian.mybatis.model.Users"/> </typeAliases> <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:@127.0.0.1:1521:orcl"/> <property name="username" value="scott"/> <property name="password" value="tiger"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/nantian/mybatis/model/UsersMapper.xml"/> </mappers> </configuration> DBUtil package com.nantian.mybatis.data; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class DBUtil { private static final DBUtil dbUtil = new DBUtil(); private SqlSessionFactory sqlSessionFactory = null; private DBUtil() { try { String resource = "com/nantian/mybatis/data/configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public static DBUtil getInstance() { return dbUtil; } public SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } }