This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
WangScaler: A writer with heart.
Declaration: uneducated, if there is a mistake, kindly correct.
We Java everyone is most familiar with the framework of spring buckets, the operation of the database, often use Mybatis. But most of the time the SQL statement is not complex, it is simple to add, delete, change and check, if we also start to write, it will be a waste of time, today I recommend you a tool Mybatis-plus based on Mybatis.
Mybatis- Plus does not change Mybatis, but only enhances Mybatis. In fact, it encapsulates some CRUD methods for us to call directly. Let’s take a look.
Introduction of depend on
First of all, if the project uses Maven, it can directly introduce the dependency mybatis-plus-boot-starter, of course, using this dependency must also introduce other necessary dependencies of the project, here takes mysql as an example.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>Latest Version</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Copy the code
With dependencies introduced, mysql related configurations can be used with a simple configuration.
configuration
Mysql-related configuration on application.yml configuration.
spring:
datasource:
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/wangscaler? useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
Copy the code
This is just a sample of some basic configurations. It is recommended that scaffolding be integrated with Druid, and the plugin is quite powerful.
Classes that inherit from Mybatis- Plus
After inheriting BaseMapper, you can use some of the methods of the parent class. If the file name of your object User does not match the database TableName, you can specify the database TableName on the object User using the @tablename annotation.
public interface UserMapper extends BaseMapper<User> {}Copy the code
use
Calling the parent class method selectList directly is equivalent to the SQL statement SELECT * FROM user; We don’t need to write the SQL statement in mapper.xml. We can achieve the result we want in one sentence, which saves us a lot of time.
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect(a) {
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size()); userList.forEach(System.out::println); }}Copy the code
Code generator
When our new business appears, we may need to create new package names, and repeatedly create new files under controller, bean, mapper and Service. The code base of the files is almost the same, so we can directly use Mybatis- Plus to automatically generate these codes.
Introduction of depend on
Add new dependencies to the above dependencies.
// Attention!! The current package is not passed dependent on MP package, you need to import<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>Latest Version</version>
</dependency>
Copy the code
code
FastAutoGenerator.create(DATA_SOURCE_CONFIG)
// Global configuration
.globalConfig((scanner, builder) -> builder.author(scanner.apply("Please enter author name?")).fileOverride())
/ / package configuration
.packageConfig((scanner, builder) -> builder.parent(scanner.apply("Please enter the package name?")))
// Policy configuration
.strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("Please enter the table name, separated by multiple English commas? All type all")))
.controllerBuilder().enableRestStyle().enableHyphenStyle()
.entityBuilder().enableLombok().addTableFills(
new Column("create_time", FieldFill.INSERT)
).build())
/* Template engine configuration, Default Velocity optional templateEngine Beetl or freemarker.templateengine (new BeetlTemplateEngine()).templateengine (new) FreemarkerTemplateEngine()) */
.execute();
// Handle all
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
Copy the code
You can run this code, will be based on the relationship between the data tables of your database, automatically generated base code, is not very convenient, fast.
conclusion
This plug-in is developed by domestic bigshot, integrated into the project, will greatly reduce our workload, I recommend you to use. Still the same words, try to have a say. Of course, after integration, you are not used to using his functions, you can still use all the functions of Mybatis through it. This plugin only provides a simple SQL statement method, complex of course you still need to write your own SQL statement.
Come all come, click “like” and then go!
Follow WangScaler and wish you a promotion, a raise and no bucket!