- 使用foreach动态sq
l<?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"> <!--注:namespace的内容就是ProductMapper接口的全限定名--> <mapper namespace="com.chen.mybatis._01_batch.mapper.ProductMapper"> ... <!-- 在mybatis中,咱们可以传集合与数组过来,mybatis会把它变成Map 如果传的是集合,对应的key就是list 如果传的是数组,对应的key就是array 执行批量添加功能 1.parameterType中接收的是集合类型(也可以写全限定名:java.util.List) 2.collection="list" 使用集合的话,默认就是使用list与collection来接收 3.separator="," 每遍历一次后的分隔符 4.item="p" 每次遍历的这个对象别名,可以修改 --> <insert id="batchInsert"> insert into product (name,price) values <foreach collection="list" separator="," item="p"> (#{p.name},#{p.price}) </foreach> </insert> </mapper>
- 案例二:批量删除
<mapper namespace="com.chen.mybatis._01_batch.mapper.ProductMapper">
...
<!--
执行批量添加功能
1.open="(" 以什么开始
2.close=")" 以什么结束
-->
<delete id="batchDelete" parameterType="list">
delete from product where id in
<foreach collection="list" separator="," item="id" open="(" close=")">
#{id}
</foreach>
</delete>
</mapper>
- 在mybatis动态有以下标签
- foreach 用于循环
- if 用于判断【这个咱们项目高级查询中使用过,可自行复习】
- foreach中的属性
- collection="list" 使用集合的话,默认就是使用list与collection来接收
- separator="," 每遍历一次后的分隔符
- item="p" 每次遍历的这个对象别名,可以修改
- index:获取当前遍历索引(一般没有作用)
- open:拼接sql以什么开始
- close:拼接sql以什么结束
关联关系的分类
- 一对一:一个员工只有一个身份证号。随意一方设计一个字段
- 多对一:多个员工对应一个部门。一般在多方设计一个一方属性 员工里面设计部门字段
- 一对多:一个部门拥有多个员工。还是在多方维护对象关联关系
- 多对多: 一个员工有多个角色。一个角色属于多个员工。用中间表来表示
- 本质上:多对一,一对一是一样的,都只是处理一个(association )。而一对多、多对多也是一样处理的都是集合(collection)
关联映射处理方式
- MyBatis提供两种方式处理我们关联对象,嵌套查询和嵌套结果。
- 嵌套结果: 发送1条SQL,查询所有的信息(本身+关联对象)
- 嵌套查询:发送1+N条sql。
接下来,分别使用两种方式对多对一、一对一和一对多、多对多进行处理。
嵌套结果查询:
需要的jar包:
test测试需要的jar包:
其他代码都与Mybatis——进阶中的一致;只有mapper.xml映射文件有改动:
改动文件代码:
一、一对一/多对一
1)嵌套结果:
<?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.softeem.dao.EmployeeMapper">
<resultMap id="empMap" type="com.softeem.domain.Employee"><!--namespace=dao接口的全限定名-->
<id column="id" property="id"></id><!--column = 数据库的属性///property = 实体类中的属性-->
<result column="name" property="name"></result>
<!--property = 实体类中的需要查询的属性///javaType = 另一个实体类的返回值 column = 传递的值-->
<association property="dept" javaType="Department" column="dept_id" >
<id column="id" property="did"></id>
<result column="name" property="dname"></result>
</association>
</resultMap>
<!--public List<Employee> findAll();-->
<select id="findAll" resultMap="empMap">
SELECT
e.*,
d.id did,
d.`name` dname
FROM
employee e
LEFT JOIN department d ON e.dept_id = d.id;
</select>
</mapper>
嵌套查询(推荐使用):
<?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.softeem.dao.EmployeeMapper"><!--namespace=dao接口的全限定名-->
<resultMap id="empMap" type="com.softeem.domain.Employee">
<!--id= select中resultMap的值/type=实体类的全限定名-->
<!--一对一/多对一使用association标签/\/property=Employee实体类的属性-->
<association property="dept" javaType="department" column="dept_id" select="findById"></association>
<!--javaType=下一个查询的返回值column = 传递的参数///select = 下一个查询语句的id-->
</resultMap>
<!--public List<Employee> findAll();-->
<select id="findAll" resultMap="empMap">
select * from Employee
</select>
<select id="findById" parameterType="int" resultType="department">
select * from department where id = #{demp_id}
</select>
</mapper>
二、一对多/多对多
1)嵌套结果:
<?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.softeem.dao.DepartmentMapper">
<!--public List<Department> queryAll();-->
<resultMap id="DepartmentMapper" type="com.softeem.domain.Department">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<collection property="list" ofType="Employee" column="dept_id" >
<id column="eid" property="eid"></id>
<result column="ename" property="ename"></result>
<result column="dept_id" property="dept_id"></result>
</collection>
</resultMap>
<!--public List<Employee> findAll();-->
<select id="queryAll" resultMap="DepartmentMapper">
select d.*,e.id eid,e.name ename,e.dept_id from department d LEFT JOIN employee e on d.id = e.dept_id
</select>
</mapper>
嵌套查询(推荐使用):
<?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.softeem.dao.DepartmentMapper">
<!--public List<Department> queryAll();-->
<resultMap id="DepartmentMapper" type="com.softeem.domain.Department">
<collection property="list" ofType="Employee" select="queryAllemp" column="id" >
<id column="id" property="eid"></id>
<result column="name" property="ename"></result>
<result column="dept_id" property="dept_id"></result>
</collection>
</resultMap>
<!--public List<Employee> findAll();-->
<select id="queryAll" resultMap="DepartmentMapper">
select * from department
</select>
<select id="queryAllemp" parameterType="Integer" resultType="Employee">
select * from employee where dept_id = #{id}
</select>
</mapper>
分页工具:
1.需要的jar包(选中的):
2.config.xml配置内容:
<?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>
<!-- 是用resource属性加载外部配置文件 -->
<properties resource="jdbc.properties"></properties>
<!--配置别名-->
<typeAliases>
<!--当domain中实体类多了,太麻烦-->
<!--<typeAlias type="com.softeem.domain.User" alias="user"></typeAlias>-->
<package name="com.softeem.domain" ></package>
</typeAliases>
<plugins><!--分页插件-->
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 配置使用的是mysql,底层就会选择limit分页关键字。 -->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
<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="UserMapper.xml"></mapper>-->
<package name="com/softeem/dao"></package>
</mappers>
</configuration>
3.代码:
//参数一:当前页码,从1开始 ,参数二:每页显示记录数
PageHelper.startPage(1, 2);
// page继承了ArrayList,所以同类型的,可以强转
Page<Department> all = (Page) mapper.findAll();
System.out.println(all.getPageNum()); //当前页码
System.out.println(all.getPages()); //总页数
System.out.println(all.getPageSize()); //2 每页记录数
System.out.println(all.getTotal()); //3. 总记录数*/