Realization of basic business

Time:2020-11-21
Delete commodity information based on ID

Add mybatis dependency
When the spring framework starts, mybatis will be automatically configured to help us create a sqlsessionfactory factory (so that we can use the @ mapper annotation to describe the data persistence layer)
Step 2: define the business data layer interface and business method

package com.cy.pj.goods.dao;

import org.apache.ibatis.annotations.Delete;

import org.apache.ibatis.annotations.Mapper;

@Mapper

public interface GoodsDao {

 @Delete("delete from tb_goods where id=#{id}")

 int deleteById(Integer id);

}

among :@Mapper It is an annotation defined in mybatis framework to describe the data interface (all annotations are only used for identification) to tell the spring framework that the implementation of this interface is created by mybatis and store its implementation class in the spring container
Step 3: define the test class and test the goodsdao object

@SpringBootTest

public class GoodsDaoTests { 

 @Autowired

 private GoodsDao goodsDao;

 @Test

 public void testDeleteById() {

 int rows=goodsDao.deleteById(10);

 System.out.println("rows="+rows);

 }

}

Call the persistence layer interface, use the delete method, print the execution information, and check whether it is successful

Business advanced analysis and Implementation

There are two ways to define SQL mapping in mybais framework:
1. Define SQL mapping in our XML Mapping file (suitable for more complex SQL statements, and make full use of dynamic SQL for design, such as < where >, < if >… Tags)

2. Declare directly by annotation (simple SQL statement)

When using the mapping file, create the mapper / goods directory under the Src / main / resources directory, and then add it to its directory GoodsMapper.xml Map the file and specify the following

<?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.cy.pj.goods.dao.GoodsDao"\>

 <delete id="deleteObjects"\>

 delete from tb\_goods

 where id in <!-- (1,2,3,4,5) -->

 <foreach collection="ids"

 open="("

 close=")"

 separator=","

 item="id"\>

 #{id}

 </foreach\>

 </delete\>

</mapper\>

1) The header of the XML file is from the official website of mybatis cv
2) The namespace refers to the full name of the persistence layer class
3) The ID value of the method should be consistent with the method name in the interface
Only “IDS” and “array” can be iterated during iteration
The collection point must fill in one of them, and the method type of the interface should correspond to

int deleteObjects(@param("ids")Integer...ids);

Where @ param (“IDS”) IDS corresponds to the array name. Integer… IDS is an array named IDS (three dots represent the array ~ ~ ~)
Add the following configuration to the startup class, which points to the SQL mapping file

mybatis.mapper-locations=classpath:/mapper/*/*.xml

Add a unit test method to the goodsdaotests class to test to see if the method runs incorrectly

Recommended Today

MVC and Vue

MVC and Vue This article was written on July 27, 2020 The first question is: is Vue an MVC or an MVVM framework? Wikipedia tells us: MVVM is a variant of PM, and PM is a variant of MVC. So to a certain extent, whether Vue is MVC or MVVM or not, its ideological direction […]