In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use MyBatis-Ext for you. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Recently, I came into contact with a MyBatis extension toolkit MyBatis-Ext in my work, which can be said to greatly reduce the workload when using mybatis.
MyBatis-Ext is an enhanced extension of MyBatis, very similar to our commonly used Mybatis-plus, simplifying the operation of MyBatis to add, delete, modify and query a single table, provide general addition, deletion, modification and query, support functional programming, support paging query, support user-defined general methods, and prevent SQL injection. The integration is also very simple, with only enhancements and no modifications to MyBatis.
Take the spring-boot project as an example, the integration is very simple. Pom imports core dependencies:
Tech.wetech.mybatis mybatis-ext-core 1.5.2 tech.wetech.mybatis mybatis-ext-spring-boot-starter 1.5.2
It is important to note that there is no need to introduce mybatis-spring-boot-starter after the introduction of mybatis-ext-spring-boot-starter.
As always, configure the data source in application.yml:
Spring: datasource: username: dater password: 123456 url: jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 8 min-idle: 1 max-active: 20mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.mybatis.ext.test.mybatisexttest .entityspring: datasource: username: dater password: 123456 url: jdbc:mysql://127.0.0.1:3306/datacenter?useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource druid: initial-size: 8 min-idle: 1 max-active: 20mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.mybatis.ext.test.mybatisexttest.entity
Create a mapped entity class:
@ Data@Table (name= "user") public class User {@ Id String identifycard; @ Column (name= "name") String name; String money; String card; String phone; String rate;}
Mybatis-ext uses Jpa annotations and currently implements @ Table, @ Id, @ Column, @ Transient, and @ Version. @ Table and @ Id are required annotations, while others are not required. Use @ Table to specify the datasheet name, and @ Id to specify the datasheet primary key.
The Mapper API of the query inherits the BaseMapper API, and entity classes are entered in generics:
Public interface UserMapper extends BaseMapper {}
Let's take a look at the methods that can be called directly. There are many general methods built into BaseMapper that can be called directly, which is very simple:
Int deleteByPrimaryKey (competitive id); int insert (S record); int insertAll (Iterable record); int insertSelective (S record); S selectByPrimaryKey (competitive id); Optional selectByPrimaryKeyWithOptional (ID id); int updateByPrimaryKey (S record); int updateByPrimaryKeySelective (S record); List selectAll (); List selectList (S record); S selectOne (S record); S selectOneWithOptional (S record); boolean existsByPrimaryKey (competitive id); int count (S record); List selectByExample (Example example); int countByExample (Example example); int deleteByExample (Example example) Int updateByExample (@ Param ("record") S record, @ Param ("example") Example example); int updateByExampleSelective (@ Param ("record") S record, @ Param ("example") Example example)
To test the interface call, try the selectAll method first:
@ GetMapping ("getUser") public void getUser () {List users = userMapper.selectAll (); for (User user: users) {System.out.println (user.getName () + "+ user.getIdentifycard ());}}
Test results:
In this way, it is possible to query directly without writing a sql statement by calling the built-in method. Similarly, if you want to query based on the primary key, call the selectByPrimaryKey method directly:
PostMapping ("getUserById") public void getUserByIdentifycard (@ RequestBody User user) {User retUser = userMapper.selectByPrimaryKey (user); System.out.println (retUser.toString ());}
Query results:
Alternatively, you can use the Optional package query to modify the method of the primary key query above:
@ PostMapping ("getUserById") public void getUserByIdentifycard (@ RequestBody User user) {User retUser = userMapper.selectByPrimaryKeyWithOptional (user) .orElseThrow (()-> new RuntimeException ("No data found"); System.out.println (retUser.toString ());}
In this way, when you pass in a primary key that does not exist, a custom exception is thrown directly:
There are many other simple queries, you can test it yourself according to the api listed above. In addition, you can use Criteria and logical combinations to make functional queries:
@ GetMapping ("criteriaTest") public void testCreateCriteria () {List list = userMapper.createCriteria () .andEqualTo (User::getName, "Trunks") .andbetween.andNotLike (User::getRate, "6") .orin (User::getCard, Arrays.asList ("10")) .selectList () List.forEach (user-> {System.out.println (user.toString ());});}
Query results:
You can also use Example to query:
@ GetMapping ("exampleTest") public void testExample () {Example example=Example.of (User.class); example.createCriteria () .andEqualTo (User::getName, "Trunks") .andbetween.andNotLike (User::getRate, "6") .orin (User::getCard, Arrays.asList ("10")); example.setDistinct (true) List list = userMapper.selectByExample (example); list.forEach (user-> {System.out.println (user.toString ());});}
The result is the same as using Criteria. In addition, you can combine several conditions:
GetMapping ("testExampleWithSub") public void selectByExampleWithSub () {try (SqlSession session = sqlSessionFactory.openSession ()) {UserMapper userMapper1 = session.getMapper (UserMapper.class); Example example=Example.of (User.class); example.and () .andEqualTo (User::getName, "Trunks"); example.and () .andEqualTo (User::getCard, "10") Example.and () .andLessThanOrEqualTo (User::getRate,300); Criteria criteria=new Criteria (); criteria.andIsNotNull (User::getPhone); example.and (criteria); List list = userMapper1.selectByExample (example); list.forEach (user-> {System.out.println (user.toString ());});}} result:
This is the end of the article on "how to use MyBatis-Ext". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.