SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

一、添加所需依赖,当前完整的pom文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
  4.0 . 0
 
   org.springframework.boot
   spring-boot-starter-parent
   1.5 . 6 .RELEASE
 
  org.rcddup
  rcddup-custom
  1.0 . 0
 
   1.8
   1.3 . 1
   3.4 . 3
   1.3 . 3
   1.1 . 4
   1.2 . 1
 
 
  
  
    org.springframework.boot
    spring-boot-starter-web
  
  
  
    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    ${mybatis.version}
  
  
  
    org.mybatis.generator
    mybatis-generator-core
    ${mybatis-generator.version}
  
  
  
    tk.mybatis
    mapper-spring-boot-starter
    ${mybatis-tk.version}
  
  
  
    mysql
    mysql-connector-java
  
 
 
  
   
   
     org.mybatis.generator
     mybatis-generator-maven-plugin
     ${mybatis.generator.version}
    
     
       mysql
       mysql-connector-java
       ${mysql.version}
     
     
       tk.mybatis
       mapper
       ${mapper.version}
     
    
    
     
      true
     
      true
     
     
       ${basedir}/src/main/resources/generator/generatorConfig.xml
     
    
   
  
 

  MyBtais 结合通用 Mapper 非常好使,接下来看逆向工程的配置。

  有关通用Mapper的信息,请查看:https://github.com/abel533/Mapper

二、generatorConfig.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"1.0" encoding= "UTF-8" ?>
   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
 
  "application.properties" />
 
  "Mysql" targetRuntime= "MyBatis3Simple" defaultModelType= "flat" >
   "beginningDelimiter" value= "`" />
   "endingDelimiter" value= "`" />
  
   "tk.mybatis.mapper.generator.MapperPlugin" >
    "mappers" value= "${generator.mappers}" />
  
  
   "org.mybatis.generator.plugins.ToStringPlugin" />
  
   "org.mybatis.generator.plugins.SerializablePlugin" />
  
  
  
   "${spring.datasource.driver-class-name}"
    connectionURL= "${spring.datasource.url}"
    userId= "${spring.datasource.username}"
    password= "${spring.datasource.password}" >
  
   "${generator.javaModel-targetPackage}"
    targetProject= "${generator.targetProject}" />
   "${generator.sqlMap-targetPackage}"
    targetProject= "${generator.targetProject}" />
   "${generator.javaClient-targetPackage}"
    targetProject= "${generator.targetProject}" type= "XMLMAPPER" />
  
  
  
  
  
   "user">
    "id" sqlStatement= "Mysql" identity= "true" />
  
 

  我们将对应的信息都在 application.properties 中定义好,在这直接使用,application.properties如下:

?
1
2
3
4
5
6
# Generator
generator.targetProject=src/main/java
generator.mappers=org.rcddup.app.common.base.IBaseMapper
generator.javaModel-targetPackage=org.rcddup.app.domain
generator.sqlMap-targetPackage=org.rcddup.app.dao
generator.javaClient-targetPackage=org.rcddup.app.dao

三、编写生成Mapper的集成接口IBaseMapper

?
1
2
3
4
5
package org.rcddup.app.common.base;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface IBaseMapper extends Mapper, MySqlMapper {
}

  集成的 Mapper 以及 MySqlMapper 几乎包含了你所有的 CRUD方法。

四、使用逆向工程生成我们的第一个文件

  右键项目 --> Maven --> Maven build --> Goals: mybtais-generator:generate,点击 run.....

  我们的UserMapper以及UserMapper.xml就生成了。

  (1)UserMapper.java

?
1
2
3
4
5
package org.rcddup.app.dao;
import org.rcddup.app.common.base.IBaseMapper;
import org.rcddup.app.domain.User;
public interface UserMapper extends IBaseMapper {
}

  (2)UserMapper.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
"1.0" encoding= "UTF-8" ?>
"-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
"org.rcddup.app.dao.UserMapper" >
  "BaseResultMap" type= "org.rcddup.app.domain.User" >
 
  "id" jdbcType= "BIGINT" property= "id" />
  "name" jdbcType= "VARCHAR" property= "name" />
  "age" jdbcType= "TINYINT" property= "age" />
  "status" jdbcType= "TINYINT" property= "status" />
 

  是不是发现xml中都没有配置任何CRUD方法。

五、重新编写 UserService.java

  我们原先编写的 UserService 现在我们用新生成的 UserMapper 来重新编写。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.rcddup.app.service.impl;
import java.util.List;
import org.rcddup.app.dao.UserMapper;
import org.rcddup.app.domain.User;
import org.rcddup.app.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
@Service
public class UserService implements IUserService{
  @Autowired
  private UserMapper userMapper;
  @Override
  public List listUser() {
   return userMapper.selectAll();
  }
  @Override
  public User getUserById(Long id) {
   // 方法一:适用于任何字段的查询
//  Example example = new Example(User.class);
//  example.createCriteria().andEqualTo("id", id);
//  List list = userMapper.selectByExample(example);
//  if (list != null && list.size() > 0) {
//   return list.get(0);
//  } else {
//   return null;
//  }
   // 方法二:只适用于主键字段
   return userMapper.selectByPrimaryKey(id);
  }
}

六、启动 App.java

  输入:localhost:8080/user/get?id=1

  输入:localhost:8080/user/list

  好了,到这我们整合了Mybatis逆向工程以及通用Mapper。

你可能感兴趣的:(spring,myBatis,通用,spring)