In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the use of TKMybatis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
What is TKMybatis
TKMybatis is a tool developed based on the Mybatis framework, which internally implements the basic data operation of a single table. It can complete the operation of a single table without writing any sql by simply inheriting the interface provided by TKMybatis.
2. TKMybatis uses 2.1 Springboot project to add dependency tk.mybatis mapper-spring-boot-starter 2.0.4.
Add dependencies to the POJO class
Javax.persistence persistence-api 1.0 compile
Configure @ MapperScan scan in the startup class
@ SpringBootApplication@MapperScan (basePackages = {"com.tom.order.mapper"}) public class OrderApplication {public static void main (String [] args) {SpringApplication.run (OrderApplication.class, args);}} 2.2 use instructions
2.2.1 used in entity classes
In entity classes, common annotations and meanings are:
@ Table: describes the information of database tables. The main attributes are name (table name), schema, catalog, uniqueConstraints, etc.
Id: specifies the table primary key field with no attribute value.
@ Column: describes the field information of the database. The main attributes are name (field name), columnDefinition, insertable, length, nullable (whether it can be empty or not), precision, scale, table, unique, updatable and so on.
@ ColumnType: describes the field type of the database. Some special types can be configured and handled. The main properties include jdbcType, column, typeHandler and so on.
Other annotations such as @ Transient, @ ColumnResult, @ JoinColumn, @ OrderBy, @ Embeddable are not described for the time being.
2.2.2 use in dao
For single table operation, you only need to inherit the Mapper interface under tk.mybatis to use
Import tk.mybatis.mapper.common.Mapper; @ Repositorypublic interface BrandMapper extends Mapper {}
Check the specific use: basic single table operations have been encapsulated internally.
2.2.3 used in the Service layer
Add Mapper.insert (record) to the operation type; save an entity, the attributes of null will also be saved, and will not use the database default value Mapper.insertSelective (record); save an entity, ignore null values, that is, uncommitted values will use database default values
Delete Mapper.delete (record); delete according to entity attributes as conditions; query conditions use equal sign Mapper.deleteByExample (example) to delete data according to Example conditions Mapper.deleteByPrimaryKey (key) delete data according to primary key fields. Method parameters must contain complete primary key attributes.
Modify Mapper.updateByExample (record,example) update all attributes contained in entity `record` according to Example condition, Mapper.updateByExampleSelective (record,example) update entity `record` contains attribute value other than null according to Example condition Mapper.updateByPrimaryKey (record) update all fields of entity according to primary key, null value will be updated Mapper.updateByPrimaryKeySelective (record) update attribute not null according to primary key
Query Mapper.select (record) queries according to the attribute values in the entity, query conditions use the equal sign Mapper.selectAll () query all results Mapper.selectByExample (example) query according to Example conditions Mapper.selectByPrimaryKey (key) query according to primary key fields, method parameters must contain complete primary key attributes, query conditions use the equal sign Mapper.selectCount (record) to query the total number of attributes in the entity Query conditions use the equal sign Mapper.selectCountByExample (example) to query the total number of Mapper.selectOne (record) based on Example conditions
When querying according to the attributes in the entity, there can only be one return value, multiple results throw an exception, and the query condition uses the equal sign.
However, if there is an attribute that is int, it is initialized to 0. May affect the actual use
2.3 actual cases
2.3.1 dao layer usage
Import tk.mybatis.mapper.common.Mapper; / * DAO using the general Mapper * DSO interface needs to inherit tk.mybatis.mapper.common.Mapper * / @ Repositorypublic interface BrandMapper extends Mapper {}
2.3.2 service layer usage
Import com.github.pagehelper.PageHelper;import com.github.pagehelper.PageInfo;import org.apache.commons.lang.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import tk.mybatis.mapper.entity.Example; import java.util.List; @ Servicepublic class BrandServiceImpl implements BrandService {@ Autowired private BrandMapper brandMapper; public Example createExample (Brand brand) {/ / Custom conditional search object Example Example example = new Example (Brand.class) Example.Criteria criteria = example.createCriteria (); / / conditional constructor if (brand! = null) {if (! StringUtils.isEmpty (brand.getName () {criteria.andLike ("name",'%'+ brand.getName () +'%') } if (! StringUtils.isEmpty (brand.getLetter () {criteria.andEqualTo ("letter", brand.getLetter ();}} return example;} @ Override public List findAll () {return brandMapper.selectAll ();} @ Override public List findList (Brand brand) {Example example = createExample (brand) Return brandMapper.selectByExample (example);} @ Override public Brand findById (Integer id) {return brandMapper.selectByPrimaryKey (id) } / * paging query * @ param page current page * @ param size number of entries displayed per page * @ return * / @ Override public PageInfo pageSearch (Integer page, Integer size) {/ / paging implementation / / the query must be immediately following the collection query PageHelper.startPage (page, size) / / query collection List brands = brandMapper.selectAll (); return new PageInfo (brands);} @ Override public PageInfo pageSearchAndCondition (Brand brand, Integer page, Integer size) {/ / start paging PageHelper.startPage (page, size); / / search data Example example = createExample (brand); List list = brandMapper.selectByExample (example); return new PageInfo (list) } / * add brand * @ param brand * / @ Override public void add (Brand brand) {/ / use the general Mapper.insertSelective / / method with selective will ignore the null value int I = brandMapper.insertSelective (brand) } / * modify brand according to id * @ param brand * / @ Override public void update (Brand brand) {/ / use generic mapper.update (); brandMapper.updateByPrimaryKeySelective (brand);} / * delete * @ param id * / @ Override public void del (Integer id) {brandMapper.deleteByPrimaryKey (id) according to id Third, extended introduction
Use
Public interface BaseMapper extends tk.mybatis.mapper.common.BaseMapper, IdsMapper, MySqlMapper, OracleMapper {} pom.xml introduce tk.mybatis mapper-spring-boot-starter 2.1.5
Note: for demonstration purposes, both MySqlMapper and OracleMapper are referenced at the same time. Normally, only one can be referenced because they have the same method insertList (List list).
The type of generic (entity class) must meet the requirements
Entity classes are converted according to the following rules and database tables, and the annotations are all annotations in JPA:
The table name defaults to the class name, and the hump is underlined (only uppercase letters are processed). For example, the table name corresponding to UserInfo is user_info by default.
Table names can be specified using @ Table (name = "tableName"), and table names can be specified in this way for those that do not meet the first default rule.
The field defaults to the same as @ Column as the table field, and the table field defaults to the Field name hump of the Java object to underscore.
You can use @ Column (name = "fieldName") to specify field names that do not conform to rule 3.
The field can be ignored with the @ Transient annotation, and the field added to the annotation is not used as a table field.
It is recommended that there must be a field with an @ Id annotation as the primary key, and multiple @ Id annotated fields as the federated primary key.
All mapper inheritance classes will have the following common methods
Query method
General methods under BaseSelectMapper
Method name functions as List selectAll (); query all data T selectByPrimaryKey (Object key); query T selectOne (T record) through primary key; query single data List select (T record) through entity; query multiple data int selectCount (T record) through entity; query number of entities boolean existsWithPrimaryKey (Object key) through entity; query whether this primary key exists through primary key.
General methods under SelectByIdsMapper
Method names function as List selectByIds (String var1); query data through multiple primary keys
Add method
General methods under BaseInsertMapper
Method name function int insert (T record); add all int insertSelective (T record); add selectively (not null)
General methods under MySqlMapper
The method name functions as int insertList (List list); batch inserts int insertUseGeneratedKeys (T record); if the primary key is self-increasing, you can use this method to get the added primary key
General methods under OracleMapper
Method name function int insertList (List list); batch insert
Modification method
General methods under BaseUpdateMapper
Method name acts as int updateByPrimaryKey (T record); int updateByPrimaryKeySelective is modified according to entity (T record); selective modification is made according to entity
Delete method
General methods under BaseDeleteMapper
The method name functions as int delete (T record); delete int deleteByPrimaryKey by entity (Object o); delete by primary key
General methods under IdsMapper
The name of the method functions as int deleteByIds (String var1); the content of "what is the use of TKMybatis" is deleted in batches according to the primary key. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.