| Map<String, Object> getUserToMap(@Param("id") int id); |
| |
| <select id="getUserToMap" resultType="map"> select * from t_user where id = #{id} </select> |
| List<Map<String, Object>> getAllUserToMap(); |
| |
| <select id="getAllUserToMap" resultType="map"> select * from t_user </select> |
| @MapKey("id") |
| Map<String, Object> getAllUserToMap(); |
| |
| <select id="getAllUserToMap" resultType="map"> select * from t_user </select> |
| |
| { 1={password=123456, sex=男, id=1, age=23, username=admin}, 2={password=123456, sex=男, id=2, age=23, username=张三}, 3={password=123456, sex=男, id=3, age=23, username=张三} } |
| List<User> testMohu(@Param("mohu") String mohu); |
| |
| <select id="testMohu" resultType="User"> |
| |
| |
| select * from t_user where username like "%"#{mohu}"%" |
| </select> |
| mapper.deleteMore("1, 2, 3"); |
| |
| int deleteMore(@Param("ids") String ids); |
| |
| <delete id="deleteMore"> delete from t_user where id in (${ids}) </delete> |
| List<User> getAllUser(@Param("tableName") String tableName); |
| |
| <select id="getAllUser" resultType="User"> select * from ${tableName} </select> |
| |
| int insertUser(User user); |
| |
| <insert id="insertUser" useGeneratedKeys="true" keyProperty="id"> |
| insert into t_user values(null,#{username},#{password},#{age},#{sex}) |
| </insert> |
| |
| List<Emp> getAllEmp(); |
| |
| <select id="getAllEmp" resultMap="Emp"> |
| select id, emp_name empName, age from t_emp |
| </select> |
| # mybatis配置文件中配置如下 |
| <settings> |
| |
| <setting name="mapUnderscoreToCamelCase" value="true"/> |
| </settings> |
| |
| # mapper |
| List<Emp> getAllEmp(); |
| |
| # xml映射 |
| <select id="getAllEmp" resultMap="Emp"> |
| select id, emp_name, age from t_emp |
| </select> |
| # mapper |
| List<Emp> getAllEmp(); |
| |
| # xml映射 |
| <resultMap id="empResultMap" type="Emp"> |
| <id property="eid" column="eid"></id> |
| <result property="empName" column="emp_name"></result> |
| <result property="age" column="age"></result> |
| <result property="sex" column="sex"></result> |
| <result property="email" column="email"></result> |
| </resultMap> |
| |
| <select id="getAllEmp" resultMap="empResultMap"> |
| select id, emp_name, age from t_emp |
| </select> |
| # 部门表 |
| public class Dept { |
| |
| private Integer did; |
| |
| private String deptName; |
| |
| |
| |
| @Override |
| public String toString() { |
| return "Dept{" + |
| "did=" + did + |
| ", deptName='" + deptName + '\'' + |
| |
| '}'; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public Integer getDid() { |
| return did; |
| } |
| |
| public void setDid(Integer did) { |
| this.did = did; |
| } |
| |
| public String getDeptName() { |
| return deptName; |
| } |
| |
| public void setDeptName(String deptName) { |
| this.deptName = deptName; |
| } |
| |
| public Dept() { |
| } |
| |
| public Dept(Integer did, String deptName) { |
| this.did = did; |
| this.deptName = deptName; |
| } |
| } |
| |
| # 员工表,添加部门对象作为属性,编写getter方法和setter方法,toString方法 |
| public class Emp implements Serializable { |
| |
| private Integer eid; |
| |
| private String empName; |
| |
| private Integer age; |
| |
| private String sex; |
| |
| private String email; |
| |
| private Dept dept; |
| |
| @Override |
| public String toString() { |
| return "Emp{" + |
| "eid=" + eid + |
| ", empName='" + empName + '\'' + |
| ", age=" + age + |
| ", sex='" + sex + '\'' + |
| ", email='" + email + '\'' + |
| ", dept=" + dept + |
| '}'; |
| } |
| |
| public Dept getDept() { |
| return dept; |
| } |
| |
| public void setDept(Dept dept) { |
| this.dept = dept; |
| } |
| |
| public Integer getEid() { |
| return eid; |
| } |
| |
| public void setEid(Integer eid) { |
| this.eid = eid; |
| } |
| |
| public String getEmpName() { |
| return empName; |
| } |
| |
| public void setEmpName(String empName) { |
| this.empName = empName; |
| } |
| |
| public Integer getAge() { |
| return age; |
| } |
| |
| public void setAge(Integer age) { |
| this.age = age; |
| } |
| |
| public String getSex() { |
| return sex; |
| } |
| |
| public void setSex(String sex) { |
| this.sex = sex; |
| } |
| |
| public String getEmail() { |
| return email; |
| } |
| |
| public void setEmail(String email) { |
| this.email = email; |
| } |
| |
| public Emp() { |
| } |
| |
| public Emp(Integer eid, String empName, Integer age, String sex, String email) { |
| this.eid = eid; |
| this.empName = empName; |
| this.age = age; |
| this.sex = sex; |
| this.email = email; |
| } |
| } |
| |
| Emp getEmpAndDept(@Param("eid") Integer eid); |
| |
| <resultMap id="empAndDeptResultMapOne" type="Emp"> |
| <id property="eid" column="eid"></id> |
| <result property="empName" column="emp_name"></result> |
| <result property="age" column="age"></result> |
| <result property="sex" column="sex"></result> |
| <result property="email" column="email"></result> |
| <result property="dept.did" column="did"></result> |
| <result property="dept.deptName" column="dept_name"></result> |
| </resultMap> |
| |
| <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne"> |
| select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid} |
| </select> |
| Emp getEmpAndDept(@Param("eid") Integer eid); |
| |
| <resultMap id="empAndDeptResultMapTwo" type="Emp"> |
| <id property="eid" column="eid"></id> |
| <result property="empName" column="emp_name"></result> |
| <result property="age" column="age"></result> |
| <result property="sex" column="sex"></result> |
| <result property="email" column="email"></result> |
| |
| |
| |
| |
| |
| <association property="dept" javaType="Dept"> |
| <id property="did" column="did"></id> |
| <result property="deptName" column="dept_name"></result> |
| </association> |
| </resultMap> |
| |
| <select id="getEmpAndDept" resultMap="empAndDeptResultMapOne"> |
| select * from t_emp left join t_dept on t_emp.did = t_dept .did where t_emp.eid = #{eid} |
| </select> |
| # 部门mapper |
| Dept getEmpDeptByStep(@Param("did") int did); |
| # 部门映射文件,根据部门id查询部门信息 |
| <select id="getEmpDeptByStep" resultType="Dept"> |
| select * from t_dept where did = #{did} |
| </select> |
| |
| # 员工mapper |
| Emp getEmpByStep(@Param("eid") int eid); |
| # 员工映射文件 |
| <resultMap id="empDeptStepMap" type="Emp"> |
| <id column="eid" property="eid"></id> |
| <result column="ename" property="ename"></result> |
| <result column="age" property="age"></result> |
| <result column="sex" property="sex"></result> |
| |
| <association property="dept" select="com.atguigu.MyBatis.mapper.DeptMapper.getEmpDeptByStep" column="did"> |
| </association> |
| </resultMap> |
| |
| <select id="getEmpByStep" resultMap="empDeptStepMap"> |
| select * from t_emp where eid = #{eid} |
| </select> |
| # lazyLoadingEnabled设置为true,aggressiveLazyLoding设置为false,mybatis版本>=3.4.1时默认为true |
| <settings> |
| <setting name="lazyLoadingEnabled" value="true"/> |
| </settings> |
| # 部门表,添加员工对象,并添加getter和setter方法,toString方法 |
| public class Dept { |
| |
| private Integer did; |
| |
| private String deptName; |
| |
| private List<Emp> emps; |
| |
| @Override |
| public String toString() { |
| return "Dept{" + |
| "did=" + did + |
| ", deptName='" + deptName + '\'' + |
| ", emps=" + emps + |
| '}'; |
| } |
| |
| public List<Emp> getEmps() { |
| return emps; |
| } |
| |
| public void setEmps(List<Emp> emps) { |
| this.emps = emps; |
| } |
| |
| public Integer getDid() { |
| return did; |
| } |
| |
| public void setDid(Integer did) { |
| this.did = did; |
| } |
| |
| public String getDeptName() { |
| return deptName; |
| } |
| |
| public void setDeptName(String deptName) { |
| this.deptName = deptName; |
| } |
| |
| public Dept() { |
| } |
| |
| public Dept(Integer did, String deptName) { |
| this.did = did; |
| this.deptName = deptName; |
| } |
| } |
| |
| # mapper |
| Dept getDeptEmpByDid(@Param("did") int did); |
| |
| # xml,配置返回结果集,根据部门id查询数据 |
| <resultMap id="deptEmpMap" type="Dept"> |
| <id property="did" column="did"></id> |
| <result property="dname" column="dname"></result> |
| <!--ofType:设置collection标签所处理的集合属性中存储数据的类型 --> |
| <collection property="emps" ofType="Emp"> |
| <id property="eid" column="eid"></id> |
| <result property="ename" column="ename"></result> |
| <result property="age" column="age"></result> |
| <result property="sex" column="sex"></result> |
| </collection> |
| </resultMap> |
| |
| <select id="getDeptEmpByDid" resultMap="deptEmpMap"> |
| select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did = emp.did where dept.did = #{did} |
| </select> |
| # 部门mapper |
| Dept getDeptByStep(@Param("did") int did); |
| # 部门xml,根据部门id查询数据 |
| <resultMap id="deptEmpStep" type="Dept"> |
| <id property="did" column="did"></id> |
| <result property="dname" column="dname"></result> |
| <collection property="emps" fetchType="eager" select="com.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid" column="did"></collection> |
| </resultMap> |
| |
| <select id="getDeptByStep" resultMap="deptEmpStep"> |
| select * from t_dept where did = #{did} |
| </select> |
| |
| # 员工mapper |
| List<Emp> getEmpListByDid(@Param("did") int did); |
| # 员工xml,根据员工id查询数据 |
| <select id="getEmpListByDid" resultType="Emp"> |
| select * from t_emp where did = #{did} |
| </select> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术