数据输出:
mapper接口插入 删除 修改时 返回值int就行 只有查询才有resultType
<mapper namespace="com.dc.mapper.EmployeeMapper">
<delete id="deleteById">
delete from t_emp where emp_id=#{
id}
</delete>
<select id="queryNameByIdById" resultType="java.lang.String">
select emp_name from t_emp where emp_id=#{
id}
</select>
<select id="querySalaryById" resultType="java.lang.Double">
select emp_salary from t_emp where emp_id=#{
id}
</select>
<select id="queryById" resultType="com.dc.pojo.Employee">
select emp_id empId,emp_name empName,emp_salary empSalary from t_emp where emp_id=#{
id}
</select>
<select id="selectEmpAndMaxSalary" resultType="java.util.Map">
SELECT
emp_name 员工姓名,
emp_salary 员工工资,
(SELECT AVG(emp_salary) FROM t_emp) 部门平均工资
FROM t_emp WHERE emp_salary=(
SELECT MAX(emp_salary)From t_emp
)
</select>
<select id="queryNameBySalary" resultType="java.lang.String">
select emp_name from t_emp where emp_salary=#{
salary}
</select>
<select id="queryAll" resultType="com.dc.pojo.Employee">
select emp_id empId,emp_name empName,emp_salary empSalary from t_emp
</select>
</mapper>
主键回显
//自增长主键
<insert id="insertEmp" useGeneratedKeys="true" keyColumn="emp_id" keyProperty="empId">
insert into t_emp(emp_name,emp_salary) value(#{
empName},#{
empSalary})
</insert>
employee.getEmpId()
//非自增长主键
<insert id="insertTeacher">
<selectKey order="BEFORE" resultType="java.lang.String" keyProperty="tId">
select replace(uuid(),'-','') //uUId()随机生成,然后去掉-
</selectKey>
insert into teacher(t_id,t_name) value(#{
tId},#{
tName})
</insert>
事务自动提交
SqlSession sqlSession = sqlSessionFactory.openSession(true);
列名和属性名不一致
1.select t_id tId,t_name tName from teacher where t_id=#{tId}
2.开启驼峰式映射
3.resultMap自定义映射 (resultType自动映射)
<resultMap id="tMap" type="com.dc.pojo.Teacher">
<id column="t_id" property="tId"></id>
<result column="t_name" property="tName"></result>
</resultMap>
<select id="queryById" resultMap="tMap">
select * from teacher where t_id=#{
id}
</select>
原文链接: https://blog.csdn.net/qq_53568730/article/details/135439855