Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build boot+MybatisPlus

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to build boot+MybatisPlus, the article is very detailed, has a certain reference value, interested friends must read it!

1. Preparation 1.1 create database tables

Create a tabl

CREATE TABLE `login` (`id` INT (4) primary key auto_increment, `login_ id` VARCHAR (50) UNIQUE, `city` VARCHAR (50) DEFAULT 'Fuping', `password` VARCHAR (50))

Add data to the visualization tool (I'm not very good at writing sql)

1.2 create boot project 1.3 create entity classes (map database tables) 2. Use mybatisPlus (operating database) 2.1 to add mybatisPlus dependencies com.baomidou mybatis-plus-boot-starter 3.1.2 mysql mysql-connector-java2.2 configuration database information spring: datasource: url: jdbc:mysql://localhost:3306/test0314?characterEncoding=utf-8&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver2.3 create mapper interface

Common crud methods are provided in this interface. We only need to obtain mapper operation data from the container.

Package com.hand.demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.hand.demo.entity.User;/** * user data access layer Interface * * / public interface UserMapper extends BaseMapper {} 2.4 configure mapper scanning

Configure which package our mapper is in in the startup class

Two methods: @ Mapper annotation (trouble) and @ MapperScan (configure on the main startup class)

@ SpringBootApplication@MapperScan ("com.hand.demo.mapper") public class Demo0318Application {public static void main (String [] args) {SpringApplication.run (Demo0318Application.class, args);}} 2.5 test junit junit test

Under the test package

Package com.hand.demo;import com.hand.demo.entity.User;import com.hand.demo.mapper.UserMapper;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTestclass Demo0318ApplicationTests {@ Autowired private UserMapper userMapper / * get the UserMapper implementation class object (the mybatisPlus container uses a dynamic proxy to generate the implementation class object for this interface and inject it into the spring container * so we just need to define a member variable here and automatically inject it through annotations) * * / @ Test public void testQueryAll () {List userList = userMapper.selectList (null); System.out.println (userList) }} 3. Common Settings 3.1 Settings Table Mapping rules

Set table prefix configuration

3.2 Primary key generation strategy (default is based on snowflake algorithm) @ TableId (type = IdType.AUTO) private Long id 3.3 Global settings mybatis-plus: global-config: db-config: table-prefix: id-type: auto3.4 field and hump mapping of column names (on by default) mybatis-plus: global-config: db-config: table-prefix: id-type: auto configuration: map-underscore-to-camel-case: false3.5 log settings mybatis-plus: global-config: db-config : table-prefix: id-type: auto configuration: map-underscore-to-camel-case: false log-impl: org.apache.ibatis.logging.stdout.StdOutImpl4. Basic exercise 4.1 insert insert () 4.2 delete deleteXxx () map4.3 update updateXxx () 5.Wrapper (conditional constructor) 5.1 Wrapper AbstractWrapper QueryWrapper UpdateWrapper

The select of QueryWrapper can set the columns to be queried.

6. Service layer usage

There is no need to manually inject the mapper in the generic type

If other mapper is needed, manual injection is fine.

Package com.hand.demo.service;import com.baomidou.mybatisplus.extension.service.IService;import com.hand.demo.entity.User;public interface UserService extends IService {} package com.hand.demo.service.Impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;import com.hand.demo.entity.User;import com.hand.demo.mapper.UserMapper;import com.hand.demo.service.UserService;@Servicepublic class UserServiceImpl extends ServiceImpl implements UserService {} @ Autowired private UserService userService Test public void testService () {List list = userService.list (); System.out.println (list);}

Also has its own batch operation (batch)

Custom method (multi-table association)

7. Code generator (to be continued)

Each interface inherits the same BaseMapper,IService (code redundancy, tedious)

The code generator provided by MybatisPlus, which generates all the code of the three layers of mvc with one click

How to use it, introduce the following package

Com.baomidou mybatis-plus-generator 3.5.2 org.freemarker freemarker and above are all the contents of this article "how to build boot+MybatisPlus". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report