mybatis详解

jdbc.properties配置文件

jdbc.uname=scott
jdbc.password=root
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl

mybatis.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>

<!-- 通用配置文件 
1.先读取properties元素内的属性property
2.再读取resource配置文件的内容
3.读取作为方法参数传递给属性,并覆盖已读取的同名属性
-->
<properties resource="jdbc.properties">
	<property name="jdbc.uname" value="user1"/>
	<property name="jdbc.password" value="root"/>
</properties>

<!-- 别名设置 -->
<typeAliases>
	<package name="org.lq.entity"/>
</typeAliases>


  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">  
        <property name="driver" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.uname}"/>
        <property name="password" value="${jdbc.password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
  //配置文件映射使用方式
<!-- 	<mapper resource="org/lq/mapper/Dept.xml"/> -->
<!-- 	<mapper resource="org/lq/mapper/Emp.xml"/> -->
//注解映射使用方式
<package name="org.lq.dao"/>
  </mappers>
</configuration>

Emp表的mapper.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="org.lq.dao.EmpDao">

	<insert id="save" parameterType="Emp">
		insert into emp
		(empno, ename, job, mgr, hiredate, sal, comm, deptno)
		values
		(emp_seq.nextval, #{ename}, #{job,jdbcType=VARCHAR},
		#{mgr,jdbcType=VARCHAR,jdbcType=VARCHAR},
		#{hiredate,jdbcType=VARCHAR}, #{sal,jdbcType=VARCHAR},
		#{comm,jdbcType=VARCHAR}, #{deptno,jdbcType=VARCHAR})
	</insert>

	<select id="findEmp" resultType="Emp" parameterType="Emp">

		select * from emp where 1=1
		<if test="ename!=null and  ename!='' ">
			and ename like '%${ename}%'
		</if>
		<if test="sal!=0 and sal !=null">
			and sal > #{sal}
		</if>

	</select>

	<select id="find" resultType="Emp" parameterType="Emp">
		select * from emp
		<where>
			<if test="ename!=null and  ename!='' ">
				and ename like '%${ename}%'
			</if>
			<if test="sal!=0 and sal !=null">
				and sal > #{sal}
			</if>

		</where>

	</select>

	<!-- 类似switch -->
	<select id="find2" resultType="Emp" parameterType="Emp">
		select * from emp
		<choose>
			<when test="ename!=null and  ename!='' ">
				and ename like '%${ename}%'
			</when>
		</choose>
	</select>

<!-- 	<select id="find" resultType="Emp" parameterType="Emp"> -->
<!-- 		select * from emp -->
<!-- 		<set> -->
<!-- 			<trim prefix=""></trim> -->
<!-- 		</set> -->

<!-- 	</select> -->


	<update id="update" parameterType="Emp">
		update emp
		<set>
			<if test="ename!=null and  ename!='' ">
				ename = #{ename},
			</if>
			<if test="job!=null and  job!='' ">
			job = #{job},
				</if>
			<if test="mgr!=null and  mgr!=0 ">
			mgr = #{mgr},
				</if>
			<if test="hiredate!=null and  hiredate!='' ">
			hiredate = #{hiredate},
				</if>
			<if test="sal!=null and  sal!=0 ">
			sal = #{sal},
				</if>
			<if test="comm!=null and  comm!=0 ">
			comm =#{comm},
				</if>
			<if test="deptno!=null and  deptno!=0 ">
			deptno = #{deptno}
				</if>
		</set>
		where empno = #{empno}
	</update>

</mapper>

Dept的mapper文件,和emp关联

<?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.lq.dao.DeptMapper">

	<resultMap type="org.lq.entity.Dept" id="baseDept">

		<result property="deptno" column="deptno" />
		<result property="dname" column="dname" />
		<result property="location" column="loc" />

	</resultMap>
	

	<resultMap type="org.lq.entity.Dept" id="deptInemps"
		extends="baseDept" >
		<collection property="emps" column="deptno"
			select="org.lq.dao.EmpMapper.getEmpsByDeptNo">
		</collection>
	</resultMap>
	
	<resultMap type="org.lq.entity.Dept" id="deptIneEmps2" extends="baseDept">
		<collection property="emps" resultMap="org.lq.dao.EmpMapper.base">
		</collection>
	</resultMap>

	<select id="getById" parameterType="int" resultMap="baseDept" >
		select * from Dept where deptno = #{no}
	</select>

	<select id="getByIdInEmps" parameterType="int"
		resultMap="deptInemps">
		select * from Dept where deptno = #{no}
	</select>
	
	<select id="getByIdInEmps2" parameterType="int"
	resultMap="deptIneEmps2">
		select* from emp e,dept d  where e.deptno=d.deptno and d.deptno=#{no}
	</select>
</mapper>

Emp的mapper文件,和dept管理

<?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.lq.dao.EmpMapper">

	<resultMap type="Emp" id="base">
		<id property="empno" column="empno" />
		<result property="ename" column="ename" />
		<result property="job" column="job" />
		<result property="mgr" column="mgr" />
		<result property="hiredate" column="hiredate" />
		<result property="sal" column="sal" />
		<result property="comm" column="comm" />
		<result property="deptno" column="deptno" />

	</resultMap>

	<resultMap type="Emp" id="baseEmp"
		extends="base">

		<!-- 配置对象属性 -->
		<association property="dept" column="deptno"
			select="org.lq.dao.DeptMapper.getById" />

	</resultMap>

	<resultMap type="org.lq.entity.Emp" id="baseEmp2" extends="base">
		<association property="dept"
			resultMap="org.lq.dao.DeptMapper.baseDept"></association>
	</resultMap>



	<select id="getById" parameterType="int"
		resultType="org.lq.entity.Emp">

		select * from Emp where empno = #{no}
	</select>

	<select id="getByIdInDept" parameterType="int"
		resultMap="baseEmp">

		select * from Emp where empno = #{no}
	</select>

	<select id="getEmpsByDeptNo" parameterType="int"
		resultMap="base">

		select * from Emp where deptno = #{no}
	</select>

	<select id="getByIdInDept2" parameterType="int"
		resultMap="baseEmp2">

		select * from(
		select * from emp e left join dept d on e.deptno = d.deptno
		) where empno= #{no}
	</select>

</mapper>

Dept和Emp注解方式

package org.lq.dao;

import java.util.List;

import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.lq.entity.Dept;

public interface DeptDaoAnno {

	@Select("select * from dept d,emp e where d.deptno = e.deptno")
	@Results(
			id = "base",
			value = {
					@Result(column = "deptno",property = "deptno"),
					@Result(column = "dname",property = "name"),
					@Result(column = "loc",property = "location"),
					@Result(property = "emps",column = "deptno",many = @Many(select = "org.lq.dao.EmpDapAnno.byid")),
				}
			)
	List<Dept> find();
}

package org.lq.dao;

import java.util.List;

import org.apache.ibatis.annotations.Select;
import org.lq.entity.Emp;

public interface EmpDapAnno {
 
	@Select("select * from emp where deptno = #{deptno}")
	Emp byid(int id);
}

测试类

package org.lq.test;

import java.io.IOException;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.lq.dao.DeptDao;
import org.lq.dao.DeptDaoAnno;
import org.lq.entity.Dept;

public class Test {

	public static void main(String[] args) {
		//配置文件方式
//		try {
//			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.xml"));
//			
//			SqlSession session = sessionFactory.openSession(true);
//			
//			DeptDao dept = session.getMapper(DeptDao.class);
			Dept d = new Dept();
			d.setName("你好");
			
			dept.save(d);
			
//			
			Empdao emp= session.getMapper(Empdao.class);
//			List<Dept> find = dept.find();
//			for (Dept emp1 : find) {
//				System.out.println(emp1);
//			}
//			
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		
		//注解方式
		try {
			SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis.xml"));
			SqlSession session = factory.openSession(true);
			
			DeptDaoAnno d =  session.getMapper(DeptDaoAnno.class);
			List<Dept> find = d.find();
			
			for (Dept dept : find) {
				System.out.println(dept);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值