栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis Day03(20210927)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis Day03(20210927)

一.封装工具类
package com.neudeu.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisUtils {
	
	private static SqlSessionFactory sqlSessionFactory;

	static {
		try {
			// 定义mybaties的配置文件路径
			String resource = "SqlMapConfig.xml";
			// 得到配置文件流
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 创建会话工厂,传入mybaties的配置文件流
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SqlSession getSession() {
		return sqlSessionFactory.openSession();
	}

}
二.数据库操作 1.插入记录,并获取主键

sql增删改都会返回int值,表示影响的行数,但insert标签中不能写resultType属性,所以想要获得插入记录的主键值需要经过一定处理;
常见于 插入于A表的该行记录主键 要作为 B表要插入记录的外键 时应用;

1.1使用selectKey标签
  • keyProperty属性:就是主键名,对应java属性名;
  • resultType属性:指的是主键的数据类型;
  • order属性:执行的顺序 对应after和before两个值
    before:先获取主键,在insert之前;
    after:先insert,再获取主键;

DeptMapper.xml



	
	
		
		 
			select LAST_INSERT_ID()
		 	
		insert into dept(dname,loc) values(#{dname},#{loc})
	
	

1.2 使用useGeneratedKeys属性
  • useGeneratedKeys设置为true ,自动获取数据库中的主键;
  • keyProperty属性值设置为主键名;
  • 只适用与有自增长的数据库(mysql,SqlServer);

DeptMapper.xml



	
	 	
		insert into dept(dname,loc) values(#{dname},#{loc})
	
	

mybatis会使用JDBC的getGeneratedkeys方法获取由数据库内部自动生成的主键,
并将该值赋值给由keyProperty指定的属性

接口:DeptMapper.java

public interface DeptMapper {

	//插入部门信息,返回值类型设置为主键数据类型
	int insertDept(Dept dept);
	
}

测试类Test02.java

public class Test02 {
	
	@Test
	public void insertDeptRecord() {
		// 创建sqlsession
		SqlSession sqlSession = MybatisUtils.getSession();
		// 获取mapper代理对象,加载字节码文件
		DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
		// 通过代理对象调用方法
		Dept dept = new Dept();
		dept.setDname("安保部");
		dept.setLoc("大连");
		//返回的是影响行数
		//int row = mapper.insertDept(dept);
		mapper.insertDept(dept);
		//提交事务
		sqlSession.commit();
		//释放资源
		sqlSession.close();
		
		System.out.println(dept.getDeptno());
	}
2.多条件查询
  • 将参数类型parameterType设置为对象类型,通过对象属性传入多个条件;

例子:根据部门名称和部门编号查找部门信息;要求名字中包含‘勤’字,且编号大于10。

DeptMapper.xml


	
		select * from dept
		
			
				and dname like '%${dname}%'
			
			
				and deptno ">> #{deptno}
			
		
	

接口:DeptMapper.java

public interface DeptMapper {

	// 根据部门名称和编号查询部门信息(动态sql-ifwhere)
	List findDeptByIfWhere(Dept dept);
	
}

测试类Test01_Dept.java

	@Test
	public void findDeptByIfWhere() {
		// 创建sqlSession
		SqlSession sqlSession = MybatisUtils.getSession();
		// 获取代理对象
		DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
		// 创建对象
		Dept dept = new Dept();
		dept.setDeptno(100);
//		dept.setDname("勤");
		
		// 通过mapper对象调用方法
		List list = mapper.findDeptByIfWhere(dept);
		for (Dept d : list) {
			System.out.println(d.getDeptno());
			System.out.println(d.getDname());
		}
		
		sqlSession.close();
	}
2.choose标签
  • 自上而下执行when标签;
  • 当某一个条件满足时,执行该标签内容,下面when标签中的内容则不再执行;
  • 当所有when标签中的条件都不满足时,执行otherwise标签中内容;
    (otherwise标签也可以不写)

DeptMapper.xml


	
		select * from dept where deptno in
		
			#{deptno}
		
	

接口:DeptMapper.java

public interface DeptMapper {

	//查询部门编号是10,20,30的部门信息(foreach标签)
	List findDeptByIds(int[] arr);
	
}

测试类Test01_Dept.java

	@Test
	public void findDeptByIds() {
		// 创建sqlSession
		SqlSession sqlSession = MybatisUtils.getSession();
		// 获取代理对象
		DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
		//定义数组
		int[] arr = {10,20,30};
		// 使用mapper对象调用方法
		List list = mapper.findDeptByIds(arr);
		for(Dept dept:list) {
			System.out.println(dept);
		}
		
		sqlSession.close();
	}
3.2遍历集合

DeptMapper.xml


	
		SELECT
			emp.*,
			dept.deptno AS 'dno',
			dept.dname AS 'dn',
			dept.loc AS 'dl'
		FROM
			emp
		LEFT JOIN dept ON emp.deptno = dept.deptno
		WHERe emp.empno = #{empno}
	

接口:EmpMapper.java

public interface EmpMapper {
	//定义方法--根据员工编号查询员工信息以及员工所在部门信息
	Emp selectEmpAndDeptById(int empno);
}

测试类Test02_Emp.java

	@Test
	public void findEmpAndDeptTest() {
		// 创建sqlSession
		SqlSession sqlSession = MybatisUtils.getSession();
		// 获取代理对象
		EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
		//访问方法
		Emp emp = mapper.selectEmpAndDeptById(7788);
		
		System.out.println(emp);
		int d = emp.getDept().getDeptno();
		System.out.println(d);
		
		sqlSession.close();
	}
五.输入/输出类型总结 1.输入映射类型(parameterType)
  • 当sql语句需要一个参数时
    1.接口方法参数为一个基本数据类型:parameterType配置一个基本数据类型;
  • 当sql语句需要多个参数时
    1.接口方法参数为一个实体对象类型:parameterType配置一个实体对象类型;
    2.接口方法参数为一个集合类型(List,Map):parameterType配置集合中元素的类型;
2.输出映射类型(resultType)
  • 当sql语句中的字段名与实体对象中的属性名一致时,使用resultType:
    1.返回一条记录时,resultType可以配置成对象类型
    2.返回多条记录时,resultType也要配置成对象类型(表示集合中存储的对象)
    3.返回一条记录,且只有一列时,resultType可以配置成简单数据类型。
  • 当sql语句中的字段名与实体对象中的属性名不一致时,使用resultMap:
    1.在resultMap中,显式的书写sql字段名与实体对象属性名的映射
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271106.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号