隐藏页面特效

MyBatis

1|0MyBatis


  • 什么是MyBatis:是一款优秀的持久层框架,用于简化JDBC开发

1|1持久层


  • 负责将数据到保存到数据库的那一层代码
  • JavaEE三层架构:表现层,业务层,持久层

1|2框架


  • 框架就是一个半成品软件,是一套可重用,通用的,软件基础代码模型
  • 在框架的基础之上构建软件编写更加高效,规范,通用,可扩展

1|3初次使用Mybatis


  • 步骤1.创建user表,添加数据

​ 略

  • 步骤2:创建模块,导入坐标,

​ 配置pom文件,添加依赖

  • 步骤三:编写MyBits核心配置文件-->替换连接信息,解决硬编码问题

MyBits-config中的配置:

<?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> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <!-- 数据库连接信息--> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql:///db3?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- 加载sql的映射文件 编写了sql映射文件以后写入地址--> <mapper resource="UserMapper.xml"/> </mappers> </configuration>

这里需要根据sql的映射文件来加入

  • 步骤四:编写sql映射文件-->统一管理sql语句,解决硬编码问题
<?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:名称空间 id:在使用的时候的唯一标签 resultType:返回的结果集对象 --> <mapper namespace="test"> <select id="selectAll" resultType="com.qiu.pojo.User"> select * from tb_user; </select> </mapper>
  • 步骤五:编码
//1.加载mybatis的核心配置文件,获取sqlsessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlSession对象 并用它执行sql语句 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行sql语句 List<User> users = sqlSession.selectList("test.selectAll"); System.out.println(users); // 释放资源 sqlSession.close();

至此完成一个Demo项目 测试结果如下所示:

image

1|4解决sql映射文件的警告提示


  • 产生原因:idea和数据库没有建立连接,不识别表信息
  • 解决方式:在idea中配置Mysql数据库连接image

image

1|5用Mapper代理开发


    1. 定义一个与sql映射文件同名的Mapper接口,并且将Mapper接口和Sql映射文件放着在同一目录下

image

  1. 设置SQL映射文件的namespace属性为Mapper接口全限定名
<mapper namespace="com.qiu.Mapper.UserMapper"> <select id="selectAll" resultType="com.qiu.pojo.User"> select * from tb_user; </select> </mapper>
  1. 在Mapper接口中定义方法,方法名就是SQL映射文件中sql语句的id,并保持参数类型和返回值类型一致
public interface UserMapper { List<User> selectAll(); } //因为这里是查询所有数据返回的是多个user对象 这里用list接受

同时记得修改MyBatis-config里面的Mapper路径:

<!-- 加载sql的映射文件--> <!-- <mapper resource="com/qiu/Mapper/UserMapper.xml"/> --> <!-- mapper代理的方式--> <package name="com.qiu.Mapper"/> </mappers> </configuration>

编码:

//3.1 获取对应的UserMapper的代理对象 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectAll();

测试结果如下image

1|6MyBatisX插件


image

可以在Mapper配置文件和对应接口之间来回跳动

1|7案例


  • 用MaBatis核心配置文件完成增删改查
  • 首先完成brand实体类,以及brandmapper和对应的配置文件
<mapper namespace="com.qiu.Mapper.BrandMapper"> <resultMap id="brandResultMap" type="brand"> <!-- id:唯一标识 type 映射类型--> <result column="brand_name" property="brandName"/> <result column="company_name" property="companyName"/> <!-- id:完成主健字段的映射 result 完成其他字段的映射--> </resultMap> <!--数据库表的字段名称和实体类的属性名称不一样,则不能自动封装--> <select id="selectAll" resultMap="brandResultMap"> select * from tb_brand ; </select> </mapper>

这里需要注意 因为数据库的字段名和brand实体类中的变量名不同 不能自动封装 ,需要使用 resultMap标签来进行映射 具体看上面的代码

  • 编写测试类文件:
//MyBatis运用mapper代理 查找所有数据 public void selectAll() throws IOException { //1.加载mybatis的核心配置文件,获取sqlsessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //2.获取sqlSession对象 并用它执行sql语句 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行sql语句 //3.1 获取对应的BrandMapper的代理对象 BrandMapper mapper = sqlSession.getMapper(BrandMapper.class); List<Brand> brands = mapper.selectAll(); System.out.println(brands); sqlSession.close(); } }

测试结果如下所示:

image

1|8参数占位符和参数类型


  • 参数占位符:

    1. 2|0{}:会将其调换为?,为了防止sql注入


    2. ${}:拼sql,会存在SQL注入的问题
    3. 使用时机:参数传递的时候用1,表名或列名不固定的时候用2
  • 特殊字符类型的处理

    1. 转义字符:例如: <可以写为 &lt 使用与特殊字符比较少的时候
    2. CDATA区:
    <![CDATA[ < ]]>

2|1sql语句设置多个参数的三个方法:


  • 散装函数:需要@Param(”sql中的参数占位符名称“)
List<Brand> selectByCondition(@Param("status") int status, @Param("brandName") String brandName, @Param("companyName")String companyName); //参数设置 int status=0; String brandName="只"; String companyName="只"; brandName="%"+brandName+"%"; companyName="%"+companyName+"%"; //执行语句 List<Brand> brands = mapper.selectByCondition(status, brandName, companyName);
  • 实体类封装参数:只需保证sql中的参数名和实体类属性名对应上,即可设置成功
ist<Brand> selectByCondition(Brand brand); //参数设置 int status=0; String brandName="只"; String companyName="只"; brandName="%"+brandName+"%"; companyName="%"+companyName+"%"; Brand brand=new Brand(); brand.setStatus(status); brand.setBrandName(brandName); brand.setCompanyName(companyName); //执行语句 List<Brand> brands = mapper.selectByCondition(brand);
  • map集合 :只需保证sql中的参数名和map集合的键的名称对应上,即可设置成功
List<Brand> selectByCondition(Map map); //参数设置 Map map =new HashMap(); map.put("status",status); map.put("brandName",brandName); map.put("companyName",companyName); //执行语句 List<Brand> brands = mapper.selectByCondition(map);

2|2动态sql


if标签:

  • test值为判断语句
# 动态sql <where> <if test="status!=null"> status=#{status} </if> <if test="brandName!=null and brandName!=''"> and brand_name like #{brandName} </if> <if test="companyName!=null and companyName!=''"> and company_name like #{companyName}; </if> </where>

choose when标签

  • 相当于 swhich和case
<choose> <!-- 相当于switch--> <when test="status!=null"> # 相当于case status=#{status}; </when> <when test="brandName!=null"> brand_name like #{brandName}; </when> <when test="companyName!=null"> company_name like #{companyName}; </when> <otherwise> 1=1; </otherwise> </choose>

foreach标签

  • 相当于增强for
<foreach collection="ids" item="id" separator="," open="(" close=")"> #{id} </foreach>

__EOF__

本文作者小邱
本文链接https://www.cnblogs.com/xiaoqiuStu/p/16125521.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   小邱ooo  阅读(70)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示