文章目录
1.数据的输入
1.1mybatis总体机制的概括
在测试的时候,进行数据的输入,通过接口传参,传到Sql语句中。
这里的数据输入就是上层的方法(Service方法)调用Mapper接口时,数据传入的形式。
简单类型:只包含一个值的数据类型
基本数据类型:int、byte、short、double、……
基本数据类型的包装类型:Integer、Character、Double、……
字符串类型:String
复杂类型:包含多个值的数据类型
实体类类型:Employee、Department、……
集合类型:List、Set、Map、……
数组类型:int[]、String[]、……
复合类型:List、实体类中包含集合……
1.2单个简单的类型的参数
mapper接口
Employee selectEmployeeById(Integer empId);
employeeMapper.xml
<!--int selectEmployeeById(Integer empId);-->
<select id="selectEmployeeById" resultType="com.sdjzu.mybatis.entity.Employee">
select emp_id empId,emp_name empName,emp_salary empSalary
from t_emp
where emp_id=#{empId}
</select>
注意返回的类型
测试
@Test
public void testselectEmployeeById(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = mapper.selectEmployeeById(4);
System.out.println(employee);
}
1.3实体类类型的参数
mapper接口
int insertEmployee(Employee employee);
employeeMapper.xml
<!--int insertEmployee(Employee employee)-->
<insert id="insertEmployee">
insert into t_emp(emp_name,emp_salary) values(#{empName},#{empSalary})
</insert>
测试
@Test
public void testInsertEmployee(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
int hwj = mapper.insertEmployee(new Employee(null, "hwj", 67.00));
System.out.println(hwj);
}
mybatis中的#{}会根据传入的数据,生成对应 的getXxx方法,通过反射的实体类的对象调用getXxx方法,进而获取值
1.4零散性的数据类型
mapepr接口
int updateEmployee(@Param("empId") Integer empId,@Param("empSalary") Double empSalary);
employeeMapper.xml
<!--int updateEmployee(Integer empId,Double empSalary);-->
<update id="updateEmployee">
update t_emp
set emp_salary=#{empSalary} where emp_id=#{empId}
</update>
测试
@Test
public void testUpdateEmployee(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
int i = mapper.updateEmployee(8, 347.34);
System.out.println(i);
}
1.5Map类型的参数
mapper接口
int updateEmployeeParamMap(Map<String,Object> paramMap);
Employeemapper.xml
<!--int updateEmployeeParamMap(Map<String,Object> paramMap);-->
<update id="updateEmployeeParamMap">
update t_emp
set emp_salary=#{empSalary} where emp_id=#{empId}
</update>
测试
@Test
public void testUpdateEmployeeParamMap(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Map<String,Object> map=new HashMap<>();
map.put("empId",8);
map.put("empSalary",4765.00);
int i = mapper.updateEmployeeParamMap(map);
System.out.println(i);
}
#{}中写Map中的key
2.数据的输出
2.1返回单个数据类型
mapper接口
int countEmployee();
<!--int countEmployee();-->
<select id="countEmployee" resultType="int">
select count(*)
from t_emp
</select>
@Test
public void testCountEmployee(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
int i = mapper.countEmployee();
System.out.println(i);
}
2.2返回实体类数据类型
Employee selectEmployeeById(Integer empId);
<!--int selectEmployeeById(Integer empId);-->
<select id="selectEmployeeById" resultType="com.sdjzu.mybatis.entity.Employee">
select emp_id empId,emp_name empName,emp_salary empSalary
from t_emp
where emp_id=#{empId}
</select>
1.以别名的形式将数据库表中的字段和java中的属性对应起来
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
2.在全局配置文件中设置驼峰命名
2.3返回的数据类型为Map
适用于SQL查询返回的各个字段综合起来并不和任何一个现有的实体类对应,没法封装到实体类对象中。能够封装成实体类类型的,就不使用Map类型
Map<String ,Object> selectEmpNameAndEmpSalaryById(Integer empId);
<!-- Map<String ,Object> selectEmpNameAndEmpSalaryById();-->
<select id="selectEmpNameAndEmpSalaryById" resultType="map">
select emp_name,emp_salary
from t_emp
where emp_id=#{empId}
</select>
@Test
public void testSelectEmpNameAndEmpSalaryById(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Map<String, Object> map = mapper.selectEmpNameAndEmpSalaryById(6);
Set<String> setKey= map.keySet();
for(String key:setKey){
Object value=map.get(key);
System.out.println(key + "=" + value);
}
}
2.4返回的数据类型为List
查询结果返回多个实体类对象,希望把多个实体类对象放在List集合中返回。此时不需要任何特殊处理,在resultType属性中还是设置实体类类型即可。
List<Employee> selectAll();
<!--List<Employee> selectAll();-->
<select id="selectAll" resultType="com.sdjzu.mybatis.entity.Employee">
select emp_id,emp_name,emp_salary
from t_emp
</select>
@Test
public void testSelectAll(){
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
List<Employee> list=mapper.selectAll();
for(Employee emp:list){
System.out.println(emp);
}
}
2.5返回自增主键
<!-- int insertEmployee(Employee employee); -->
<!-- useGeneratedKeys属性字面意思就是“使用生成的主键” -->
<!-- keyProperty属性可以指定主键在实体类对象中对应的属性名,Mybatis会将拿到的主键值存入这个属性 -->
<insert id="insertEmployee" useGeneratedKeys="true" keyProperty="empId">
insert into t_emp(emp_name,emp_salary)
values(#{empName},#{empSalary})
</insert>
3.数据库表字段和实体类属性对应关系
①别名
将字段的别名设置成和实体类属性一致。
<!-- 编写具体的SQL语句,使用id属性唯一的标记一条SQL语句 -->
<!-- resultType属性:指定封装查询结果的Java实体类的全类名 -->
<select id="selectEmployee" resultType="com.atguigu.mybatis.entity.Employee">
<!-- Mybatis负责把SQL语句中的#{}部分替换成“?”占位符 -->
<!-- 给每一个字段设置一个别名,让别名和Java实体类中属性名一致 -->
select emp_id empId,emp_name empName,emp_salary empSalary from t_emp where emp_id=#{maomi}
</select>
关于实体类属性的约定:
getXxx()方法、setXxx()方法把方法名中的get或set去掉,首字母小写。
②全局配置自动识别驼峰式命名规则
在Mybatis全局配置文件加入如下配置:
<!-- 使用settings对Mybatis全局进行设置 -->
<settings>
<!-- 将xxx_xxx这样的列名自动映射到xxXxx这样驼峰式命名的属性名 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
SQL语句中可以不使用别名
<!-- Employee selectEmployee(Integer empId); -->
<select id="selectEmployee" resultType="com.atguigu.mybatis.entity.Employee">
select emp_id,emp_name,emp_salary from t_emp where emp_id=#{empId}
</select>
③使用resultMap
使用resultMap标签定义对应关系,再在后面的SQL语句中引用这个对应关系
<!-- 专门声明一个resultMap设定column到property之间的对应关系 -->
<resultMap id="selectEmployeeByRMResultMap" type="com.atguigu.mybatis.entity.Employee">
<!-- 使用id标签设置主键列和主键属性之间的对应关系 -->
<!-- column属性用于指定字段名;property属性用于指定Java实体类属性名 -->
<id column="emp_id" property="empId"/>
<!-- 使用result标签设置普通字段和Java实体类属性之间的关系 -->
<result column="emp_name" property="empName"/>
<result column="emp_salary" property="empSalary"/>
</resultMap>
<!-- Employee selectEmployeeByRM(Integer empId); -->
<select id="selectEmployeeByRM" resultMap="selectEmployeeByRMResultMap">
select emp_id,emp_name,emp_salary from t_emp where emp_id=#{empId}
</select>