使用wueasy框架过程中,微服务怎么使用MyBatis
对多个数据库进行操作?
前一章我们已经介绍了MyBatis使用教程,本章是在上一章的基础上进行操作,假设你已经对上一章内容有所了解。
新增数据库映射
编写数据库表结构映射实体bean,注意:包的路径和之前不一样。
为了简化自定义sql,这里使用的是通用Mapper
,部分属性需要使用注解方式,注解可参考以下api文档https://github.com/abel533/Mapper/wiki/2.2-mapping
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
| package com.wueasy.demo2.entity;
import java.io.Serializable;
import javax.persistence.Id; import javax.persistence.Table;
import tk.mybatis.mapper.annotation.KeySql;
@Table(name = "demo") public class Demo implements Serializable {
private static final long serialVersionUID = 1L; @Id @KeySql(useGeneratedKeys = true) private Long id;
private String name;
private Long createdBy;
private String createdTime;
private Long modifiedBy;
private String modifiedTime;
private String city;
private String type;
private String sex;
private String state;
private String description;
private String filePath;
private String area1;
private String area2;
private String area3;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Long getCreatedBy() { return createdBy; }
public void setCreatedBy(Long createdBy) { this.createdBy = createdBy; }
public String getCreatedTime() { return createdTime; }
public void setCreatedTime(String createdTime) { this.createdTime = createdTime; }
public Long getModifiedBy() { return modifiedBy; }
public void setModifiedBy(Long modifiedBy) { this.modifiedBy = modifiedBy; }
public String getModifiedTime() { return modifiedTime; }
public void setModifiedTime(String modifiedTime) { this.modifiedTime = modifiedTime; }
public String getCity() { return city; }
public void setCity(String city) { this.city = city; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getFilePath() { return filePath; }
public void setFilePath(String filePath) { this.filePath = filePath; }
public String getArea1() { return area1; }
public void setArea1(String area1) { this.area1 = area1; }
public String getArea2() { return area2; }
public void setArea2(String area2) { this.area2 = area2; }
public String getArea3() { return area3; }
public void setArea3(String area3) { this.area3 = area3; }
}
|
新增mapper接口
注意:包的路径和之前不一样 接口需要继承通用Mapper
接口,Mapper
接口提供了常见的增删改查方法,可以直接使用。
1 2 3 4 5 6 7 8 9 10 11 12
| package com.wueasy.demo2.mapper;
import java.util.List;
import com.wueasy.base.entity.DataMap; import com.wueasy.demo2.entity.Demo;
import tk.mybatis.mapper.common.Mapper;
public interface DemoMapper extends Mapper<Demo> {
}
|
数据库连接配置
配置数据库连接信息和mybatis扫描信息,需要配置两个数据库连接。
重点注意:mybatis
的扫描配置,通过配置不同的扫描路径,自动注入不同的数据源
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
| wueasy: datasource : demo : mybatis: basePackage: com.wueasy.demo.mapper mapperLocations: classpath*:mybatis/wueasy/demo/*.xml connection: url : jdbc:mysql://localhost:3306/easy_data?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 username : easyadmin password : easyadmin initialSize : 10 minIdle : 10 maxActive : 20 maxWait : 60000 timeBetweenEvictionRunsMillis : 2000 minEvictableIdleTimeMillis : 600000 maxEvictableIdleTimeMillis : 600000 validationQuery : SELECT 1 from dual testWhileIdle : true testOnBorrow : false testOnReturn : false keepAlive : true demo2 : mybatis: basePackage: com.wueasy.demo2.mapper mapperLocations: classpath*:mybatis/wueasy/demo2/*.xml connection: url : jdbc:mysql://localhost:3306/easy_data?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf-8 username : easyadmin password : easyadmin initialSize : 10 minIdle : 10 maxActive : 20 maxWait : 60000 timeBetweenEvictionRunsMillis : 2000 minEvictableIdleTimeMillis : 600000 maxEvictableIdleTimeMillis : 600000 validationQuery : SELECT 1 from dual testWhileIdle : true testOnBorrow : false testOnReturn : false keepAlive : true
|
测试接口
使用junit
测试接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes=Application.class) public class DbTests {
@Autowired private UserMapper userMapper;
@Autowired private DemoMapper demoMapper;
@Test public void testObj() {
try{ System.err.println(JsonHelper.toJSONString(userMapper.selectAll()));
}catch(Exception e) { e.printStackTrace(); }
} }
|