SpringBoot + Mybatis/JPA

这篇主要讲解SpringBoot结合JPA和Mybatis的使用
SpringBoot作为后端框架,必定要对数据库进行crud操作,JPA和Mybatis把这些操作进行了封装,方便了代码的编写。

JPA

先从比较简单的JPA讲起
第一步,添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>

一般只要添加前两个,mysql-connector-java无论是jpa还是mybatis还是传统的jdbc都需要添加。
第二步,编写dao类
准确的说应该是一个接口,继承JpaRepository<>的接口

public interface OrderDetailRepository extends JpaRepository<OrderDetail, String> {
    List<OrderDetail> findByOrderId(String orderId);
}

OrderDetail是数据表对应的实体类,String是主键类型
JpaRepository提供了一系列的操作:

  1. save()更新和添加
  2. findAll()
  3. findById(id)通过id进行查找,返回的是一个Optional对象
    还可以在接口中自定义的方法,但是有一定的命名规则,这些命名规则对应的是sql语句中的语法。

Mybatis

Mybatis有两种使用方式:

  1. 注解
  2. xml文件

无论使用哪种方式第一步就是先添加依赖

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

先说注解,现在官方推行使用注解的方式

annotation

第一步,建一个mapper包,在里面新建一个接口
第二步,编写方法,在方法上面添加注解
具体的直接看下面的程序

package com.hwy.sell.dataobject.mapper;

import com.hwy.sell.dataobject.ProductCategory;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface ProductCategoryMapper {

    @Insert("insert into product_category(category_name, category_type) values (#{category_name, jdbcType=VARCHAR}, #{category_type, jdbcType=INTEGER})")
    int insertByMap(Map<String, Object> map);

    @Insert("insert into product_category(category_name, category_type) values (#{categoryName, jdbcType=VARCHAR}, #{categoryType, jdbcType=INTEGER})")
    int insertByObject(ProductCategory productCategory);

    @Select("select * from product_category where category_type= #{categoryType}")
    @Results({
            @Result(column = "category_type", property = "categoryType"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "category_id", property = "categoryId")
    })
    ProductCategory findByCategoryType(Integer categoryType);

    @Select("select * from product_category where category_name= #{categoryName}")
    @Results({
            @Result(column = "category_type", property = "categoryType"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "category_id", property = "categoryId")
    })
    List<ProductCategory> findByCategoryName(String categoryName);

    @Update("update product_category set category_name = #{categoryName} where category_type = #{categoryType}")
    int updateByCategoryType(@Param("category_name") String categoryName, @Param("category_type") Integer categoryType);

    @Update("update product_category set category_name = #{categoryName} where category_type = #{categoryType}")
    int updateByObject(ProductCategory productCategory);

    @Delete("delete from product_category where category_type = #{categoryType}")
    int deleteByCategoryType(Integer categoryType);

    ProductCategory selectByCategoryType(Integer categoryType);
}

增@Insert 查@Select 删@Delete 改@Update
在括号中编写sql语句,变量用#{}包起来,如果使用实体类作为方法的参数时,sql语句中的变量名要和实体类中的属性名对应起来
当方法的参数列表有多个变量时候要在参数前使用注解@Param("")
要让这个接口起作用,还要在SpringBoot的启动类那里添加一个@MapperScan(basePackages=""),basePackages中填写的是接口所在的包

xml

进行简单了解
第一步就是新建一个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.hwy.sell.dataobject.mapper.ProductCategoryMapper">
    <resultMap id="BaseResultMap" type="com.hwy.sell.dataobject.ProductCategory">
        <id column="category_id" property="categoryId" jdbcType="INTEGER"/>
        <id column="category_type" property="categoryType" jdbcType="INTEGER"/>
        <id column="category_name" property="categoryName" jdbcType="VARCHAR"/>
    </resultMap>
    
    <select id="selectByCategoryType" resultMap="BaseResultMap" parameterType="java.lang.Integer">
        select category_id, category_name, category_type
        from product_category
        where category_type = #{categoryType, jdbcType=INTEGER}
    </select>
</mapper>

这里就不做详细讲解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值