JavaWeb个人笔记01--Maven到MyBatis

Maven 基本使用

常用指令
  • compile 编译

  • clean 清理

  • test 测试

  • package 打包

  • install 安装

依赖管理

1.使用坐标导入jar包
  1. 在pom.xml 中编写标签

  2. 在该标签中使用 引入坐标

  3. 定义坐标的 groupId,artifactId,version

  4. 点击刷新按钮,使得坐标生效

xml 复制代码
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
<!-- 添加依赖范围 -->
        <scope>test</scope>
    </dependency>
<dependencies>
2.使用坐标导入jar包快捷方式
  1. 在pom.xml中 按alt + insert 选择 Dependency

  2. 在弹出的面板中搜索对应坐标(如junit) 然后双击选中对应坐标

  3. 点击刷新按钮,使坐标生效

依赖范围(默认compile,了解)

MyBatis

简化JDBC开发的优秀 持久层框架

Mapper 代理开发

1.入门案例(黑马程序员JavaWeb(50/163))

  1. 定义与SQL映射文件同名的Mapper接口,并且将Mapper接口和SQL映射文件放置在同一目录下:
    注意:resource目录下创建分层的文件夹用的是 "/" 不是 "."
  1. 设置SQL映射文件(如UserMapper.xml)的namespace属性为Mapper接口全限定名
xml 复制代码
<mapper namespace="com.yanxi.mapper.UserMapper">
  1. 在Mapper接口种定义方法,方法名就是SQL映射文件种SQL语句的id,并保持参数类型和返回值类型一致
java 复制代码
//接口
import com.yanxi.pojo.User;

import java.util.List;

public interface UserMapper {

//在Mapper接口种定义方法,方法名就是SQL映射文件种SQL语句的id,并保持参数类型和返回值类型一致
  //  User selectALL();// 一个用
    //如果范围的是集合要写List<User>
    List<User> selectAll(); 注意大小写
}
xml 复制代码
<!--UserMapper.xml 文件-->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yanxi.mapper.UserMapper">
    <select id="selectAll" resultType="com.yanxi.pojo.User">
        select * from tb_user;
    </select>
</mapper>
  1. 编码

  2. 通过SqlSession的getMapper方法获取 Mapper 接口的代理对象

  3. 调用对应方法完成sql的执行

细节:如果Mapper接口名称和sql映射文件名称相同,并在同一目录下,则可以使用包扫描的方式简化SQL映射文件的加载

xml 复制代码
        <!-- 加载sql映射文件 -->
        <!--原来的写法-->
        <mapper resource="com/yanxi/mapper/UserMapper.xml"/>
        <!--可以写成-->
        <package name="com.yanxi.mapper">

MyBatis 核心配置文件

environment

配置数据库的连接环境信息,可以配置多个environment,通过default属性切换不同的environment,如下

typeAliases
xml 复制代码
    <!-- 起别名 不区分大小写?
    包扫描 扫描com.yanxi.pojo 相当于给pojo实体类取了别名,默认User 不区分大小写
    在UserMapper.xml <select 那一栏多 resultType="user"  //不区分大小写
    -->
    <typeAliases>
        <package name="com.yanxi.pojo"/>
    </typeAliases>

    <select id="selectAll" resultType="user"> <!--不区分大小写
    修改前是 com.yanxi.pojo.User
    -->
注意事项

配置文件需要注意顺序

MyBatis 实战增删改查

查询
  1. 创建Brand.java 和 BrandMapper.xml

  2. 在Test文件中创建MyBatisTest

java 复制代码
public class MyBatisTest {
    @Test
    public void testSelectAll() throws IOException {
        //1.加载mybatis的核心配置文件,获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //3.获取Mapper 接口的代理对象
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //4.执行方法(核心记住的方法)
        List<Brand> brands = brandMapper.selectAll();
        System.out.println(brands);
        //5.释放资源
        sqlSession.close();
    }
}
resultMap

解决 数据库表中字段名称 与 实体类的属性名称对不上问题

如company_name 与 companyName(有一种方式是起别名)

xml 复制代码
<!--处理数据库表的属性名称和实体类名称不一致的问题
resultMap --
    1.定义<resultMap>标签
    2.在<select>标签中,使用resultMap属性替换 resultType 属性
id : 完成主键字段的映射
column: 表的列名
property: 实体类的属性名


-->
    <resultMap id="brandResult" type="brand">
        <result column="brand_name" property="brandName"/>
        <result column="company_name" property="companyName" />

    </resultMap>
   <select id="selectAll" resultType="brand">
        select *
        from tb_brand;
    </select>
查看详情
参数占位符

#{}: 会将其替换为 ? 防止sql注入 大部分情况使用

${}: 拼sql,存在sql注入问题 表名或列名不固定的时候用

xml 复制代码
    <select id="selectById" parameterType="int" resultMap="brandResult">
        select *
        from tb_brand where id = #{id};
    </select><!--parameterType="int" 可以省略-->
特殊字符的处理
  1. 转移字符 < 用&lt 表示 (<会被识别成 标签)

  2. CDATA区(输入 CD 会有提示)

xml 复制代码
    <select id="selectById" parameterType="int" resultMap="brandResult">
        select *
        from tb_brand where id
        <![CDATA[
          <
        ]]>
        #{id};
    </select>
条件查询
动态查询

和 标签搭配

xml 复制代码
    <select id="selectByCondition" resultMap="brandResult">
        select *
        from tb_brand
        <where>

    <!-- test="" 双引号里面加条件 -->
        <if test="status != null">
            and status = #{status}
        </if>
        <if test="companyName != null and companyName != '' ">
            and company_name like #{companyName}
        </if>
        <if test="brandName != null and brandName != '' ">
            and brand_name like #{brandName}
        </if>
        </where>
    </select>
单条件动态查询

多个条件中选择一个。

choose(when,otherwise) : 类似于swtich(case,default)

  • otherwise 可以用 代替?
xml 复制代码
    <select id="selectByConditionSingle" resultMap="brandResult">
        select *
        from tb_brand
        where
        <choose>
            <when test="status != null">
                status = #{status}
            </when>
            <when test="companyName != null and companyName !='' ">
                company_name like #{companyName}
            </when>
            <when test="brandName != null and brandName !='' ">
                brand_name like #{brandName}
            </when>
            <!--如果那个条件都不满足-->
            <otherwise>
                1=1
            </otherwise>
        </choose>
    </select>
添加
主键返回
xml 复制代码
    <insert id="add" useGeneratedKeys="true" keyProperty="id">
        insert into tb_brand (brand_name,company_name,ordered,description,status)
        values (#{brandName},#{companyName},#{ordered},#{description},#{status});
    </insert>
修改
全部修改
xml 复制代码
    <update id="update">
        update tb_brand
        set brand_name = #{brandName},
            company_name = #{companyName},
             ordered = #{ordered},
            description = #{description},
            status = #{status}
        where id = #{id};
    </update>
修改动态字段
删除
批量删除
xml 复制代码
   <!--    mybatis会将数组参数,封装为一个Map集合,-->
<!--    * 默认: array = 数组;-->
<!--    * 使用@Param 主键盖面map集合的默认key名称(BrandMapper.java文件中)-->
     <delete id="deleteByIds">
        delete from tb_brand where id
        in (
            <foreach collection="ids" item="id" separator=",">
                #{id}
            </foreach>
            );
    </delete>
   <!--等于-->
        <delete id="deleteByIds">
        delete from tb_brand where id
        in (
            <foreach collection="ids" item="id" separator="," >
                #{id}
            </foreach>
            );
    </delete>

注解完成增删改查

相关推荐
lll23526917791 小时前
线下多人发言对比与录音操作
笔记·智能手机
叼烟扛炮2 小时前
Linux 学习笔记(三十三):线程互斥——互斥锁(Mutex)与死锁
linux·运维·服务器·笔记·学习·1024程序员节
深圳老胡4 小时前
STM32C5A3ZG 网络调试总结:从启动失败到连续 Ping 稳定
笔记·stm32·单片机·代码规范
职豚求职小程序刷题5 小时前
宁德时代社招高岗晋升测评笔试改版了
笔记
CarIise5 小时前
JDBC 与数据库事务核心
数据库·笔记
鬼手点金5 小时前
KV Cache 未必是一次性的:可编辑、可组合的“笔记”
java·人工智能·笔记·学习·自动化·1024程序员节
摇滚侠5 小时前
《On Java 中文版 基础卷》阅读笔记 初始化和清理 08
java·开发语言·笔记
dire_7776 小时前
TH26027A开尔文夹子线缆问题导致同惠TH2822电桥测量不准
笔记
宝绿特塑业再生科技7 小时前
板材收缩率不稳,户外家具装配尺寸为何对不上:从切割尺寸变化到接缝不一的系统性排查与对策
笔记·科技·学习·生活·户外家具·hdpe再生颗粒·hdpe
~城南花已开~7 小时前
Intel E810 SR-IOV VF Spoofcheck 防伪造测试-KVM3
服务器·网络·笔记·学习·ubuntu·etl工程师