MyBatis 6 入参为HashMap、 MyBatis调用存储过程执行CRUD

入参为HashMap、 MyBatis调用存储过程执行CRUD

学习于https://study.163.com/course/courseMain.htm?courseId=1005847005&_trace_c_p_k2_=82d8e4a53fbd48c5842d426aad41b6d2

项目结构

1.

CREATE TABLE empinfo (
eid INT (10) PRIMARY KEY,
name VARCHAR (20),
age INT(10),
sex VARCHAR(5)
);

2.

<?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"/>
	
	<typeAliases>
		<!-- 单个定义别名 -->
		<!-- <typeAlias type="org.lanqiao.entity.Empinfo" alias="empinfo"/> -->
		<!-- 批量定义别名 -->
		<package name="org.lanqiao.entity"/>
	</typeAliases>

	<!--通过 environments的 default值和environments的id值来指定MyBatis运行时的数据库环境-->
	 <environments default="development">
		 <environment id="development">
			 <transactionManager type="JDBC"/>
			 <dataSource type="POOLED">
			 	<!--修改的第一处,配置数据库信息,根据自己的数据库修改-->
				 <property name="driver" value="${driver}"/>
				 <property name="url" value="${url}"/>
				 <property name="username" value="${username}"/>
				 <property name="password" value="${password}"/>
			 </dataSource>
	 	</environment>
	 </environments>
	 <mappers>
	 	<!--加载映射文件-->
	 	<mapper resource="org/lanqiao/mapper/empinfoMapper.xml"/><!--修改的第二处-->
	 </mappers>
</configuration>

3.

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/company
username=root
password=root

4.

package org.lanqiao.entity;
 
public class Empinfo {
	private int eid;
	private String name;
	private int age;
	private String sex;
	private String phone;
	
	//无参构造
	public Empinfo () {
	}
	//有参构造
	public Empinfo (int eid, String name, int age, String sex, String phone) {
		super();
		this.eid = eid;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.phone = phone;
	}
	
	/**
	 * @return the eid
	 */
	public int getEid() {
		return eid;
	}
	/**
	 * @param eid the eid to set
	 */
	public void setEid(int eid) {
		this.eid = eid;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}
	/**
	 * @param age the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}
	/**
	 * @return the sex
	 */
	public String getSex() {
		return sex;
	}
	/**
	 * @param sex the sex to set
	 */
	public void setSex(String sex) {
		this.sex = sex;
	}
	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return eid+"-"+name+"-"+age+"-"+sex+"-"+phone ;
	}
 
}

5.

<?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.lanqiao.mapper.EmpinfoMapper">
	
 	<!-- ().按年龄或名字查询Empinfo进行模糊查询,两种方法 -->
 	<select id="queryEmpinfoByageOrname" parameterType="empinfo" resultType="empinfo" >	
 	<!-- select * from empinfo where age=#{age} or name like #{name} -->
 	select * from empinfo where age=#{age} or name like '%${name}%'
 	</select>
 	<!-- ().按年龄或名字查询Empinfo进行模糊查询(输入参数HashMap) -->
 	<select id="queryEmpinfoByageOrnameWithHashMap" parameterType="HashMap" resultType="empinfo" >	
 	select * from empinfo where age=#{age} or name like '%${name}%'
 	</select>
 	<!-- 通过调用[存储过程]实现查询,statementType="CALLABLE" -->
 	<!-- <select id="queryCountByageWithProcedure" statementType="CALLABLE"  parameterType="HashMap" >	
 		{CALL queryCountByageWithProcedure(#{name,jdbcType=VARCHAR mode=IN },#{ecount,jdbcTypeINTEGER mode=OUT})}
 	</select> -->
 	
</mapper>

6.

package org.lanqiao.mapper;


import java.util.List;
import java.util.Map;

import org.lanqiao.entity.Empinfo;

public interface EmpinfoMapper {	
	//().按年龄或名字查询Empinfo进行模糊查询
	List<Empinfo> queryEmpinfoByageOrname(Empinfo empinfo);
	//().按年龄或名字查询Empinfo进行模糊查询(输入参数HashMap)
	List<Empinfo> queryEmpinfoByageOrnameWithHashMap(Map<String,Object> map);
	//根据存储过程某个年龄的Empinfo总数
	void queryCountByageWithProcedure(Map<String,Object> params);
}

7.

package org.lanqiao.test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.lanqiao.entity.Empinfo;
import org.lanqiao.mapper.EmpinfoMapper;

public class TestEmpinfo {
	//().按年龄或名字查询Empinfo进行模糊查询,两种方法
	public static void queryEmpinfoByageOrname() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		
		Empinfo empinfo=new Empinfo();
		empinfo.setAge(22);
//		empinfo.setName("%f%");
		empinfo.setName("f");
		List<Empinfo> empinfos=empinfoMapper.queryEmpinfoByageOrname(empinfo);
		System.out.println(empinfos);
		session.close();
	}
	//().按年龄或名字查询Empinfo进行模糊查询(输入参数HashMap)
	public static void queryEmpinfoByageOrnameWithHashMap() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		
		Map<String,Object> empinfoMap=new HashMap<>();
		empinfoMap.put("age",23);
		empinfoMap.put("name","maliya");
		
		List<Empinfo> empinfos=empinfoMapper.queryEmpinfoByageOrnameWithHashMap(empinfoMap);
		System.out.println(empinfos);
		session.close();
	}
	//通过调用[存储过程]实现查询
	public static void queryCountByageWithProcedure() throws IOException {
		Reader reader = Resources.getResourceAsReader("empinfoConf.xml");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);		
		SqlSession session = sessionFactory.openSession();
		
		EmpinfoMapper empinfoMapper = session.getMapper(EmpinfoMapper.class);
		//通过Map给存储过程指定输入参数
		Map<String,Object> params=new HashMap<>();
		params.put("age",23);//指定存储过程的输入参数是23
		
		empinfoMapper.queryCountByageWithProcedure(params);//调用存储过程
		//获取存储过程的输出参数
		Object count=params.get("count");
		System.out.println(count);
		session.close();
	}
	public static void main(String[] args) throws IOException {
		//queryEmpinfoByageOrname();
		//queryEmpinfoByageOrnameWithHashMap();
		queryCountByageWithProcedure();//未完成,错误
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值