【MyBatis】XML版快速入门——练习题(下)

简介: 【MyBatis】XML版快速入门——练习题(下)

3.2、练习2

  • 查询所有的班级,同时
  • 查询班级对应的老师
  • 查询班级学生的总人数
  • 查询班级包含的学生详情

CourseMapper

@Mapper
public interface CourseMapper {
    public List<Course> selectAll();
}

CourseMapper.xml

<?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.czxy.mapper.CourseMapper">
    <resultMap id="courseResultMap" type="course">
        <result property = "cid" column = "c_id"></result>
        <association property="total" column="c_id" select="com.czxy.mapper.StudentMapper.countByCourseId"></association>
        <collection property="studentList" column="c_id" select="com.czxy.mapper.StudentMapper.selectAllByCourseId"></collection>
    </resultMap>
    <select id="selectAll" resultMap="courseResultMap">
        select * from tb_course
    </select>
</mapper>

StudentMapper.xml

<?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.czxy.mapper.StudentMapper">
    <select id="countByCourseId" resultType="long">
        select count(1) from tb_student s
            inner join tb_student_course sc on s.s_id = sc.s_id
            where sc.c_id = #{courseId}
    </select>
    <select id="selectAllByCourseId" resultType="student">
        select * from tb_student s
            inner join tb_student_course sc on s.s_id = sc.s_id
            where sc.c_id = #{courseId}
    </select>
</mapper>

3.3、练习3

  • 查询所有的学生,同时
  • 查询学生所属班级
  • 查询学生省市县信息
  • 查询学生所学课程

StudentMapper

@Mapper
public interface StudentMapper {
    /**
     * 查询所有
     * @return
     */
    List<Student> selectAll();
}

StudentMapper.xml

<?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.czxy.mapper.StudentMapper">
    <resultMap id="studentResultMap" type="student">
        <result property="sid" column="s_id"></result>
        <result property="cid" column="c_id"></result>
        <result property="provinceId" column="province_id"></result>
        <result property="cityId" column="city_id"></result>
        <result property="countyId" column="county_id"></result>
        <association property="classes" column="c_id" select="com.czxy.mapper.ClassesMapper.selectById"></association>
        <association property="province" column="province_id" select="com.czxy.mapper.CityMapper.selectById"></association>
        <association property="city" column="city_id" select="com.czxy.mapper.CityMapper.selectById"></association>
        <association property="county" column="county_id" select="com.czxy.mapper.CityMapper.selectById"></association>
        <collection property="courseList" column="s_id" select="com.czxy.mapper.CourseMapper.selectAllByStudentId"></collection>
    </resultMap>
    <select id="selectAll" resultMap="studentResultMap">
        select * from tb_student
    </select>
</mapper>

ClassesMapper.xml

<?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.czxy.mapper.ClassesMapper">
    <select id="selectById" resultType="classes">
        select * from tb_class where cid = #{classesId}
    </select>
</mapper>

CityMapper.xml

<?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.czxy.mapper.CityMapper">
    <select id="selectById" resultType="city">
        select * from tb_city where c_id = #{cid}
    </select>
</mapper>

CourseMapper.xml

<?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.czxy.mapper.CourseMapper">
    <select id="selectAllByStudentId" resultType="course">
        select c.* from tb_course c
        inner join tb_student_course sc on c.c_id = sc.c_id
        where sc.s_id = #{studentId}
    </select>
</mapper>

3.4、练习4

  • 查询所有课程,同时
  • 查询学生学习该课程总人数
  • 查询学生学习该课程详情

CourseMapper

@Mapper
public interface CourseMapper {
    public List<Course> selectAll();
}

CourseMapper.xml

<?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.czxy.mapper.CourseMapper">
    <resultMap id="courseResultMap" type="course">
        <result property = "cid" column = "c_id"></result>
        <association property="total" column="c_id" select="com.czxy.mapper.StudentMapper.countByCourseId"></association>
        <collection property="studentList" column="c_id" select="com.czxy.mapper.StudentMapper.selectAllByCourseId"></collection>
    </resultMap>
    <select id="selectAll" resultMap="courseResultMap">
        select * from tb_course
    </select>
</mapper>

StudentMapper.xml

<?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.czxy.mapper.StudentMapper">
    <select id="countByCourseId" resultType="long">
        select count(1) from tb_student s
            inner join tb_student_course sc on s.s_id = sc.s_id
            where sc.c_id = #{courseId}
    </select>
    <select id="selectAllByCourseId" resultType="student">
        select * from tb_student s
            inner join tb_student_course sc on s.s_id = sc.s_id
            where sc.c_id = #{courseId}
    </select>
</mapper>

3.5、练习5

  • 创建或修改表,完成老师与课程关联关系
  • 不同的老师可以教授不同的课程
  • 不同的课程可以由不同的老师教授

3.6、优化

  • 优化:2.1、2.2、2.3、2.4,该查询所有指定若干

4、总结

@Param作用:给参数命名 sql语句使用#{}或${}取出

mybatis.mapper-locations:classpath:mapper/*.xml:扫描mapper mapper接口与mapper xml

才能绑定

mybatis.type-aliases-package:扫描自定义POJO 映射不用全类名 可以直接写类名

mybatis.configuration.map-underscore-to-camel-case:开启自动驼峰命名转换

<ResultMap>

ResultMap:自定义结果集映射规则,自定义某个JavaBean的封装规则。

type:返回的类型

id:指定主键列的封装规则(主键也可以使用result来定义)

property:指定对应的javaBean属性

column:指定哪一列

<association>

association:配置一对一关联

property:指定对应的javaBean属性

column:指定哪一列

select:dao层进行查询的方法

<collection>

collection:一对多关联

property:指定对应的javaBean属性

column:指定哪一列

select:dao层进行查询的方法

select、update、delete的parameterType

传递参数的类型

<foreach>

foeach是mybatis的动态标签 一般用来批量操作

collection:传入的集合 【必选】

item:集合中元素迭代时的别名 【必选】

open:代码开始符号 一般时(和close)合用 常用in(),values()时 【可选】

separator:元素之间的分隔符 一般用,【可选】

close:代码关闭符号 ) 【可选】

相关文章
|
8月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
377 0
|
6月前
|
SQL XML Java
菜鸟之路Day35一一Mybatis之XML映射与动态SQL
本文介绍了MyBatis框架中XML映射与动态SQL的使用方法,作者通过实例详细解析了XML映射文件的配置规范,包括namespace、id和resultType的设置。文章还对比了注解与XML映射的优缺点,强调复杂SQL更适合XML方式。在动态SQL部分,重点讲解了`&lt;if&gt;`、`&lt;where&gt;`、`&lt;set&gt;`、`&lt;foreach&gt;`等标签的应用场景,如条件查询、动态更新和批量删除,并通过代码示例展示了其灵活性与实用性。最后,通过`&lt;sql&gt;`和`&lt;include&gt;`实现代码复用,优化维护效率。
472 5
|
8月前
|
XML Java 数据库连接
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作)
224 21
|
12月前
|
SQL 缓存 Java
MyBatis如何关闭一级缓存(分注解和xml两种方式)
MyBatis如何关闭一级缓存(分注解和xml两种方式)
430 5
|
12月前
|
SQL Java 数据库连接
MyBatis-Plus快速入门:从安装到第一个Demo
本文将带你从零开始,快速入门 MyBatis-Plus。我们将首先介绍如何安装和配置 MyBatis-Plus,然后通过一个简单的示例演示如何使用它进行数据操作。无论你是 MyBatis 的新手还是希望提升开发效率的老手,本文都将为你提供清晰的指导和实用的技巧。
2780 0
MyBatis-Plus快速入门:从安装到第一个Demo
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
214 1
|
SQL Java 数据库连接
MyBatis Mapper.XML 标签使用说明
MyBatis Mapper.XML 标签使用说明
157 0
|
5月前
|
Java 数据库连接 数据库
Spring boot 使用mybatis generator 自动生成代码插件
本文介绍了在Spring Boot项目中使用MyBatis Generator插件自动生成代码的详细步骤。首先创建一个新的Spring Boot项目,接着引入MyBatis Generator插件并配置`pom.xml`文件。然后删除默认的`application.properties`文件,创建`application.yml`进行相关配置,如设置Mapper路径和实体类包名。重点在于配置`generatorConfig.xml`文件,包括数据库驱动、连接信息、生成模型、映射文件及DAO的包名和位置。最后通过IDE配置运行插件生成代码,并在主类添加`@MapperScan`注解完成整合
800 1
Spring boot 使用mybatis generator 自动生成代码插件
|
8月前
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
625 0
|
10月前
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
363 2