In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "Mybatis uses @ Mapper and @ MapperScan annotations to achieve mapping relationship method tutorial". Many people will encounter this dilemma in the operation of actual cases, 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!
Catalogue
Use @ Mapper and @ MapperScan annotations to implement mapping
Mybatis-@MapperScan and mybatis:scan analysis
MapperScan
Use @ Mapper and @ MapperScan annotations to implement mapping
After the integration of MyBatis and Spring, it is necessary to realize the mapping relationship between entities and data tables.
To implement the mapping between entities and data tables, you can add @ Mapper annotations to the Mapper class, as shown in the following code:
/ * user Information Mapper dynamic proxy API * @ author pan_junbiao * * / @ Mapper@Repositorypublic interface UserMapper {/ * add users And get the self-incremented primary key * / @ Insert ("INSERT INTO tb_user (user_account,user_password,blog_url,blog_remark) VALUES (# {userAccount}, # {userPassword}, # {blogUrl}, # {blogRemark})") @ Options (useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId") / / or: @ SelectKey (statement = "SELECT LAST_INSERT_ID ()", keyColumn = "user_id", keyProperty = "userId", before = false) ResultType = Integer.class) public int insertUser (UserInfo userInfo) / * modify user * / @ Update ("UPDATE tb_user SET user_account = # {userAccount}, user_password = # {userPassword}, blog_url=# {blogUrl}, blog_remark=# {blogRemark} WHERE user_id = # {userId}") public int updateUser (UserInfo userInfo) / * Delete user * / @ Delete ("DELETE FROM tb_user WHERE user_id = # {userId}") public int deleteUser (int userId); / * obtain user information according to user ID * / @ Select ("SELECT * FROM tb_user WHERE user_id = # {userId}") public UserInfo getUserById (int userId);}
However, it is recommended that you directly add @ MapperScan ("com.pjb.mapper") annotation to the SpringBoot startup class in the future, which will be more convenient, and you do not need to add @ Mapper annotation to each Mapper.
Package com.pjb; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication; / * SpringBoot launch class * @ author pan_junbiao * * / @ MapperScan ("com.pjb.mapper") @ SpringBootApplicationpublic class SpringbootMybatisDemoApplication {public static void main (String [] args) {SpringApplication.run (SpringbootMybatisDemoApplication.class, args);}} Mybatis-@MapperScan and mybatis:scan analysis
MyBatis-Spring-1.2.0 has added two new ways to scan the mapper Mapper interface:
Use element
Use @ MapperScan annotations (Spring3.1+ version required)
The element searches for the mapper Mapper interface in a specific comma-separated list of package names. To use this new MyBatis-Spring namespace, you need to add the following schema declaration:
Element provides the following attributes to customize the scanning process:
Annotation: the scanner will register all mapper Mapper interfaces that are in the base-package package and match the specified annotations.
Factory-ref: when there are multiple SqlSessionFactory instances in the Spring context, you need to specify a specific SqlSessionFactory to create the mapper Mapper interface. Normally, only more than one data source in the application will be used.
Marker-interface: the scanner will map the Mapper interface of the mapper registered in the base-package package and inheriting a specific interface class
Template-ref: when there are multiple SqlSessionTemplate instances in the Spring context, you need to specify a specific SqlSessionTemplate to create the mapper Mapper interface. Normally, only more than one data source in the application will be used.
The fully qualified class name of the name-generator:BeannameGenerator class to name the detected component.
MapperScan
Spring 3.x + supports the use of @ Configuration and @ Bean annotations to provide Java-based configurations. If you use a java-based configuration, you can use the @ MapperScan annotation to scan the mapper Mapper interface. @ MapperScan and how it works
The same, and the corresponding customization options are also provided.
Configuration@MapperScan ("com.mybatis.x.mappers") public class AppConfig {@ Beanpublic DataSource dataSource () {return new PooledDataSource ("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "root");} @ Beanpublic SqlSessionFactory sqlSessionFactory () throws Exception {SqlSessionFactoryBeansessionFactory = newSqlSessionFactoryBean (); sessionFactory.setDataSource (dataSource ()); return sessionFactory.getObject ();}}
The @ MapperScan annotation has the following attributes for use by the custom scanning process:
AnnotationClass: the scanner will register all mapper Mapper interfaces that are in the base-package package and match the specified annotations.
MarkerInterface: the scanner will map the Mapper interface of the mapper registered in the base-package package and inheriting a specific interface class
SqlSessionFactoryRef: used to specify a special SqlSessionFactory when there is more than one SqlSesssionFactory in the Spring context
SqlSessionTemplateRef: used to specify a specific sqlSessionTemplate when there is more than one sqlSessionTemplate in the Spring context
The nameGenerator:BeanNameGenerator class is used to name the components detected in the Spring container.
A type-safe alternative to basePackageClasses:basePackages (). Every class in the package is scanned.
BasePackages: the base packet scanned by the scanner, which scans the internal Mapper interface. Note that only those with at least one method declaration in the package will be registered. The specific class will be ignored.
Of course, it can also be configured in applicationContext.xml as follows
Use MapperScannerConfigurer to scan all mapper Mapper interfaces under the package package ("com.mybatis3.mappers") and register automatically
This is the end of the tutorial on how Mybatis uses @ Mapper and @ MapperScan annotations to implement mapping relationships. Thank you for 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.