package cn.happy.dao;
import java.util.List;
import cn.happy.entity.Dept;
public interface IDeptDao {
//查看全部---------getAllDept要和小配置里面的id一样
public List<Dept> getAllDept();
}
<?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="cn.happy.dao.IDeptDao"> <select id="getAllDept" resultType="cn.happy.entity.Dept"> select * from Dept </select> </mapper>
// 01查看全部信息getMapper()接口类的方法名要和小配置的id一样
@Test
public void testSelectAll() {
SqlSession session = factory.openSession();
//用的是弱类型========实体类.小配置里面的Id名称============字符串
/*List<Dept> list = session.selectList("cn.happy.dao.IDeptDao.getAllDept");
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}*/
// 用getMapper方法HIbernate帮我们在内存中代理出一个接口的实现类======相当于强类型
//mapper是一个实现类对象
IDeptDao mapper = session.getMapper(IDeptDao.class);
List<Dept> list = mapper.getAllDept();
for (Dept dept : list) {
System.out.println(dept.getDeptName());
}
<?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> <!-- Alias别名 小配置里面的type的属性值改成别名--> <typeAliases> <typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="sa" /> <property name="password" value="1" /> </dataSource> </environment> </environments> <!--映射文件:描述某个实体和数据库表的对应关系 --> <mappers> <mapper resource="cn/resultMap/enetity/Emp.xml" /> </mappers> </configuration>
package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//检索所有员工,以及隶属部门
public List<Emp> getAllEmps();
}
<?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="cn.resultMap.dao.IEmpDao"> <resultMap type="cn.resultMap.enetity.Emp" id="empMap"> <id property="empId" column="EMPID"/> <result property="empName" column="EMPNAME"/> <result property="empCity" column="EMPCITY"/> <!-- 员工角度 多的一方,嵌入一的一方的各个属性请使用association --> <association property="dept" javaType="cn.resultMap.enetity.Dept"> <result property="deptName" column="DEPTNAME"/> <result property="deptNo" column="DEPTNO"/> </association> </resultMap> <select id="getAllEmps" resultMap="empMap"> select e.*,d.* from Emp e,Dept d where e.deptNo=d.deptNo </select> </mapper>
//resultMap:实体的属性名和表的字段名保证一致用resultMap
//如果报NullException查看小配置的映射关联resultMap是否配置
@Test
public void testAllEmp(){
SqlSession session=factory.openSession();
IEmpDao mapper = session.getMapper(IEmpDao.class);
List<Emp> allEmps = mapper.getAllEmps();
for (Emp emp : allEmps) {
System.out.println(emp.getEmpName()+"\t隶属部门"+emp.getDept().getDeptName());
}
session.close();
}
<!-- SQl标签的使用 --> <sql id="columns"> d.deptNo,d.deptName </sql> <!-- SQl标签的使用 --> <select id="getAllEmps" resultMap="empMap"> select e.*,<include refid="columns"/>from Emp e,Dept d where e.deptNo=d.deptNo </select>
<!-- Alias别名 小配置里面的type的属性值改成别名--> <typeAliases> <typeAlias type="cn.resultMap.enetity.Emp" alias="emp"/> </typeAliases>
if choose(when,otherwise) where set
package cn.resultMap.dao;
import java.util.List;
import cn.resultMap.enetity.Emp;
public interface IEmpDao {
//检索所有员工,以及隶属部门
public List<Emp> getAllEmps();
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.resultMap.dao.IEmpDao">
<resultMap type="cn.resultMap.enetity.Emp" id="empMap">
<id property="empId" column="EMPID"/>
<result property="empName" column="EMPNAME"/>
<result property="empCity" column="EMPCITY"/>
<!-- 员工角度 多的一方,嵌入一的一方的各个属性请使用association -->
<association property="dept" javaType="cn.resultMap.enetity.Dept">
<result property="deptName" column="DEPTNAME"/>
<result property="deptNo" column="DEPTNO"/>
</association>
</resultMap>
<select id="getAllEmps" resultMap="empMap">
select e.*,d.* from Emp e,Dept d
where e.deptNo=d.deptNo
</select>
<!--查询动态查询 -->
<select id="testAllEmpBuSelect" parameterType="cn.resultMap.enetity.Emp" resultType="cn.resultMap.enetity.Emp">
select * from Emp
<where>
<if test="empId!=null">
and empId=#{empId}
</if>
<if test="empName!=null">
and empName=#{empName}
</if>
<if test="empCity!=null">
and empCity=#{empCity}
</if>
</where>
</select>
</mapper>
//动态查询
@Test
public void testSelect(){
SqlSession session=factory.openSession();
Emp emp=new Emp();
//emp.setEmpName("331");
emp.setEmpCity("sh");
List<Emp> list = session.selectList("cn.resultMap.dao.IEmpDao.testAllEmpBuSelect",emp);
for (Emp emps : list) {
System.out.println(emps.getEmpName());
}
session.close();
}
<?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="cn.resultMap.dao.IDeptDao">
<resultMap type="cn.happy.entity.Dept" id="deptResultMap">
<id property="deptNo" column="deptNo"/>
<result property="deptName" column="deptName"/>
</resultMap>
<select id="getAllDept" resultMap="deptResultMap">
select d.*,e.* from Dept d,Emp e
where d.deptNo=e.deptNo and d.deptNo=#{deptNo}
</select>
<!--修改动态查询 -->
<select id="testUpdate" parameterType="int" resultType="cn.resultMap.enetity.Dept">
update dept
<set>
<if test="deptNo!=null">
deptNo=#{deptNo},
</if>
<if test="deptName!=null">
deptName=#{deptName},
</if>
</set>
where deptNo=#{deptNo}
</select>
</mapper>
/**
* 动态修改
* */
@Test
public void testUpdate(){
SqlSession session=factory.openSession();
Dept dept=new Dept();
dept.setDeptName("财务部");
dept.setDeptNo(1);
int count = session.update("cn.resultMap.dao.IDeptDao.testUpdate",dept);
session.commit();
System.out.println(count);
session.close();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有