定制开发小程序MyBatis的使用

目录


一.Sring定制开发小程序项目中关于MyBatis的配置

1.在pom.xml定制开发小程序中引入框架依赖

定制开发小程序这里手动进行添加,定制开发小程序通过在已有的spring项目中的pom.xml定制开发小程序文件中引入以下依赖

  1. <!-- 添加 MyBatis 框架 -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. <version>2.1.4</version>
  6. </dependency>
  7. <!-- 添加 MySQL 驱动 -->
  8. <dependency>
  9. <groupId>mysql</groupId>
  10. <artifactId>mysql-connector-java</artifactId>
  11. <scope>runtime</scope>
  12. </dependency>

2.定制开发小程序配置文件配置相关信息

在application.properties定制开发小程序中配置以下内容

  1. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  2. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/blog?characterEncoding=utf8&useSSL=false
  3. spring.datasource.username=root
  4. spring.datasource.password=root
  5. #配置mybatis的xml文件路径
  6. mybatis.mapper-locations=classpath:mapper/**Mapper.xml

这里关于xml定制开发小程序文件路径配置的匹配规定制开发小程序则是在当前mapper定制开发小程序下所有文件名以Mapper.xml定制开发小程序结尾的文件。

如下所示:

 3.设置mapper中的xml文件内容

 其中mapper定制开发小程序中需要配置的内容有如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="org.example.mapper.UserMapper">
  4. </mapper>

mapper里面就是对于CRUD操作的一些语句,只不过这些语句头需要遵守一些约束和规定,之后才能转化为java对象,并作为返回值返回给java中调用的方法。

4.根据以上配置实现的查询示例

查询前的配置:

(1)添加实体类

(2)添加mapper接口

(3)添加UserMapper.xml

将查询到的结果集会自动装配到配置的resultMap中

(4)server层的调用

 mapper里面的指定方法被调用后会通过之前配置的xml和对应的路径会执行xml文件中的方法

(5)的调用

 定义service层,然后在该层调用mapper中的方法

(6)前端访问后端路径

 通过postman来通过url来访问该资源下的路径

 上面就是使用mybatis来对数据库操作的整个流程,接下来就是一些有关xml中关于CRUD语句的具体操作和配置。

二.有关mybatis中xml对于数据库操作的具体使用

1.插入操作

seGeneratedKeys: MyBatis 使用 JDBC 的 getGeneratedKeys 方法来取出由数据库内部生成的主键,默认值:false。
keyColumn:设置生成键值在数据库表中的列名;如果生成列不止一个,可以用逗号分隔多个属性名称。
keyProperty:指定能够唯一识别对象的属性,MyBatis 会使用 getGeneratedKeys 的返回值或 insert 语句的 selectKey 子元素设置它的值,默认值:未设置(unset)。如果生成列不止一个,可以用逗号分隔多个属性名称。

controller层

 service层

 mapper层

通过接口插入成功的显示

 UserMapper.xml中的sql代码:

  1. <insert id="insertOne" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
  2. insert
  3. into
  4. user(username, password, nickname)
  5. values (#{username},#{password},#{nickname})
  6. </insert>

2.删除操作

基于xml文件配置好的前提下,然后在xml文件中写sql操作

 通过构造前端接口,来调用mybatis中的sql来进行验证,执行顺序如下:

3.修改操作

 

 

4.查询操作

对于查询操作来说,需要在xml中配置结果集映射,因为需要将查找中的数据映射为一个java对象。

首先在xml文件中使用resultMap,然后设置id,之后的查询语句需要根据该id来进行转化为type中的对象,其中type中为model类的位置(类路径),然后里面就是关于映射关系的指定,其中id是数据库中关于主键的映射,如果不是主键,就使用result,对于属性中的参数,如column是数据库中的字段名,而property是类的成员变量。

  1. <!-- 这里是配置有关查询到的结果集转化为哪一个-->
  2. <!-- column代表数据库中的字段-->
  3. <resultMap id="ResultBaseMap" type="org.example.model.User">
  4. <id column="id" property="id"></id>
  5. <result column="username" property="username"></result>
  6. <result column="password" property="password"></result>
  7. <result column="nickname" property="nickname"></result>
  8. <result column="github" property="github"></result>
  9. <result column="head" property="head"></result>
  10. </resultMap>

 三.Mybatis进阶操作

1.参数占位符

#{}是预编译占位符,在处理是,会将该位置的数据先转化为?,然后在执行PreparedStament中的set方法时,将?替换变量值。如果是字符串,会添加""。

${}是直接进行替换为变量的值,存在sql注入问题。

假如传递的数据是sort ,对于#{},最终会处理成"sort",而${}就是sort。

2.like查询

在使用like的时候,一般是模糊查询,在mybatis中需要使用concat函数来将%与#{变量值}来进行拼接。

concat(str1, str2, str3...)是将str1和str2和str3等全部进行拼接起来。

  1. <!-- like查询-->
  2. <select id="selectLike" resultMap="ResultBaseMap">
  3. select
  4. id,
  5. username,
  6. password
  7. from
  8. user
  9. where password like concat('%',#{password},'%');
  10. </select>

 

3.

(1)一对一查询

对于多表查询时,都需要使用resultMap来进行配置,在配置前还需要给对象中添加新的属性。例如一个用户登录表和用户信息表之间的关系是一对一,在进行关联查询的时候首先需要给User类中添加一个Message类型的成员变量,然后再在resultMap中除了配置有关User信息的属性外,还需要添加一个<association property=""  resultMap="" columnPrefix="">属性.

property是在User中新添加的成员message,  resultMap是需要关联的结果集的映射,而columnPrefix是对于查询的结果字段添加前缀,如果两个表中右重复字段,可以通过该字段进行区分,之后在associate中配置的resultMap中会去除掉前缀进行匹配。

  1. <!-- 一对一关联自己的message信息-->
  2. <resultMap id="ResultBaseMap2" type="org.example.model.User">
  3. <id column="id" property="id"></id>
  4. <result column="username" property="username"></result>
  5. <result column="password" property="password"></result>
  6. <result column="nickname" property="nickname"></result>
  7. <result column="github" property="github"></result>
  8. <result column="head" property="head"></result>
  9. <association property="message" resultMap="org.example.mapper.UMMapper.BaseResultMap"
  10. columnPrefix="m_"></association>
  11. </resultMap>
  12. <!-- 一对一关联查询-->
  13. <select id="onebyone" resultMap="ResultBaseMap2">
  14. select
  15. u.id,
  16. u.username,
  17. m.id_card m_id_card,
  18. m.id m_id,
  19. m.user_id m_user_id
  20. from user u left join message m on u.id=m.user_id;
  21. </select>

 查询和配置关系

 

(2)一对多查询 

对于一个用户表和文章表的关系是一对多,一个用户可以有多篇文章,Mybatis中也是通过resultMap来对查询结果集进行配置,然后再对查询结果中映射的对象中添加一个新的集合属性(因为是一对多关系),和一对一不同的是resultMap中设置的是collection属性,其他的配置规则基本不变。

  1. <!-- 这里是配置有关查询到的结果集转化为哪一个-->
  2. <!-- column代表数据库中的字段-->
  3. <resultMap id="ResultBaseMap" type="org.example.model.User">
  4. <id column="id" property="id"></id>
  5. <result column="username" property="username"></result>
  6. <result column="password" property="password"></result>
  7. <result column="nickname" property="nickname"></result>
  8. <result column="github" property="github"></result>
  9. <result column="head" property="head"></result>
  10. <collection property="articles" resultMap="org.example.mapper.ArticleMapper.ResultBaseMap"
  11. columnPrefix="a_"></collection>
  12. </resultMap>
  13. <!--关联查询,一个用户对应多篇文章-->
  14. <select id="selectUserAndArticle" resultMap="ResultBaseMap">
  15. select
  16. u.id,
  17. u.username,
  18. u.password,
  19. u.nickname,
  20. u.github,
  21. u.head,
  22. a.id a_id,
  23. a.title a_title,
  24. a.date a_date,
  25. a.content a_content
  26. from user u join article a on u.id=a.user_id
  27. </select>

 

 

4.动态查询(if,trim,where,set,foreach的使用)

(1)if

标签为:<if test="属性值不为空"> </if>

如果对于一个参数是否传递是不确定的(可能为空或不为空),我们可以使用if来进行判断,如果传了就在sql中显示,没有传就不显示。

如注册用户时,有些信息可以不用传递,如头像等,这时候就可以在mybatis中使用if来进行过滤该参数。

  1. <insert id="insertSelective">
  2. insert into user(
  3. username,
  4. nickname,
  5. password
  6. <if test="head!=null"> ,head </if>
  7. <if test="github!=null"> ,github </if>
  8. )
  9. values (
  10. #{username},
  11. #{nickname},
  12. #{password}
  13. <if test="head!=null"> ,#{head} </if>
  14. <if test="github!=null"> ,#{github} </if>
  15. )
  16. </insert>

 

(2)trim

首先trim标签中有多个参数,分别为:

prefix=“”  表示整个语句块以prefix中的内容作为前缀

suffix=“”   表示整个语句块以suffix中的内容作为后缀

prefixOverrides=“”  表示整个语句块要去掉的前缀

suffixOverrides=“”  表示整个语句块要去掉的后缀

以之前的选择插入语句为例,来使用上面的参数:

  1. <insert id="insertParam">
  2. insert into user
  3. <trim prefix="(" suffix=")" suffixOverrides=",">
  4. <if test="username!=null">
  5. username,
  6. </if>
  7. <if test="nickname!=null">
  8. nickname,
  9. </if>
  10. <if test="password!=null">
  11. password,
  12. </if>
  13. <if test="github!=null">github</if>
  14. </trim>
  15. <trim prefix="values(" suffix=")" suffixOverrides=",">
  16. <if test="username!=null">
  17. #{username},
  18. </if>
  19. <if test="nickname">
  20. #{nickname},
  21. </if>
  22. <if test="password!=null">
  23. #{password},
  24. </if>
  25. <if test="github!=null">
  26. #{github},
  27. </if>
  28. </trim>
  29. </insert>

(3)where

<where></where>是用在条件查询中(也可以使用上面的trim),对于where中的<if></if>里面的内容,写成and 属性名=属性值,之后会自动去掉第一个and。

如下所示,如果传递的password和username不为空,就根据这两个条件进行过滤:

  1. <select id="selectByWhere" resultMap="BaseResultMap">
  2. select * from user
  3. <where>
  4. <if test="password!=null">
  5. and password=#{password}
  6. </if>
  7. <if test="username!=null">
  8. and username=#{username}
  9. </if>
  10. </where>
  11. </select>

(4)set

用于sql修改中,会自动去除掉set中的if属性中的.

  1. <update id="updateSet">
  2. update user
  3. <set>
  4. <if test="username!=null">
  5. username=#{username},
  6. </if>
  7. <if test="nickname!=null">
  8. nickname=#{nickname},
  9. </if>
  10. <if test="password!=null">
  11. password=#{password},
  12. </if>
  13. </set>
  14. where id=#{id}
  15. </update>

(5)foreach

可以对于传入的集合进行遍历,里面的参数如下所示:

collection:用于绑定方法参数中的集合

item:遍历时的每一个对象

open:语句块开头的字符串

close:语句块结束的字符串

separator:每次遍历之间的分隔符

示例,根据list中的id号来进行批量删除:

  1. <delete id="deleteByIds">
  2. delete from user where id in <foreach collection="list" open="(" close=")" separator="," item="item">
  3. #{item}
  4. </foreach>
  5. </delete>

 

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发