In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Students who have used Mybatis know that for each database table used in each project, it is necessary to establish its corresponding database add, delete, modify and check xxxMapper.xml files, entity class xxx.java files and other classes to call xxxMapper.java files for database operations. When I started learning Mybatis, I'm sure a lot of people created these files manually. There is no doubt that it is very inefficient to create these files manually if the project is large, and we can generate these files automatically through mybatis-generator. However, the tool generates related files by default in the form of a command line, so we can automatically generate them by writing an Ant script that executes the Ant script in eclipse every time we need to create these files. The complete steps are as follows:
Import related jar packages
To use "mybatis-generator", you need to import a corresponding mybatis-generator-1.3.x.jar file in the lib of the web project. The download address on Github is mybatis-generator 's jar package.
Configuration file for configuring mybatis-generator
(1) first, create several new packages in the project to store the corresponding files:
As can be seen from the figure above, src/main/java is used to store Java source code; src/main/env/dev is used to store configuration files in the development environment (such as jdbc, cache, log, etc.); src/main/resources is used to store some general configuration files, where our automatically generated Mapper.xml files are stored in this path; src/test/java represents test code, regardless of here
Note: how do I add these source folders in eclipse?
(2) create generatorConfig.xml and build_mybatis.xml under the root directory of the project:
These two files are the configuration file for "mybatis-generator" and the automated Ant script, and the path in the project is as follows:
I) generatorConfig.xml:
Note: some areas that need to be modified can be modified by referring to my comments above, and don't forget the data-driven jar package.
Ii) build_mybatis.xml:
There are only two points to pay attention to in the above code: one is the jar package of "mybatis-generator", and the other is the corresponding "generatorConfig.xml" file.
Note: if you are not familiar with Ant scripts, you can refer to this article I wrote: http://www.zifangsky.cn/444.html
Three tests
When testing the effect, just drag the file "build_mybatis.xml" to the Ant view, then click execute this script to automatically generate the file we need, and finally refresh the project structure to see the file. The effect is as follows:
Note: the database data used in my test:
SET FOREIGN_KEY_CHECKS=0;---Table structure for user---DROP TABLE IF EXISTS `user` CREATE TABLE `user` (`id` int (11) NOT NULL AUTO_INCREMENT, `name` varchar (32) DEFAULT NULL, `password` varchar (64) DEFAULT NULL, `email` varchar (64) DEFAULT NULL, `day` date DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 -Records of user-- INSERT INTO `user`VALUES ('1x, 'admin',' 123456, 'admin@qq.com',' 2000-01-02') INSERT INTO `user`VALUES ('2mm,' test', '12344th,' test@zifangsky.cn', '1990-12-12'); INSERT INTO `user`VALUES (' 32nd, 'xxxx',' xx', 'xx@zifangsky.cn',' 1723-06-21')
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.