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 use the Mybatis reverse Engineering plug-in for idea

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

Share

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

Idea how to use Mybatis reverse engineering plug-in, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Use mybatis to connect to the database

Add the information of the connected mysql, and test the link successfully.

Install the Better-Mybatis-Generator plug-in

After the installation is successful, right-click on the table that needs to be generated and select mybatis-generator.

Add some configurations to generate.

Click OK, the first generation will pop up window, you need to enter the database account password. You can see the mapper interface, entity class, and sql that generated the table.

Third, about the example class detailed explanation 1, example member variable

Mybatis-generator generates a Criterion for each field and creates a dynamic sql for the underlying mapper.xml. If the table has a large number of fields, the resulting example class will be very large. In theory, any filter you can think of can be constructed through the example class.

/ function: ascending or descending order / / Parameter format: field + space + asc (desc) protected String orderByClause; / / function: remove duplicate / / true is to choose not to repeat records, false, and vice versa a collection of protected boolean distinct; / / custom query conditions / / Criteria. The object in the collection is the starting subscript private Long offset of the display number of protected List oredCriteria; / / pages connected by or. / / the inner class Criteria contains a collection of Cretiron. / / between the Cretiron contained in each Criteria object is public static class Criteria extends GeneratedCriteria {protected Criteria () {super ();}} / is the code model in reverse engineering in mybatis protected abstract static class GeneratedCriteria {.} / / is the most basic and lowest-level Where condition for field-level filtering public static class Criterion {.} 2, example use

Test in the MybatisDemoApplicationTests class:

Package org.ywz.test; import org.junit.jupiter.api.Test;import org.junit.platform.commons.util.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.ywz.dao.StudentDao;import org.ywz.pojo.Student;import org.ywz.pojo.StudentExample; import java.util.List; / * Example class instructions * / @ SpringBootTestclass MybatisDemoApplicationTests {@ Autowired private StudentDao studentDao @ Test void contextLoads () {StudentExample studentExample = new StudentExample (); / / the total number of query data is similar to: select count (*) from student long l = studentDao.countByExample (studentExample); System.out.println ("- total number -") System.out.println ("Total number of database entries:" + l); System.out.println ("- and condition -"); / / where conditional query or multi-conditional query Student student = new Student (); student.setName ("Wang Wu"); student.setSex ("male") SelectAndCondition (student); System.out.println ("- or condition -"); selectOrCondition (student); System.out.println ("- fuzzy query -"); student.setName ("King") SelectLikeCondition (student); System.out.println ("- paging query -"); selectLimit ();} / * where conditional query or multiple conditional query * similar to: select * from student where name= {# student.name} and sex= {# student.sex} order by score asc * * @ param student * / private void selectAndCondition (Student student) {StudentExample studentExample = new StudentExample (); StudentExample.Criteria criteria = studentExample.createCriteria (); studentExample.setOrderByClause ("score asc"); / / Ascending studentExample.setDistinct (false); / / No if (StringUtils.isNotBlank (student.getName () {criteria.andNameEqualTo (student.getName ()) } if (StringUtils.isNotBlank (student.getSex () {criteria.andSexEqualTo (student.getSex ());} List students = studentDao.selectByExample (studentExample); students.forEach (System.out::println);} / * similar to: select * from student where name= {# student.name} or sex= {# student.sex} * * @ param student * / private void selectOrCondition (Student student) {StudentExample studentExample = new StudentExample (); StudentExample.Criteria criteria1 = studentExample.createCriteria (); StudentExample.Criteria criteria2 = studentExample.createCriteria (); if (StringUtils.isNotBlank (student.getName () {criteria1.andNameEqualTo (student.getName ()) } if (StringUtils.isNotBlank (student.getSex () {criteria2.andSexEqualTo (student.getSex ());} studentExample.or (criteria2); List students = studentDao.selectByExample (studentExample); students.forEach (System.out::println) } / * is similar to: select * from student where name like% {# student.name}% * * @ param student * / private void selectLikeCondition (Student student) {StudentExample studentExample = new StudentExample (); StudentExample.Criteria criteria = studentExample.createCriteria (); if (StringUtils.isNotBlank (student.getName () {criteria.andNameLike ("%" + student.getName () + "%") } List students = studentDao.selectByExample (studentExample); students.forEach (System.out::println);} / * is similar to: select * from student limit offset,limit * / public void selectLimit () {StudentExample studentExample = new StudentExample (); studentExample.setOffset (21); studentExample.setLimit (5); List students = studentDao.selectByExample (studentExample) Students.forEach (System.out::println);}}

Running result:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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