转载请注明出处 csdn wankaiming
1.在src下配置mybatisconfig.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>
<!-- 设置属性文件 ,从中读取数据库访问所需的基本信息-->
<properties resource="database.properties"></properties>
<!-- 设置开发的环境 -->
<environments default="development">
<environment id="development">
<!-- 设置事务管理器 org.apache.ibatis.transaction.jdbc
JdbcTransactionFactory-->
<transactionManager type="jdbc"></transactionManager>
<!-- 设置数据源org.apache.ibatis.datasource.pooled
还有jndi要企业级应用服务器 还有unpooled -->
<dataSource type="pooled">
<!-- ognl -->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
<property name="defaultAutoCommit" value="true"/>
<property name="poolMaximumActiveConnections" value="80"/>
<property name="poolMaximumIdleConnections" value="20"/>
</dataSource>
</environment>
</environments>
<!-- 设置映射文件 -->
<mappers>
<!-- resource不同于type,class写包名的 ,要写完整 -->
<mapper resource="testmybatis/po/Emp.xml" />
<mapper resource="testmybatis/po/Dept.xml" />
</mappers>
</configuration>
2.database.properties属性文件配置 给出两个数据库的配置
#oracle
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
username=exam
password=exam
#mysql
#driver=com.mysql.jdbc.Driver
#url=jdbc:mysql://127.0.0.1:3306/test
#username=root
#password=root
3.po文件的写法 演示常用功能
Dept.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 name="testmybatis.po.Dept">
<select id="getempbydept" parameterType="int" resultMap="deptempresult">
<!-- 不能换行 -->
Select e.empno,e.ename,e.job,e.hiredate,e.sal,e.deptno,d.dname,d.loc From emp e Left Outer Join dept d On e.deptno=d.deptno Where d.deptno=#{deptno}
</select>
<resultMap type="testmybatis.po.Dept" id="deptempresult">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
<!-- 设置集合,ofType集合当中元素的类型 -->
<!-- 多个员工 用collection -->
<collection property="emp" ofType="testmybatis.po.Emp">
<id column="empno" property="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="hiredate" column="hiredate" />
<result property="sal" column="sal" />
<result property="deptno" column="deptno" />
</collection>
</resultMap>
</mapper>
Emp.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">
<!-- namespace必须写 DTD中虽然是可选的 namespace 可以用po名字来命名-->
<mapper namespace="testmybatis.po.Emp">
<!-- 配置下result可以返回数据,写上包名 -->
<!-- 结果集 主要使用在查询语句上 -->
<resultMap type="testmybatis.po.Emp" id="empresult">
<result property="empno" column="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="hiredate" column="hiredate" />
<result property="sal" column="sal" />
<result property="deptno" column="deptno" />
</resultMap>
<!-- id名字可以自己取 查询操作 -->
<!-- 返回多行数据时,需要使用resultMap -->
<select id="getall" resultMap="empresult">
Select empno,ename,job,hiredate,sal,deptno From emp
</select>
<!-- 传递一个参数时,给出参数类型 ,返回一行数据时,使用resultType -->
<select id="getone" parameterType="int" resultType="testmybatis.po.Emp">
Select empno,ename,job,hiredate,sal,deptno From emp Where empno=#{empno}
</select>
<!-- 查询的时候多个参数的时候 如此配置 -->
<!-- 参数集 使用在查询语句的条件上 -->
<parameterMap type="testmybatis.po.Emp" id="empparameter">
<parameter property="job"/>
<parameter property="sal"/>
</parameterMap>
<!-- 查询操作 根据多个条件来查询时 需要配置perameterMap -->
<select id="getempbyjobandsal" parameterMap="empparameter" resultMap="empresult">
Select empno,ename,job,hiredate,sal,deptno From emp Where job=#{job} And Sal=#{sal}
</select>
<select id="getempbysal" parameterType="double" resultMap="empresult">
Select empno,ename,job,hiredate,sal,deptno From emp Where <![CDATA[sal<#{sql}]]>
</select>
<!-- 另外一种多个参数的方式 -->
<select id="getempbyenameandjob" parameterType="testmybatis.po.Emp" resultMap="empresult">
Select empno,ename,job,hiredate,sal,deptno From emp Where job=#{job} And ename=#{ename}
</select>
<!-- 添加操作 -->
<insert id="addemp" parameterType="testmybatis.po.Emp">
Insert Into emp(empno,ename,job,hiredate,sal,deptno) Values(#{empno},#{ename},#{job},#{hiredate},#{sal},#{deptno})
</insert>
<!-- 修改操作 -->
<update id="updateemp" parameterType="testmybatis.po.Emp">
Update emp Set sal=sal+#{sal} Where ename=#{ename}
</update>
<!-- 删除操作 -->
<delete id="deleteemp" parameterType="int">
delete from emp where empno=#{empno}
</delete>
<!-- 多表连接操作 -->
<select id="getempdept" resultMap="empdeptresult">
Select empno,ename,job,hiredate,sal,deptno,dname,loc From emp Natural Join dept
</select>
<resultMap type="testmybatis.po.Emp" id="empdeptresult">
<!-- id标示主键 -->
<id column="empno" property="empno" />
<result property="ename" column="ename" />
<result property="job" column="job" />
<result property="hiredate" column="hiredate" />
<result property="sal" column="sal" />
<result property="deptno" column="deptno" />
<!-- 必须配置dept的po文件 且在po.Emp当中关联po.Dept这个类 -->
<association property="dept" column="deptno" javaType="testmybatis.po.Dept" resultMap="deptresult"></association>
</resultMap>
<resultMap type="testmybatis.po.Dept" id="deptresult">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
</resultMap>
</mapper>