Mapper.xml常用配置

  • 全局配置文件(数据库,事物管理,Mapper的注册、打印文件SQL、慢性加载、二级缓存)

  • Mapper配置文件 (定义自定义接口的具体方案;SQL、数据库、数据库与POJO的映射)

    多表关联查询:

    一对一,一对多、多对多

    单表查询:

    <select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
    select *from people where id=#{id}
    </select>

    业务:通过id查询people对象

    目标表:test/people

    实体类:com.souththwind.entity.Peaple

    Mapper.xml设置相关的配置,由Mybatis自动完成查询,生成POJO

    statement标签的主要属性由:id、parameterType\resultType

    id:对应接口的方法名;parameterType对应定义的参数类型;resultType定义查询的结果的数据类型(要一致)

    parameterType

    支持基本的数据类型、包装类、String、多参数、POJO

    1.基本数据类型:

    public  People findById(int id);
<select id="findById2" parameterType="int" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>

2.包装类:

public People findById(Integer id);
<select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>

3.String:

public  People findByName(String name);
<select id="findByName" parameterType="java.lang.String" resultType="com.shouthwind.entity.People">
select *from people where name=#{name}
</select>

4.多参数:

public  People findByIdAndName(Integer id,String name);
<select id="findByIdAndName" resultType="com.shouthwind.entity.People">
select * from people where id=#{param1} and name=#{param2}
</select>

5.POJO:

public  int update(People people);
<update id="update" parameterType="com.shouthwind.entity.People">
update people set name=#{name} ,money=#{money} where id=#{id}
</update>

resultType

parameterType基本一致:

1.基本数据类型

public int count();
<select id="count" resultType="int">
select count(*) from people
</select>

2.包装类

public Integer count();
<select id="count" resultType="java.lang.Integer">
select count(*) from people
</select>

3.String

public  String findNameById(Integer id);
<select id="findNameById" parameterType="java.lang.Integer" resultType="java.lang.String">
select name from people where id=#{id}
</select>

4.POJO

public People findById(Integer id);
<select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
select *from people where id=#{id}
</select>

多表关联查询

实际开发中最常用的一对多和多对多

一对多

1.建表

2.Sql数据库基础

3.创建实体类

package com.shouthwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Sc {
private Integer Sno;
private Integer Cno;
private Integer Grade;
private List<Student> students;
}
package com.shouthwind.entity;

import lombok.Data;

@Data
public class Student {
private Double Sno;
private String Sname;
private String Ssex;
private Integer Sage;
private String Sdept;
private Sc sc;
}

4.实际

package com.shouthwind.repository;

import com.shouthwind.entity.Student;

public interface StudentRepository {
public Student findById( Integer id);
}

resultType是直接将结果集与实体类映射的,名字一样就映射。

resultMap对结果集二次封装,根据需求来对结果集合分装。

<mapper namespace="com.shouthwind.repository.StudentRepository">
<resultMap id="studentMap" type="com.shouthwind.entity.Student">
<result column="sSno" property="Sno"></result>
<result column="sSname" property="Sname"></result>
<result column="sSsex" property="Ssex"></result>
<result column="sSage" property="Sage"></result>
<result column="sSdept" property="Sdept"></result>
<association property="sc" javaType="com.shouthwind.entity.Sc">
<result column="cSno" property= "Sno"></result>
<result column="cCno" property="Cno"></result>
<result column="cGrade" property="Grade"></result>
</association>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="studentMap">
select s.Sno sSno,s.Sname sSname,s.Ssex sSsex,s.Sage sSage,s.Sdept sSdept,c.Sno cSno,c.Cno cCno,c.Grade cGrade from student s,sc c where s.Sno=c.Sno and s.Sno=#{Sno}
</select>
</mapper>

Class:

package com.shouthwind.repository;

import com.shouthwind.entity.Sc;

public interface ClassRepository {
public Sc finById(Integer Cno);
}
<?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="com.shouthwind.repository.ClassRepository">
<resultMap id="classMapper" type="com.shouthwind.entity.Sc">
<id property="Cno" column="cCno"></id>
<result property="Sno" column="cSno"></result>
<result property="Grade" column="cGrade"></result>
<collection property="students" ofType="com.shouthwind.entity.Student">
<result column="sSno" property="Sno"></result>
<result column="sSname" property="Sname"></result>
<result column="sSsex" property="Ssex"></result>
<result column="sSage" property="Sage"></result>
<result column="sSdept" property="Sdept"></result>
</collection>
</resultMap>
<select id="finById" resultMap="classMapper">
select c.Sno cSno,c.Cno cCno,c.Grade cGrade,s.Sno sSno,s.Sname sSname,s.Ssex sSsex,s.Sage sSage,s.Sdept sSdept from sc c,student s where c.Cno=s.Cno and c.Cno=#{Cno}
</select>
</mapper>

注意:

1.association是封装成一个实体类:javaType设置数据类型

2.collection是封装成一个集合:ofType设置数据类型

多对多

多对多是一种双向的关系

1.建表

mysql> create table account_course(

-> id int(11) not null primary key auto_increment,

-> aid int(11) default null,

-> cid int(11) default null,

-> key aid(aid),

-> key cid(cid),

-> constraint account_course_ibfk_1 foreign key(aid) references account(id),

-> constraint account_course_ibfk_2 foreign key(aid) references t_course(id)

-> );

mysql> create table t_course(

-> id int(11) not null primary key auto_increment,

-> name varchar(11) default null

-> );

2.实体类

package com.shouthwind.entity;

import lombok.Data;

import java.util.List;

@Data
public class Account {
private Integer id;
private String name;
private List<Course> courses;
}
package com.shouthwind.entity;

import java.util.List;

public class Course {
private Integer id;
private String name;
private List<Student> students;
}

3.学生选课:

学生------》课

package com.shouthwind.repository;

import com.shouthwind.entity.Account;

public interface AccountRepository {
public Account findById(Integer id);
}
<?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="com.shouthwind.repository.AccountRepository">
<resultMap id="accoutMap" type="com.shouthwind.entity.Account">
<id column="aid" property="id"></id>
<result column="aname" property="name"></result>
<collection property="courses" ofType="com.shouthwind.entity.Course">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
</collection>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="accoutMap">
select a.id aid,a.name aname,c.id cid,c.name cname from account a,t_course c,account_course ac where a.id=#{id} and a.id=ac.aid and c.id=ac.cid
</select>
</mapper>

课程-------》学生

package com.shouthwind.repository;

import com.shouthwind.entity.Course;

public interface CourseRepository {
public Course findById(Integer id);
}
<?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="com.shouthwind.repository.CourseRepository">
<resultMap id="courseMap" type="com.shouthwind.entity.Course">
<id column="cid" property="id"></id>
<result column="cname" property="name"></result>
<collection property="accounts" ofType="com.shouthwind.entity.Account">
<id column="aid" property="id"></id>
<result column="aname" property="name"></result>
</collection>
</resultMap>
<select id="findById" parameterType="java.lang.Integer" resultMap="courseMap">
select a.id aid,a.name aname,c.id cid,c.name cname from account a,t_course c,account_course ac where c.id=#{id} and a.id=ac.aid and c.id=ac.cid
</select>
</mapper>

最新文章

  1. [LeetCode] Combinations 组合项
  2. Java IO4:字符编码
  3. 【转】C#类似Jquery的html解析类HtmlAgilityPack基础类介绍及运用
  4. BZOJ4439——[Swerc2015]Landscaping
  5. Linux_查看linux并发连接数
  6. 1、iOS9 HTTP 不能正常使用的解决办法
  7. vagrant在windows下的使用
  8. wpa_cli调试工具的使用
  9. 对Cookie进行增删改查
  10. 二十七、Java图形化界面设计——容器(JFrame)
  11. HTML——使用表格对表单进行布局
  12. 超全!整理常用的iOS第三方资源
  13. Js、Jquery定时执行(一次或者重复多次,取消重复)
  14. 深度学习与计算机视觉系列(3)_线性SVM与SoftMax分类器
  15. 请求超时VUE axios重新再次请求
  16. express.Router
  17. 使用ie的filter来解决rgba在IE8下没有效果的问题
  18. mysql 慢查询日志,灾难日志恢复,错误日志
  19. SaltStack配置管理-jinja模板
  20. react 中使用 lodash 中的 _.throttle

热门文章

  1. golang面向对象
  2. CPU TLB原理 [转载好文]
  3. jsp 页面返回、本页面刷新
  4. http转成https工具类
  5. Linux系统CentOS6找回密码解决方法
  6. VmWare安装Centos后配置Net网络SSH链接问题看这一遍就够了
  7. js 获取当前时间转换时间戳 (毫秒)
  8. 【转载】EXCEL VBA 自定义排序的三种方法
  9. [常用工具] Python视频处理库VidGear使用指北
  10. Redis 数据结构-双向链表