配置:
<?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>
<!-- 读取国际化文件 -->
<!-- <properties resource="db.properties"></properties> -->
<!-- 为类型设置别名 -->
<typeAliases>
<typeAlias type="com.oak.pojo.Admin" alias="admin"/>
<typeAlias type="com.oak.pojo.User" alias="user"/>
</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:@localhost:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/oak/dao/EmpDao.xml"/>
</mappers>
</configuration>
映射文件:
<?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.xml 中的namespace 设置为 mapper.java 的全限定名。 -->
<!-- 该映射文件取代了dao的实现类
namespace是指要实现的接口的全限定名 -->
<mapper namespace="com.oak.dao.EmpDao">
<!-- 所有的查询使用select 插入insert 更新 update 删除 delete
id在当前映射文件中不允许重复,要和接口的方法名保持一致
resultType表示方法的返回类型,如果返回类型中有泛型,则只描述泛型即可-->
<select id="empList" resultType="com.oak.entity.Emp">
select * from emp
</select>
<insert id="AddEmp" parameterType="com.oak.entity.Emp">
<!-- 使用序列生成主键值
keyProperty="aid"表示主键对应的属性,
order="BEFORE"表示生成id序列值时机:before表示在insert前,after表示在insert后
resultType="int"表示序列值的类型
-->
<selectKey keyProperty="empno" order="BEFORE" resultType="int">
select seq_customer_id.nextval from dual
</selectKey>
<!-- #{参数}代替了占位符? -->
insert into emp values(#{empno},#{ename},#{job},#{mgr},#{hiredate},#{sal},#{comm},#{deptno})
</insert>
<select id="ToupdateEmp" resultType="com.oak.entity.Emp" parameterType="int">
select * from emp where empno = #{id}
</select>
<update id="UpdateEmp" parameterType="com.oak.entity.Emp">
update emp set job = #{job}<!-- ,ename = #{ename}, mgr = #{mgr},hiredate= #{hiredate},sal = #{sal}, comm = #{comm}, deptno = #{deptno} --> where empno = #{empno}
</update>
<delete id="DeliteEmp" parameterType="int">
delete from emp where empno = #{id}
</delete>
</mapper>