# introduction:

Mybatis is still quite popular in the persistence layer framework, general projects are based on SSM. Although Mybatis can operate the database directly in XML through SQL statements, it is very flexible. But even if all operations are done through SQL statements, it is necessary to write a large number of XML files, which is very troublesome. Mybatis – Plus solves this problem. Sorted out a 272 page Mybatis study notes

Mybatis – plus introduction.

Mybatis-Plus (MP for short) is a Mybatis enhancement tool, on the basis of Mybatis only do enhancement do not change, to simplify the development and improve efficiency. This is the official definition. For more introduction and features of Mybatis – Plus, please refer to mybatis- Plus official website. So how is it enhanced? In fact, it already encapsulates some CRUD methods, so we don’t need to write XML anymore, just call these methods, similar to JPA.

Today I will share with you a tutorial on using MyBatisPlus in the DAO layer.

Official API address: mp.baomidou.com/#/?id=%E7%A…

Mybatis-PLus is an enhancement kit of Mybatis. It only enhances and does not change. It is born to simplify development work and improve productivity.

# general CRUD

  • Through this project (small table, large amount of data, very suitable)

  • Found MyBatisPlus in single table CRUD

  • Has absolute advantages over the original MyBatis:

Druid data source configuration, MyBatisPlus paging configuration, and SQL output configuration are available on Github.

Github.com/larger5/Spr…

@RunWith(SpringRunner.class) @SpringBootTest public class PlusApplicationTests { @Autowired private UserMapper userMapper; /** * 1, insert */ @test public void insertTest() {User User = new User(); User. SetUsername (" green "); user.setPassword("lvcha"); 1 Integer insert1 = usermapper. insert(user); Println (user.getid ())); system.out.println (user.getid ()); / / same as above Integer insert2 = userMapper. InsertAllColumn (user); // System.out.println(user.getid ()); Update */ @test public void updateTest() {User User = new User(); user.setId(45); user.setUsername("cun"); //user.setPassword("666"); 1 Integer integer1 = usermapper. updateById(user); System.out.println(integer1); / / same as above Integer integer2 = userMapper. UpdateAllColumnById (user); System.out.println(integer2); Select * from @test public void selectTest() {// Select usermapper.selectById (46) from @test public void selectTest() { System.out.println(user.getUsername()); ArrayList<Integer> idList = new ArrayList<>(); idList.add(61); idList.add(63); / / based on multiple id batch query List < User > users = userMapper. SelectBatchIds (idList); System.out.println(users.get(0).getUsername() + users.get(1).getUsername()); User user1 = new User(); user1.setId(61); user1.setUsername("cun"); User user2 = usermapper.selectOne (user1); usermapper.selectOne (user1); System.out.println(user2.getPassword()); ColumnMap = new HashMap<String, Object> columnMap = new HashMap<>(); columnMap.put("username", "cun"); List<User> users1 = userMapper.selectByMap(columnMap); System.out.println(users1.size()); } /** * 4, delete delete */ @test public void deleteTest() { 1 Integer integer1 = userMapper.deleteById(65); System.out.println(integer1); ArrayList<Integer> idList = new ArrayList<>(); Add (64); // Delete idlist.add (64); idList.add(66); / / based on multiple id batch delete, return to the database affect operands: 2 Integer integer2 = userMapper. DeleteBatchIds (idList); System.out.println(integer2); HashMap<String, Object> columnMap = new HashMap<>(); columnMap.put("username", "cun"); Integer integer3 = usermapper. deleteByMap(columnMap); System.out.println(integer3); */ @test public void pageTest() {// if the PaginationInterceptor intercepts pageTest(), the PaginationInterceptor intercepts pageTest();  List<User> users2 = userMapper.selectPage(new Page<User>(1, 2), null); System.out.println(users2.get(0).getUsername() + users2.get(1).getUsername())); System.out.println(users2.size()); } @test public void wrapperTest(){List<User> users = usermapper.selectList (new EntityWrapper<User>()) .eq("username", "linhongcun") ); System.out.println(users.size()); }}Copy the code

# Code generator

The above code is automatically written by MyBatisPlsus general Mapper layer, in the Service layer to write the relevant business logic, in fact, using the MyBatisPlus code generator, Automatically generate Entity, Dao, Service, Controller layer!

We usually write the relevant business logic in the Controller layer, using a similar approach to Mapper.

public class MpG { public static void main(String[] args) { //1. GlobalConfig config = new GlobalConfig(); Config.setactiverecord (false) // Whether the AR mode is supported. SetAuthor ("linhongcun") // author.setOutputDir ("C:\ data\ mp") // Generate path SetIdType (idtype.auto) // Primary key policy.setServicename ("%sService") // Sets whether the name of the generated Service interface starts with I  // IUserService .setBaseResultMap(true) .setBaseColumnList(true); //2. DataSourceConfig dsConfig = new DataSourceConfig(); Dsconfig.setdbtype (dbtype.mysql) // Set the database type. SetDriverName (" com.mysql.jdbc.driver ") The setUrl (" JDBC: mysql: / / 120.79.197.130:3307 / testspring? useSSL=true&verifyServerCertificate=false&allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8") .setUsername("root") .setPassword("123"); //3. StrategyConfig stConfig = new StrategyConfig(); Stconfig.setcapitalmode (true) // global uppercase naming.setdbColumnunderline (true) // Specifies whether the table name field name is underlined .setnaming (namingstrategy.underline_to_camel) // NamingStrategy for database table mapping to entity.settableprefix ("tb_").setinclude ("tb_user"); PackageConfig pkConfig = new PackageConfig(); pkConfig.setParent("com.cun.plus") .setMapper("mapper") .setService("service") .setController("controller") .setEntity("entity") .setXml("mapper"); AutoGenerator AG = new AutoGenerator(); ag.setGlobalConfig(config) .setDataSource(dsConfig) .setStrategy(stConfig) .setPackageInfo(pkConfig); / / 6. Perform ag. The execute (); }}Copy the code

# dependency

// Select freemarker engine, default Veloctiy // mpg.setTemplateEngine(new FreemarkerTemplateEngine()); <! -- mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> The < version > 2.3 < / version > < / dependency > <! <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> The < version > 2.0 < / version > < / dependency >Copy the code

Finally, attention:

One of the drawbacks of using MyBatis is that it relies on using code generators so that the logic is written in the Controller layer rather than the service layer, which is not appropriate. Sorted out a 272 page Mybatis study notes