55 | * 当使用多数据源时,需要通过 {@link org.springframework.context.annotation.Primary} 注解指定主要的默认的 {@link MapperProvider}
56 | */
57 | @Configuration
58 | public static class AutoRegisterConfiguration implements InitializingBean {
59 | private final MapperProvider mapperProvider;
60 |
61 | public AutoRegisterConfiguration(MapperProvider mapperProvider) {
62 | this.mapperProvider = mapperProvider;
63 | }
64 |
65 | @Override
66 | public void afterPropertiesSet() {
67 | mapperProvider.registerAsDefault();
68 | }
69 | }
70 |
71 | }
72 |
--------------------------------------------------------------------------------
/activerecord/src/main/java/io/mybatis/activerecord/spring/boot/autoconfigure/MapperProviderProperties.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring.boot.autoconfigure;
18 |
19 | import io.mybatis.activerecord.spring.MapperProvider;
20 | import org.springframework.boot.context.properties.ConfigurationProperties;
21 |
22 | /**
23 | * Mapper 提供者配置
24 | *
25 | * @author liuzh
26 | */
27 | @ConfigurationProperties(MapperProviderProperties.PREFIX)
28 | public class MapperProviderProperties {
29 | public static final String PREFIX = "iomybatis.framework.activerecord";
30 | /**
31 | * 是否启动自动注入 {@link MapperProvider},默认 true
32 | */
33 | private boolean enabled = true;
34 |
35 | public boolean isEnabled() {
36 | return enabled;
37 | }
38 |
39 | public void setEnabled(boolean enabled) {
40 | this.enabled = enabled;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/activerecord/src/main/resources/META-INF/spring.factories:
--------------------------------------------------------------------------------
1 | org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2 | io.mybatis.activerecord.spring.boot.autoconfigure.MapperProviderAutoConfiguration
--------------------------------------------------------------------------------
/activerecord/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
--------------------------------------------------------------------------------
1 | io.mybatis.activerecord.spring.boot.autoconfigure.MapperProviderAutoConfiguration
2 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/BaseId.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | import io.mybatis.activerecord.ActiveRecord;
20 | import io.mybatis.mapper.BaseMapper;
21 | import io.mybatis.provider.Entity;
22 |
23 | @Entity.Table
24 | public class BaseId implements ActiveRecord {
25 | @Entity.Column(id = true, insertable = false)
26 | private Integer id;
27 |
28 | @Override
29 | public Integer pkValue() {
30 | return id;
31 | }
32 |
33 | public Integer getId() {
34 | return id;
35 | }
36 |
37 | public void setId(Integer id) {
38 | this.id = id;
39 | }
40 |
41 | @Override
42 | public BaseMapper baseMapper() {
43 | //获取指定数据源的实例
44 | return MapperProvider.>getInstance("mapperProviderUser").baseMapper(entityClass());
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/CustomMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.activerecord.spring;
17 |
18 | import io.mybatis.mapper.Mapper;
19 |
20 | public interface CustomMapper extends Mapper {
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/README.md:
--------------------------------------------------------------------------------
1 | # 测试说明
2 |
3 | 当使用 ActiveRecord 时,不建议存在多数据源。
4 |
5 | 为了避免真的需要多数据源,因此唯一的这个测试直接使用多数据源,这个实例只是为了测试,不适合直接在生产环境照搬使用。
6 |
7 | 当前测试用例为多数据源,因为在同一个包下面,扫描时为了区分开,增加两个 Marker 接口:
8 |
9 | - UserMarker
10 | - RoleMarker
11 |
12 | 在配置扫描时,指定了扫描那些接口:
13 |
14 | ```xml
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | ```
28 |
29 | 在 `BaseId` 实现中,使用了 `database-schema-user.sql` 对应的数据源:
30 |
31 | ```xml
32 |
33 |
34 |
35 |
36 | ```
37 |
38 | ```java
39 | @Override
40 | public Mapper baseMapper(){
41 | //获取指定数据源的实例
42 | return SpringMapperRegistry.>getInstance("springMapperRegistryUser").baseMapper(entityClass());
43 | }
44 | ```
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/Role.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | import io.mybatis.activerecord.EntityRecord;
20 | import io.mybatis.provider.Entity;
21 |
22 | @Entity.Table
23 | public class Role implements EntityRecord {
24 | @Entity.Column(id = true, insertable = false)
25 | private Integer id;
26 |
27 | @Entity.Column
28 | private String name;
29 |
30 | public Integer getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Integer id) {
35 | this.id = id;
36 | }
37 |
38 | public String getName() {
39 | return name;
40 | }
41 |
42 | public void setName(String name) {
43 | this.name = name;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/RoleMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.activerecord.spring;
17 |
18 | import io.mybatis.mapper.Mapper;
19 |
20 | public interface RoleMapper extends Mapper, RoleMarker {
21 |
22 | Role findById(Integer id);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/RoleMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
23 |
24 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/RoleMarker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | public interface RoleMarker {
20 | }
21 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/SystemTask.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | import java.util.Optional;
20 |
21 | public class SystemTask {
22 |
23 | public void run() {
24 | Optional user = new User().baseMapper().wrapper().eq(User::getId, 1).one();
25 | user.ifPresent(u -> System.out.println(u.getName()));
26 | }
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | @Entity.Table
22 | public class User extends BaseId {
23 |
24 | @Entity.Column
25 | private String name;
26 |
27 | @Entity.Column("role_id")
28 | private Integer roleId;
29 |
30 | public User() {
31 | }
32 |
33 | public User(String name) {
34 | this.name = name;
35 | }
36 |
37 | public User(String name, Integer roleId) {
38 | this.name = name;
39 | this.roleId = roleId;
40 | }
41 |
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | public void setName(String name) {
47 | this.name = name;
48 | }
49 |
50 | public Integer getRoleId() {
51 | return roleId;
52 | }
53 |
54 | public void setRoleId(Integer roleId) {
55 | this.roleId = roleId;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.activerecord.spring;
17 |
18 | import io.mybatis.mapper.base.EntityProvider;
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.InsertProvider;
21 | import org.apache.ibatis.annotations.Lang;
22 | import org.apache.ibatis.annotations.Options;
23 |
24 | public interface UserMapper extends CustomMapper, UserMarker {
25 | /**
26 | * 保存实体,默认主键自增,并且名称为 id
27 | *
28 | * 这个方法是个示例,你可以在自己的接口中使用相同的方式覆盖父接口中的配置
29 | *
30 | * @param entity 实体类
31 | * @return 1成功,0失败
32 | */
33 | @Override
34 | @Lang(Caching.class)
35 | @Options(useGeneratedKeys = true, keyProperty = "id")
36 | @InsertProvider(type = EntityProvider.class, method = "insert")
37 | int insert(User entity);
38 |
39 | User findById(Long id);
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
23 |
24 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/UserMarker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.activerecord.spring;
18 |
19 | public interface UserMarker {
20 | }
21 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/database-schema-role.sql:
--------------------------------------------------------------------------------
1 | drop table role if exists;
2 |
3 | create table role
4 | (
5 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
6 | name VARCHAR(32) DEFAULT 'DEFAULT'
7 | );
8 |
9 | insert into role(id, name)
10 | values (1, '管理员'),
11 | (2, '游客');
12 |
--------------------------------------------------------------------------------
/activerecord/src/test/java/io/mybatis/activerecord/spring/database-schema-user.sql:
--------------------------------------------------------------------------------
1 | drop table user if exists;
2 |
3 | create table user
4 | (
5 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
6 | name VARCHAR(32) DEFAULT 'DEFAULT',
7 | role_id INTEGER
8 | );
9 |
10 | insert into user(id, name, role_id)
11 | values (1, 'admin', 1),
12 | (2, 'guest', 2);
13 |
--------------------------------------------------------------------------------
/activerecord/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/common/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-common
29 |
30 |
31 |
32 | junit
33 | junit
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/core/Code.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.core;
18 |
19 | import io.mybatis.common.util.I18n;
20 |
21 | /**
22 | * 响应码
23 | *
24 | * @author liuzh
25 | */
26 | public class Code {
27 | public static final String CODE_BUNDLE = "mybatis_common_code";
28 | public static final I18n.Language LANG = I18n.language(CODE_BUNDLE);
29 | public static final Code SUCCESS = new Code("00000");
30 | public static final Code FAILURE = new Code("M0100");
31 | public static final Code UNKNOWN = new Code("M0200");
32 | public static final Code SAVE_FAILURE = new Code("M0201");
33 | public static final Code UPDATE_FAILURE = new Code("M0202");
34 | public static final Code DELETE_FAILURE = new Code("M0203");
35 |
36 | private String code;
37 | private String message;
38 |
39 | public Code() {
40 | }
41 |
42 | public Code(String code) {
43 | this.code = code;
44 | this.message = LANG.message(code);
45 | }
46 |
47 | public Code(String code, String message) {
48 | this.code = code;
49 | this.message = message;
50 | }
51 |
52 | public String getCode() {
53 | return code;
54 | }
55 |
56 | public void setCode(String code) {
57 | this.code = code;
58 | }
59 |
60 | public String getMessage() {
61 | return message;
62 | }
63 |
64 | public void setMessage(String message) {
65 | this.message = message;
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/core/DataResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.core;
18 |
19 | /**
20 | * 返回单个值
21 | *
22 | * @param 值类型
23 | * @author liuzh
24 | */
25 | public class DataResponse extends Response {
26 | /**
27 | * 单个数据对象
28 | */
29 | private T data;
30 |
31 | public static DataResponse ok(T data) {
32 | return ok(data, null);
33 | }
34 |
35 | public static DataResponse ok(T data, String message) {
36 | DataResponse response = new DataResponse<>();
37 | response.success = true;
38 | response.code = Code.SUCCESS.getCode();
39 | response.data = data;
40 | response.message = message;
41 | return response;
42 | }
43 |
44 | public DataResponse data(T data) {
45 | this.data = data;
46 | return this;
47 | }
48 |
49 | public T getData() {
50 | return data;
51 | }
52 |
53 | public void setData(T data) {
54 | this.data = data;
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/core/Response.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.core;
18 |
19 | import io.mybatis.common.exception.ServiceException;
20 | import io.mybatis.common.util.I18n;
21 |
22 | /**
23 | * 响应结果
24 | *
25 | * @author liuzh
26 | */
27 | public class Response {
28 | public static final String RESPONSE_BUNDLE = "mybatis_common_response";
29 | /**
30 | * 执行是否成功
31 | */
32 | protected boolean success;
33 | /**
34 | * 响应码
35 | */
36 | protected String code;
37 | /**
38 | * 响应信息
39 | */
40 | protected String message;
41 |
42 | public static Response ok() {
43 | Response response = new Response();
44 | response.success = true;
45 | response.code = Code.SUCCESS.getCode();
46 | return response;
47 | }
48 |
49 | public static Response error() {
50 | return error(Code.UNKNOWN);
51 | }
52 |
53 | public static Response error(String code) {
54 | return error(code, I18n.message(RESPONSE_BUNDLE, code));
55 | }
56 |
57 | public static Response error(Throwable t) {
58 | return error(Code.UNKNOWN.getCode(), t.getMessage());
59 | }
60 |
61 | public static Response error(ServiceException e) {
62 | return error(e.getCode());
63 | }
64 |
65 | public static Response error(String code, String message) {
66 | Response response = new Response();
67 | response.success = false;
68 | response.code = code;
69 | response.message = message;
70 | return response;
71 | }
72 |
73 | public static Response error(Code code) {
74 | return error(code.getCode(), code.getMessage());
75 | }
76 |
77 | public T code(String code) {
78 | this.code = code;
79 | return (T) this;
80 | }
81 |
82 | public T message(String message) {
83 | this.message = message;
84 | return (T) this;
85 | }
86 |
87 | public T success(boolean success) {
88 | this.success = success;
89 | return (T) this;
90 | }
91 |
92 | public boolean isSuccess() {
93 | return success;
94 | }
95 |
96 | public void setSuccess(boolean success) {
97 | this.success = success;
98 | }
99 |
100 | public String getCode() {
101 | return code;
102 | }
103 |
104 | public void setCode(String code) {
105 | this.code = code;
106 | }
107 |
108 | public String getMessage() {
109 | return message;
110 | }
111 |
112 | public void setMessage(String message) {
113 | this.message = message;
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/core/RowsResponse.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.core;
18 |
19 | import java.util.List;
20 |
21 | /**
22 | * 返回多个值集合
23 | *
24 | * @param 值类型
25 | * @author liuzh
26 | */
27 | public class RowsResponse extends Response {
28 |
29 | /**
30 | * 数据集合
31 | */
32 | private List rows;
33 |
34 | /**
35 | * 总数,分页查询时的总条数
36 | */
37 | private Long total;
38 |
39 | public static RowsResponse ok(List rows) {
40 | return ok(rows, null, null);
41 | }
42 |
43 | public static RowsResponse ok(List rows, Long total) {
44 | return ok(rows, total, null);
45 | }
46 |
47 | public static RowsResponse ok(List rows, String message) {
48 | return ok(rows, null, message);
49 | }
50 |
51 | public static RowsResponse ok(List rows, Long total, String message) {
52 | RowsResponse response = new RowsResponse<>();
53 | response.success = true;
54 | response.code = Code.SUCCESS.getCode();
55 | response.rows = rows;
56 | response.total = total;
57 | response.message = message;
58 | return response;
59 | }
60 |
61 | public RowsResponse rows(List rows) {
62 | this.rows = rows;
63 | return this;
64 | }
65 |
66 | public RowsResponse total(Long total) {
67 | this.total = total;
68 | return this;
69 | }
70 |
71 | public List getRows() {
72 | return rows;
73 | }
74 |
75 | public void setRows(List rows) {
76 | this.rows = rows;
77 | }
78 |
79 | public Long getTotal() {
80 | return total;
81 | }
82 |
83 | public void setTotal(Long total) {
84 | this.total = total;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/exception/AssertException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.exception;
18 |
19 | import io.mybatis.common.core.Code;
20 |
21 | /**
22 | * 断言异常
23 | *
24 | * @author liuzh
25 | */
26 | public class AssertException extends ServiceException {
27 | public static final Code ASSERT_FAILURE = new Code("M0300");
28 |
29 | public AssertException(String message) {
30 | super(new Code(ASSERT_FAILURE.getCode(), message));
31 | }
32 |
33 | public AssertException(Code code) {
34 | super(code);
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/exception/ServiceException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.exception;
18 |
19 | import io.mybatis.common.core.Code;
20 |
21 | /**
22 | * 业务异常
23 | *
24 | * @author liuzh
25 | */
26 | public class ServiceException extends RuntimeException {
27 | private Code code;
28 |
29 | public ServiceException(Code code) {
30 | super(code.getMessage());
31 | this.code = code;
32 | }
33 |
34 | public ServiceException(Code code, Throwable cause) {
35 | this(code);
36 | this.code = code;
37 | }
38 |
39 | public Code getCode() {
40 | return code;
41 | }
42 |
43 | public void setCode(Code code) {
44 | this.code = code;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/common/src/main/java/io/mybatis/common/util/I18n.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.util;
18 |
19 | import java.text.MessageFormat;
20 | import java.util.Locale;
21 | import java.util.MissingResourceException;
22 | import java.util.ResourceBundle;
23 |
24 | /**
25 | * 多语言支持
26 | *
27 | * 测试时,通过 JVM 参数可以设置 Locale {@code -Duser.country=US -Duser.language=en}
28 | *
29 | * @author liuzh
30 | */
31 | public class I18n {
32 |
33 | /**
34 | * 获取对应语言的文本,当资源文件或key不存在时,直接返回 {@code MessageFormat.format(key, args)}
35 | *
36 | * @param locale 语言
37 | * @param bundleName 资源文件名
38 | * @param key 字符串key
39 | * @param args 格式化参数
40 | * @return 格式化文本
41 | */
42 | public static String message(Locale locale, String bundleName, String key, Object... args) {
43 | ResourceBundle bundle;
44 | try {
45 | bundle = ResourceBundle.getBundle(bundleName, locale);
46 | } catch (Exception e) {
47 | bundle = null;
48 | }
49 | try {
50 | return MessageFormat.format(bundle.getString(key), args);
51 | } catch (MissingResourceException e) {
52 | return MessageFormat.format(key, args);
53 | }
54 | }
55 |
56 | /**
57 | * 获取对应语言的文本,当资源文件或key不存在时,直接返回 {@code MessageFormat.format(key, args)}
58 | *
59 | * @param bundleName 资源文件名
60 | * @param key 字符串key
61 | * @param args 格式化参数
62 | * @return 格式化文本
63 | */
64 | public static String message(String bundleName, String key, Object... args) {
65 | return message(Locale.getDefault(), bundleName, key, args);
66 | }
67 |
68 | /**
69 | * 获取语言包
70 | *
71 | * @param locale 语言
72 | * @param bundleName 语言包名称
73 | * @return
74 | */
75 | public static Language language(Locale locale, String bundleName) {
76 | return (key, args) -> message(locale, bundleName, key, args);
77 | }
78 |
79 | /**
80 | * 获取语言包
81 | *
82 | * @param bundleName 语言包名称
83 | * @return
84 | */
85 | public static Language language(String bundleName) {
86 | return language(Locale.getDefault(), bundleName);
87 | }
88 |
89 | /**
90 | * 语言包
91 | */
92 | public interface Language {
93 | /**
94 | * 获取对应语言的文本
95 | *
96 | * @param key
97 | * @param args
98 | * @return
99 | */
100 | String message(String key, Object... args);
101 | }
102 |
103 | }
104 |
--------------------------------------------------------------------------------
/common/src/main/resources/mybatis_common_code.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 | 00000=\u64CD\u4F5C\u6210\u529F
17 | M0100=\u64CD\u4F5C\u5931\u8D25
18 | M0200=\u672A\u77E5\u9519\u8BEF
19 | M0201=\u4FDD\u5B58\u5931\u8D25
20 | M0202=\u4FEE\u6539\u5931\u8D25
21 | M0203=\u5220\u9664\u5931\u8D25
22 | M0300=\u6821\u9A8C\u5931\u8D25
23 |
--------------------------------------------------------------------------------
/common/src/main/resources/mybatis_common_code_en.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 | 00000=Success
17 | M0100=Failure
18 | M0200=Unknown
19 | M0201=Save failure
20 | M0202=Update failure
21 | M0203=Delete failure
22 | M0300=Assert failure
23 |
--------------------------------------------------------------------------------
/common/src/main/resources/mybatis_common_code_zh.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 | 00000=\u64CD\u4F5C\u6210\u529F
17 | M0100=\u64CD\u4F5C\u5931\u8D25
18 | M0200=\u672A\u77E5\u9519\u8BEF
19 | M0201=\u4FDD\u5B58\u5931\u8D25
20 | M0202=\u4FEE\u6539\u5931\u8D25
21 | M0203=\u5220\u9664\u5931\u8D25
22 | M0300=\u6821\u9A8C\u5931\u8D25
23 |
--------------------------------------------------------------------------------
/common/src/test/java/io/mybatis/common/util/I18nTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.common.util;
18 |
19 | import io.mybatis.common.core.Code;
20 | import org.junit.Test;
21 |
22 | import java.util.Locale;
23 |
24 | public class I18nTest {
25 |
26 | @Test
27 | public void testCode() {
28 | Locale.setDefault(Locale.CHINA);
29 | org.junit.Assert.assertEquals("操作成功", Code.SUCCESS.getMessage());
30 | org.junit.Assert.assertEquals("操作失败", Code.FAILURE.getMessage());
31 | org.junit.Assert.assertEquals("未知错误", Code.UNKNOWN.getMessage());
32 | org.junit.Assert.assertEquals("保存失败", Code.SAVE_FAILURE.getMessage());
33 | org.junit.Assert.assertEquals("修改失败", Code.UPDATE_FAILURE.getMessage());
34 | org.junit.Assert.assertEquals("删除失败", Code.DELETE_FAILURE.getMessage());
35 | }
36 |
37 | @Test
38 | public void testDefault() {
39 | //使用当前操作系统默认值
40 | org.junit.Assert.assertEquals("操作成功", I18n.message(Code.CODE_BUNDLE, "00000"));
41 | //语言无法匹配时,使用默认值
42 | org.junit.Assert.assertEquals("操作成功", I18n.message(new Locale("hello", "WORLD"), Code.CODE_BUNDLE, "00000"));
43 | }
44 |
45 | @Test
46 | public void testZh() {
47 | org.junit.Assert.assertEquals("操作成功", I18n.message(new Locale("zh", ""), Code.CODE_BUNDLE, "00000"));
48 | }
49 |
50 | @Test
51 | public void testZhCN() {
52 | org.junit.Assert.assertEquals("操作成功", I18n.message(new Locale("zh", "CN"), Code.CODE_BUNDLE, "00000"));
53 | }
54 |
55 | @Test
56 | public void testEn() {
57 | org.junit.Assert.assertEquals("Success", I18n.message(new Locale("en", ""), Code.CODE_BUNDLE, "00000"));
58 | }
59 |
60 | @Test
61 | public void testEnUS() {
62 | org.junit.Assert.assertEquals("Success", I18n.message(new Locale("en", "US"), Code.CODE_BUNDLE, "00000"));
63 | }
64 |
65 | @Test
66 | public void testEnOthers() {
67 | org.junit.Assert.assertEquals("Success", I18n.message(new Locale("en", "Others"), Code.CODE_BUNDLE, "00000"));
68 | }
69 |
70 | @Test
71 | public void testLanguage() {
72 | I18n.Language defLanguage = I18n.language(new Locale("hello", "WORLD"), Code.CODE_BUNDLE);
73 | org.junit.Assert.assertEquals("操作成功", defLanguage.message("00000"));
74 |
75 | I18n.Language zhLanguage = I18n.language(new Locale("zh", ""), Code.CODE_BUNDLE);
76 | org.junit.Assert.assertEquals("操作成功", zhLanguage.message("00000"));
77 |
78 | I18n.Language enLanguage = I18n.language(new Locale("en", "Others"), Code.CODE_BUNDLE);
79 | org.junit.Assert.assertEquals("Success", enLanguage.message("00000"));
80 | }
81 |
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/generator/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-generator
29 | MyBatis Mapper Generator Templates
30 |
31 |
32 |
--------------------------------------------------------------------------------
/generator/src/main/resources/lib/mysql-connector-java-5.1.49.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mybatis-mapper/mapper/eaa78e8dbae41a4813768d55bb7e987c851aa8b0/generator/src/main/resources/lib/mysql-connector-java-5.1.49.jar
--------------------------------------------------------------------------------
/generator/src/main/resources/lib/rui-cli.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mybatis-mapper/mapper/eaa78e8dbae41a4813768d55bb7e987c851aa8b0/generator/src/main/resources/lib/rui-cli.jar
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/controller.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import ${project.attrs.basePackage}.model.${it.name.className};
4 | import ${project.attrs.basePackage}.service.${it.name.className}Service;
5 |
6 | import org.springframework.beans.factory.annotation.Autowired;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.RestController;
10 |
11 | import java.util.List;
12 | /**
13 | * ${it.name} - ${it.comment}
14 | *
15 | * @author ${SYS['user.name']}
16 | */
17 | @RestController
18 | @RequestMapping("${it.name.fieldName.s}")
19 | public class ${it.name.className}Controller {
20 |
21 | @Autowired
22 | private ${it.name.className}Service ${it.name.fieldName}Service;
23 |
24 | @RequestMapping(method = RequestMethod.GET)
25 | public List<${it.name.className}> ${it.name.fieldName.s}(${it.name.className} ${it.name.fieldName}) {
26 | return ${it.name.fieldName}Service.findList(${it.name.fieldName});
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/mapper.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import io.mybatis.mapper.Mapper;
4 | import ${project.attrs.basePackage}.model.${it.name.className};
5 |
6 | /**
7 | * ${it.name} - ${it.comment}
8 | *
9 | * @author ${SYS['user.name']}
10 | */
11 | @org.apache.ibatis.annotations.Mapper
12 | public interface ${it.name.className}Mapper extends Mapper<${it.name.className}, Long> {
13 |
14 | }
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/mapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
21 | <#list it.columns as column>
22 | <#if column.pk>
23 |
24 | <#else>
25 | <#if column.comment?has_content>#if>
26 | #if>
27 | #list>
28 |
29 |
30 |
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/model-lombok.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import io.mybatis.provider.Entity;
4 | import org.apache.ibatis.type.JdbcType;
5 |
6 | import lombok.Getter;
7 | import lombok.Setter;
8 |
9 | <#list it.importJavaTypes as javaType>
10 | import ${javaType};
11 | #list>
12 |
13 | /**
14 | * ${it.name} - ${it.comment}
15 | *
16 | * @author ${SYS['user.name']}
17 | */
18 | @Getter
19 | @Setter
20 | @Entity.Table("${it.name}" remark = "${it.comment}", autoResultMap = true)
21 | public class ${it.name.className} {
22 | <#list it.columns as column>
23 | <#if column.pk>
24 | @Entity.Column(value = "${column.name}", id = true, remark = "${column.comment}", updatable = false, insertable = false)
25 | <#else>
26 | @Entity.Column(value = "${column.name}", remark = "${column.comment}"<#if column.tags.jdbcType>, jdbcType = JdbcType.${column.jdbcType}#if>)
27 | #if>
28 | private ${column.javaType} ${column.name.fieldName};
29 |
30 | #list>
31 | }
32 |
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/model.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import io.mybatis.provider.Entity;
4 | import org.apache.ibatis.type.JdbcType;
5 |
6 | <#list it.importJavaTypes as javaType>
7 | import ${javaType};
8 | #list>
9 |
10 | /**
11 | * ${it.name} <#if it.comment?has_content>- ${it.comment}#if>
12 | *
13 | * @author ${SYS['user.name']}
14 | */
15 | @Entity.Table(value = "${it.name}", <#if it.comment?has_content>remark = "${it.comment}", #if>autoResultMap = true)
16 | public class ${it.name.className} {
17 | <#list it.columns as column>
18 | <#if column.pk>
19 | @Entity.Column(value = "${column.name}", id = true, remark = "${column.comment}", updatable = false, insertable = false)
20 | <#else>
21 | @Entity.Column(value = "${column.name}", remark = "${column.comment}"<#if column.tags.jdbcType>, jdbcType = JdbcType.${column.jdbcType}#if>)
22 | #if>
23 | private ${column.javaType} ${column.name.fieldName};
24 |
25 | #list>
26 |
27 | <#list it.columns as column>
28 | /**
29 | * 获取 ${column.comment}
30 | *
31 | * @return ${column.name.fieldName} - ${column.comment}
32 | */
33 | public ${column.javaType} get${column.name.className}() {
34 | return ${column.name.fieldName};
35 | }
36 |
37 | /**
38 | * 设置${column.comment}
39 | *
40 | * @param ${column.name.fieldName} ${column.comment}
41 | */
42 | public void set${column.name.className}(${column.javaType} ${column.name.fieldName}) {
43 | this.${column.name.fieldName} = ${column.name.fieldName};
44 | }
45 |
46 | #list>
47 | }
48 |
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/service.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import io.mybatis.service.BaseService;
4 |
5 | import ${project.attrs.basePackage}.model.${it.name.className};
6 |
7 | /**
8 | * ${it.name} - ${it.comment}
9 | *
10 | * @author ${SYS['user.name']}
11 | */
12 | public interface ${it.name.className}Service extends BaseService<${it.name.className}, Long> {
13 |
14 | }
15 |
--------------------------------------------------------------------------------
/generator/src/main/resources/mapper-templates/serviceImpl.java:
--------------------------------------------------------------------------------
1 | package ${package};
2 |
3 | import io.mybatis.service.AbstractService;
4 |
5 | import ${project.attrs.basePackage}.service.${it.name.className}Service;
6 | import ${project.attrs.basePackage}.mapper.${it.name.className}Mapper;
7 | import ${project.attrs.basePackage}.model.${it.name.className};
8 | import org.springframework.stereotype.Service;
9 |
10 | /**
11 | * ${it.name} - ${it.comment}
12 | *
13 | * @author ${SYS['user.name']}
14 | */
15 | @Service
16 | public class ${it.name.className}ServiceImpl extends AbstractService<${it.name.className}, Long, ${it.name.className}Mapper> implements ${it.name.className}Service {
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/jakarta-jpa/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-jakarta-jpa
29 |
30 | 11
31 | 11
32 |
33 |
34 |
35 |
36 | io.mybatis
37 | mybatis-provider
38 |
39 |
40 |
41 | jakarta.persistence
42 | jakarta.persistence-api
43 | 3.1.0
44 | compile
45 |
46 |
47 |
48 |
49 | org.hsqldb
50 | hsqldb
51 |
52 |
53 | ch.qos.logback
54 | logback-classic
55 |
56 |
57 | junit
58 | junit
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/java/io/mybatis/provider/jpa/JakartaJpaEntityClassFinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.defaults.GenericEntityClassFinder;
20 | import jakarta.persistence.Table;
21 |
22 | /**
23 | * 支持识别带有 @javax.persistence.Table 的实体类或者不带任何注解的POJO
24 | *
25 | * @author liuzh
26 | */
27 | public class JakartaJpaEntityClassFinder extends GenericEntityClassFinder {
28 |
29 | @Override
30 | public boolean isEntityClass(Class> clazz) {
31 | //带注解或不是简单类型和枚举的都算实体
32 | return clazz.isAnnotationPresent(Table.class)
33 | || (!clazz.isPrimitive()
34 | && !clazz.isInterface()
35 | && !clazz.isArray()
36 | && !clazz.isAnnotation()
37 | && !clazz.isEnum() && !SimpleTypeUtil.isSimpleType(clazz));
38 | }
39 |
40 | @Override
41 | public int getOrder() {
42 | return super.getOrder() + 100;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/java/io/mybatis/provider/jpa/JakartaJpaEntityColumnFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.*;
20 | import jakarta.persistence.*;
21 | import org.apache.ibatis.type.TypeHandler;
22 |
23 | import java.util.Arrays;
24 | import java.util.List;
25 | import java.util.Optional;
26 |
27 | /**
28 | * 通过 SPI 工厂扩展 EntityColumn 和 EntityTable
29 | *
30 | * @author liuzh
31 | */
32 | public class JakartaJpaEntityColumnFactory implements EntityColumnFactory {
33 |
34 | @Override
35 | public Optional> createEntityColumn(EntityTable entityTable, EntityField field, Chain chain) {
36 | Optional> optionalEntityColumns = chain.createEntityColumn(entityTable, field);
37 | if (optionalEntityColumns == IGNORE || field.isAnnotationPresent(Transient.class)) {
38 | return IGNORE;
39 | } else if (!optionalEntityColumns.isPresent()) {
40 | //没有 @Transient 注解的字段都认为是表字段,不自动排除字段,字段名默认驼峰转下划线
41 | optionalEntityColumns = Optional.of(Arrays.asList(EntityColumn.of(field).column(Style.getDefaultStyle().columnName(entityTable, field))));
42 | }
43 | if (optionalEntityColumns.isPresent()) {
44 | List entityColumns = optionalEntityColumns.get();
45 | for (EntityColumn entityColumn : entityColumns) {
46 | EntityField entityField = entityColumn.field();
47 | //主键
48 | if (!entityColumn.id()) {
49 | entityColumn.id(entityField.isAnnotationPresent(Id.class));
50 | }
51 | //列名
52 | if (field.isAnnotationPresent(Column.class)) {
53 | Column column = field.getAnnotation(Column.class);
54 | String columnName = column.name();
55 | if (!columnName.isEmpty()) {
56 | entityColumn.column(columnName);
57 | }
58 | entityColumn.insertable(column.insertable()).updatable(column.updatable());
59 | if (column.scale() != 0) {
60 | entityColumn.numericScale(String.valueOf(column.scale()));
61 | }
62 | }
63 | //只能默认空 ASC,或者写 ASC 或 DESC,不能写多个列
64 | if (field.isAnnotationPresent(OrderBy.class)) {
65 | OrderBy orderBy = field.getAnnotation(OrderBy.class);
66 | if (orderBy.value().isEmpty()) {
67 | entityColumn.orderBy("ASC");
68 | } else {
69 | entityColumn.orderBy(orderBy.value());
70 | }
71 | }
72 | //TypeHandler注解
73 | if(field.isAnnotationPresent(Convert.class)) {
74 | Convert convert = field.getAnnotation(Convert.class);
75 | Class converter = convert.converter();
76 | if(converter != void.class || TypeHandler.class.isAssignableFrom(converter)) {
77 | entityColumn.typeHandler(converter);
78 | }
79 | }
80 | }
81 | }
82 | return optionalEntityColumns;
83 | }
84 |
85 | @Override
86 | public int getOrder() {
87 | return EntityColumnFactory.super.getOrder() + 100;
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/java/io/mybatis/provider/jpa/JakartaJpaEntityTableFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.EntityTable;
20 | import io.mybatis.provider.EntityTableFactory;
21 | import io.mybatis.provider.Style;
22 | import io.mybatis.provider.util.Utils;
23 | import jakarta.persistence.Entity;
24 | import jakarta.persistence.Table;
25 |
26 | /**
27 | * 通过 SPI 工厂扩展 EntityColumn 和 EntityTable
28 | *
29 | * @author liuzh
30 | */
31 | public class JakartaJpaEntityTableFactory implements EntityTableFactory {
32 |
33 | @Override
34 | public EntityTable createEntityTable(Class> entityClass, Chain chain) {
35 | EntityTable entityTable = chain.createEntityTable(entityClass);
36 | if (entityTable == null) {
37 | entityTable = EntityTable.of(entityClass);
38 | }
39 | if (entityClass.isAnnotationPresent(Table.class)) {
40 | Table table = entityClass.getAnnotation(Table.class);
41 | if (!table.name().isEmpty()) {
42 | entityTable.table(table.name());
43 | }
44 | if(!table.catalog().isEmpty()) {
45 | entityTable.catalog(table.catalog());
46 | }
47 | if(!table.schema().isEmpty()) {
48 | entityTable.schema(table.schema());
49 | }
50 | } else if (Utils.isEmpty(entityTable.table())) {
51 | //没有设置表名时,默认类名转下划线
52 | entityTable.table(Style.getDefaultStyle().tableName(entityClass));
53 | }
54 | //使用 JPA 的 @Entity 注解作为开启 autoResultMap 的标志,可以配合字段的 @Convert 注解使用
55 | if(entityClass.isAnnotationPresent(Entity.class)) {
56 | entityTable.autoResultMap(true);
57 | }
58 | return entityTable;
59 | }
60 |
61 | @Override
62 | public int getOrder() {
63 | return EntityTableFactory.super.getOrder() + 100;
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityClassFinder:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JakartaJpaEntityClassFinder
18 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityColumnFactory:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JakartaJpaEntityColumnFactory
18 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityTableFactory:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JakartaJpaEntityTableFactory
18 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/BaseTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider;
18 |
19 | import org.apache.ibatis.io.Resources;
20 | import org.apache.ibatis.jdbc.ScriptRunner;
21 | import org.apache.ibatis.session.SqlSession;
22 | import org.apache.ibatis.session.SqlSessionFactory;
23 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24 | import org.junit.BeforeClass;
25 |
26 | import java.io.IOException;
27 | import java.io.Reader;
28 | import java.sql.Connection;
29 |
30 | public class BaseTest {
31 | private static SqlSessionFactory sqlSessionFactory;
32 |
33 | @BeforeClass
34 | public static void init() {
35 | try {
36 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
37 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38 | reader.close();
39 |
40 | //创建数据库
41 | try (SqlSession session = sqlSessionFactory.openSession()) {
42 | Connection conn = session.getConnection();
43 | reader = Resources.getResourceAsReader("testdb.sql");
44 | ScriptRunner runner = new ScriptRunner(conn);
45 | runner.setLogWriter(null);
46 | runner.runScript(reader);
47 | reader.close();
48 | }
49 | } catch (IOException ignore) {
50 | }
51 | }
52 |
53 | public SqlSession getSqlSession() {
54 | return sqlSessionFactory.openSession();
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/BaseProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.EntityColumn;
20 | import io.mybatis.provider.SqlScript;
21 | import org.apache.ibatis.builder.annotation.ProviderContext;
22 |
23 | import java.util.stream.Collectors;
24 |
25 | import static io.mybatis.provider.SqlScript.LF;
26 |
27 | public class BaseProvider {
28 |
29 | public static String getById(ProviderContext providerContext) {
30 | return SqlScript.caching(providerContext, entity ->
31 | "SELECT " + entity.baseColumnAsPropertyList() + " FROM " + entity.tableName() +
32 | " WHERE " + entity.idColumns().stream().map(EntityColumn::columnEqualsProperty).collect(Collectors.joining(" AND ")));
33 | }
34 |
35 | public static String deleteById(ProviderContext providerContext) {
36 | return SqlScript.caching(providerContext, entity ->
37 | "DELETE FROM " + entity.tableName() +
38 | " WHERE " + entity.idColumns().stream().map(EntityColumn::columnEqualsProperty).collect(Collectors.joining(" AND ")));
39 | }
40 |
41 | public static String insertSelective(ProviderContext providerContext) {
42 | return SqlScript.caching(providerContext, (entity, util) ->
43 | "INSERT INTO " + entity.tableName()
44 | + util.trimSuffixOverrides("(", ")", ",", () ->
45 | entity.insertColumns().stream().map(column ->
46 | util.ifTest(column.notNullTest(), () -> column.column() + ",")
47 | ).collect(Collectors.joining(LF)))
48 | + util.trimSuffixOverrides(" VALUES (", ")", ",", () ->
49 | entity.insertColumns().stream().map(column ->
50 | util.ifTest(column.notNullTest(), () -> column.variables() + ",")
51 | ).collect(Collectors.joining(LF))));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | import jakarta.persistence.Column;
22 | import jakarta.persistence.Id;
23 | import jakarta.persistence.Table;
24 |
25 | @Table(name = "user")
26 | public class User {
27 | @Id
28 | @Column
29 | private Long id;
30 | @Column(name = "name")
31 | private String username;
32 | @Column
33 | @Entity.Column(selectable = false)
34 | private String sex;
35 |
36 | public Long getId() {
37 | return id;
38 | }
39 |
40 | public void setId(Long id) {
41 | this.id = id;
42 | }
43 |
44 | public String getUsername() {
45 | return username;
46 | }
47 |
48 | public void setUsername(String username) {
49 | this.username = username;
50 | }
51 |
52 | public String getSex() {
53 | return sex;
54 | }
55 |
56 | public void setSex(String sex) {
57 | this.sex = sex;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/UserAutoMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.InsertProvider;
21 | import org.apache.ibatis.annotations.Lang;
22 | import org.apache.ibatis.annotations.Options;
23 | import org.apache.ibatis.annotations.SelectProvider;
24 |
25 | public interface UserAutoMapper {
26 |
27 | @Lang(Caching.class)
28 | @SelectProvider(type = BaseProvider.class, method = "getById")
29 | UserAuto getById(Integer id);
30 |
31 |
32 | @Lang(Caching.class)
33 | @Options(useGeneratedKeys = true, keyProperty = "id")
34 | @InsertProvider(type = BaseProvider.class, method = "insertSelective")
35 | int insertSelective(UserAuto userAuto);
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/UserAutoMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.BaseTest;
20 | import org.apache.ibatis.session.SqlSession;
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 |
24 | public class UserAutoMapperTest extends BaseTest {
25 |
26 | @Test
27 | public void testSelectById() {
28 | try (SqlSession sqlSession = getSqlSession()) {
29 | UserAutoMapper userMapper = sqlSession.getMapper(UserAutoMapper.class);
30 | UserAuto user = userMapper.getById(1);
31 | Assert.assertNotNull(user);
32 | Assert.assertEquals("sjz", user.getUserName());
33 | Assert.assertNotNull(user.getAddress());
34 | Assert.assertEquals("河北省", user.getAddress().getSheng());
35 | Assert.assertEquals("石家庄市", user.getAddress().getShi());
36 |
37 | UserAuto.Address address = user.getAddress();
38 | address.setShi("秦皇岛市");
39 | user.setUserName("qhd");
40 | user.setId(null);
41 | userMapper.insertSelective(user);
42 | Assert.assertNotNull(user.getId());
43 |
44 | UserAuto qhd = userMapper.getById(user.getId());
45 | Assert.assertEquals("河北省", qhd.getAddress().getSheng());
46 | Assert.assertEquals("秦皇岛市", qhd.getAddress().getShi());
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.Lang;
21 | import org.apache.ibatis.annotations.SelectProvider;
22 |
23 | public interface UserMapper {
24 |
25 | @Lang(Caching.class)
26 | @SelectProvider(type = BaseProvider.class, method = "getById")
27 | User getById(Long id);
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/java/io/mybatis/provider/jpa/UserMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.BaseTest;
20 | import org.apache.ibatis.session.SqlSession;
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 |
24 | public class UserMapperTest extends BaseTest {
25 |
26 | @Test
27 | public void testSelectById() {
28 | try (SqlSession sqlSession = getSqlSession()) {
29 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
30 | User user = userMapper.getById(1L);
31 | Assert.assertNotNull(user);
32 | Assert.assertEquals("张无忌", user.getUsername());
33 | Assert.assertNull(user.getSex());
34 | }
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
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 |
--------------------------------------------------------------------------------
/jakarta-jpa/src/test/resources/testdb.sql:
--------------------------------------------------------------------------------
1 | drop table user if exists;
2 | drop table user_auto if exists;
3 |
4 | create table user
5 | (
6 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
7 | name VARCHAR(32) DEFAULT 'DEFAULT',
8 | sex VARCHAR(2)
9 | );
10 |
11 |
12 | insert into user(id, name, sex)
13 | values (1, '张无忌', '男'),
14 | (2, '赵敏', '女'),
15 | (3, '周芷若', '女'),
16 | (4, '小昭', '女'),
17 | (5, '殷离', '女');
18 |
19 | -- 自动映射
20 | create table user_auto
21 | (
22 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
23 | user_name VARCHAR(32) DEFAULT 'DEFAULT',
24 | address VARCHAR(64)
25 | );
26 | insert into user_auto(id, user_name, address)
27 | values (1, 'sjz', '河北省/石家庄市'),
28 | (2, 'hd', '河北省/邯郸市'),
29 | (3, 'xt', '河北省/邢台市');
30 |
--------------------------------------------------------------------------------
/jpa/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-jpa
29 |
30 |
31 |
32 | io.mybatis
33 | mybatis-provider
34 |
35 |
36 |
37 | javax.persistence
38 | javax.persistence-api
39 | 2.2
40 | provided
41 |
42 |
43 |
44 |
45 | io.mybatis
46 | mybatis-mapper
47 | ${project.version}
48 | test
49 |
50 |
51 | org.hsqldb
52 | hsqldb
53 |
54 |
55 | ch.qos.logback
56 | logback-classic
57 |
58 |
59 | junit
60 | junit
61 |
62 |
63 |
64 |
--------------------------------------------------------------------------------
/jpa/src/main/java/io/mybatis/provider/jpa/JpaEntityClassFinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.defaults.GenericEntityClassFinder;
20 |
21 | import javax.persistence.Table;
22 |
23 | /**
24 | * 支持识别带有 @javax.persistence.Table 的实体类或者不带任何注解的POJO
25 | *
26 | * @author liuzh
27 | */
28 | public class JpaEntityClassFinder extends GenericEntityClassFinder {
29 |
30 | @Override
31 | public boolean isEntityClass(Class> clazz) {
32 | //带注解或不是简单类型和枚举的都算实体
33 | return clazz.isAnnotationPresent(Table.class)
34 | || (!clazz.isPrimitive()
35 | && !clazz.isInterface()
36 | && !clazz.isArray()
37 | && !clazz.isAnnotation()
38 | && !clazz.isEnum() && !SimpleTypeUtil.isSimpleType(clazz));
39 | }
40 |
41 | @Override
42 | public int getOrder() {
43 | return super.getOrder() + 100;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/jpa/src/main/java/io/mybatis/provider/jpa/JpaEntityColumnFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.*;
20 | import org.apache.ibatis.type.TypeHandler;
21 |
22 | import javax.persistence.*;
23 | import java.util.Arrays;
24 | import java.util.List;
25 | import java.util.Optional;
26 |
27 | /**
28 | * 通过 SPI 工厂扩展 EntityColumn 和 EntityTable
29 | *
30 | * @author liuzh
31 | */
32 | public class JpaEntityColumnFactory implements EntityColumnFactory {
33 |
34 | @Override
35 | public Optional> createEntityColumn(EntityTable entityTable, EntityField field, Chain chain) {
36 | Optional> optionalEntityColumns = chain.createEntityColumn(entityTable, field);
37 | if (optionalEntityColumns == IGNORE || field.isAnnotationPresent(Transient.class)) {
38 | return IGNORE;
39 | } else if (!optionalEntityColumns.isPresent()) {
40 | //没有 @Transient 注解的字段都认为是表字段,不自动排除字段,字段名默认驼峰转下划线
41 | optionalEntityColumns = Optional.of(Arrays.asList(EntityColumn.of(field).column(Style.getDefaultStyle().columnName(entityTable, field))));
42 | }
43 | if (optionalEntityColumns.isPresent()) {
44 | List entityColumns = optionalEntityColumns.get();
45 | for (EntityColumn entityColumn : entityColumns) {
46 | EntityField entityField = entityColumn.field();
47 | //主键
48 | if (!entityColumn.id()) {
49 | entityColumn.id(entityField.isAnnotationPresent(Id.class));
50 | }
51 | //列名
52 | if (field.isAnnotationPresent(Column.class)) {
53 | Column column = field.getAnnotation(Column.class);
54 | String columnName = column.name();
55 | if (!columnName.isEmpty()) {
56 | entityColumn.column(columnName);
57 | }
58 | entityColumn.insertable(column.insertable()).updatable(column.updatable());
59 | if (column.scale() != 0) {
60 | entityColumn.numericScale(String.valueOf(column.scale()));
61 | }
62 | }
63 | //只能默认空 ASC,或者写 ASC 或 DESC,不能写多个列
64 | if (field.isAnnotationPresent(OrderBy.class)) {
65 | OrderBy orderBy = field.getAnnotation(OrderBy.class);
66 | if (orderBy.value().isEmpty()) {
67 | entityColumn.orderBy("ASC");
68 | } else {
69 | entityColumn.orderBy(orderBy.value());
70 | }
71 | }
72 | //TypeHandler注解
73 | if(field.isAnnotationPresent(Convert.class)) {
74 | Convert convert = field.getAnnotation(Convert.class);
75 | Class converter = convert.converter();
76 | if(converter != void.class || TypeHandler.class.isAssignableFrom(converter)) {
77 | entityColumn.typeHandler(converter);
78 | }
79 | }
80 | }
81 | }
82 | return optionalEntityColumns;
83 | }
84 |
85 | @Override
86 | public int getOrder() {
87 | return EntityColumnFactory.super.getOrder() + 100;
88 | }
89 |
90 | }
91 |
--------------------------------------------------------------------------------
/jpa/src/main/java/io/mybatis/provider/jpa/JpaEntityTableFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.EntityTable;
20 | import io.mybatis.provider.EntityTableFactory;
21 | import io.mybatis.provider.Style;
22 | import io.mybatis.provider.util.Utils;
23 |
24 | import javax.persistence.Entity;
25 | import javax.persistence.Table;
26 |
27 | /**
28 | * 通过 SPI 工厂扩展 EntityColumn 和 EntityTable
29 | *
30 | * @author liuzh
31 | */
32 | public class JpaEntityTableFactory implements EntityTableFactory {
33 |
34 | @Override
35 | public EntityTable createEntityTable(Class> entityClass, Chain chain) {
36 | EntityTable entityTable = chain.createEntityTable(entityClass);
37 | if (entityTable == null) {
38 | entityTable = EntityTable.of(entityClass);
39 | }
40 | if (entityClass.isAnnotationPresent(Table.class)) {
41 | Table table = entityClass.getAnnotation(Table.class);
42 | if (!table.name().isEmpty()) {
43 | entityTable.table(table.name());
44 | }
45 | if(!table.catalog().isEmpty()) {
46 | entityTable.catalog(table.catalog());
47 | }
48 | if(!table.schema().isEmpty()) {
49 | entityTable.schema(table.schema());
50 | }
51 | } else if (Utils.isEmpty(entityTable.table())) {
52 | //没有设置表名时,默认类名转下划线
53 | entityTable.table(Style.getDefaultStyle().tableName(entityClass));
54 | }
55 | //使用 JPA 的 @Entity 注解作为开启 autoResultMap 的标志,可以配合字段的 @Convert 注解使用
56 | if(entityClass.isAnnotationPresent(Entity.class)) {
57 | entityTable.autoResultMap(true);
58 | }
59 | return entityTable;
60 | }
61 |
62 | @Override
63 | public int getOrder() {
64 | return EntityTableFactory.super.getOrder() + 100;
65 | }
66 |
67 | }
68 |
--------------------------------------------------------------------------------
/jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityClassFinder:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JpaEntityClassFinder
18 |
--------------------------------------------------------------------------------
/jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityColumnFactory:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JpaEntityColumnFactory
18 |
--------------------------------------------------------------------------------
/jpa/src/main/resources/META-INF/services/io.mybatis.provider.EntityTableFactory:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.provider.jpa.JpaEntityTableFactory
18 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/BaseTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider;
18 |
19 | import org.apache.ibatis.io.Resources;
20 | import org.apache.ibatis.jdbc.ScriptRunner;
21 | import org.apache.ibatis.session.SqlSession;
22 | import org.apache.ibatis.session.SqlSessionFactory;
23 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24 | import org.junit.BeforeClass;
25 |
26 | import java.io.IOException;
27 | import java.io.Reader;
28 | import java.sql.Connection;
29 |
30 | public class BaseTest {
31 | private static SqlSessionFactory sqlSessionFactory;
32 |
33 | @BeforeClass
34 | public static void init() {
35 | try {
36 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
37 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
38 | reader.close();
39 |
40 | //创建数据库
41 | try (SqlSession session = sqlSessionFactory.openSession()) {
42 | Connection conn = session.getConnection();
43 | reader = Resources.getResourceAsReader("testdb.sql");
44 | ScriptRunner runner = new ScriptRunner(conn);
45 | runner.setLogWriter(null);
46 | runner.runScript(reader);
47 | reader.close();
48 | }
49 | } catch (IOException ignore) {
50 | }
51 | }
52 |
53 | public SqlSession getSqlSession() {
54 | return sqlSessionFactory.openSession();
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/BaseProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.EntityColumn;
20 | import io.mybatis.provider.SqlScript;
21 | import org.apache.ibatis.builder.annotation.ProviderContext;
22 |
23 | import java.util.stream.Collectors;
24 |
25 | import static io.mybatis.provider.SqlScript.LF;
26 |
27 | public class BaseProvider {
28 |
29 | public static String getById(ProviderContext providerContext) {
30 | return SqlScript.caching(providerContext, entity ->
31 | "SELECT " + entity.baseColumnAsPropertyList() + " FROM " + entity.tableName() +
32 | " WHERE " + entity.idColumns().stream().map(EntityColumn::columnEqualsProperty).collect(Collectors.joining(" AND ")));
33 | }
34 |
35 | public static String deleteById(ProviderContext providerContext) {
36 | return SqlScript.caching(providerContext, entity ->
37 | "DELETE FROM " + entity.tableName() +
38 | " WHERE " + entity.idColumns().stream().map(EntityColumn::columnEqualsProperty).collect(Collectors.joining(" AND ")));
39 | }
40 |
41 | public static String insertSelective(ProviderContext providerContext) {
42 | return SqlScript.caching(providerContext, (entity, util) ->
43 | "INSERT INTO " + entity.tableName()
44 | + util.trimSuffixOverrides("(", ")", ",", () ->
45 | entity.insertColumns().stream().map(column ->
46 | util.ifTest(column.notNullTest(), () -> column.column() + ",")
47 | ).collect(Collectors.joining(LF)))
48 | + util.trimSuffixOverrides(" VALUES (", ")", ",", () ->
49 | entity.insertColumns().stream().map(column ->
50 | util.ifTest(column.notNullTest(), () -> column.variables() + ",")
51 | ).collect(Collectors.joining(LF))));
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | import javax.persistence.Column;
22 | import javax.persistence.Id;
23 | import javax.persistence.Table;
24 |
25 | @javax.persistence.Entity
26 | @Table(name = "user")
27 | public class User {
28 | @Id
29 | private Long id;
30 | @Column(name = "name")
31 | private String username;
32 | @Column
33 | @Entity.Column(selectable = false)
34 | private String sex;
35 | @Entity.Transient
36 | private String password;
37 |
38 |
39 | public Long getId() {
40 | return id;
41 | }
42 |
43 | public void setId(Long id) {
44 | this.id = id;
45 | }
46 |
47 | public String getUsername() {
48 | return username;
49 | }
50 |
51 | public void setUsername(String username) {
52 | this.username = username;
53 | }
54 |
55 | public String getSex() {
56 | return sex;
57 | }
58 |
59 | public void setSex(String sex) {
60 | this.sex = sex;
61 | }
62 |
63 | public String getPassword() {
64 | return password;
65 | }
66 |
67 | public void setPassword(String password) {
68 | this.password = password;
69 | }
70 | }
71 |
72 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserAutoMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.InsertProvider;
21 | import org.apache.ibatis.annotations.Lang;
22 | import org.apache.ibatis.annotations.Options;
23 | import org.apache.ibatis.annotations.SelectProvider;
24 |
25 | public interface UserAutoMapper {
26 |
27 | @Lang(Caching.class)
28 | @SelectProvider(type = BaseProvider.class, method = "getById")
29 | UserAuto getById(Integer id);
30 |
31 |
32 | @Lang(Caching.class)
33 | @Options(useGeneratedKeys = true, keyProperty = "id")
34 | @InsertProvider(type = BaseProvider.class, method = "insertSelective")
35 | int insertSelective(UserAuto userAuto);
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserAutoMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.BaseTest;
20 | import org.apache.ibatis.session.SqlSession;
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 |
24 | public class UserAutoMapperTest extends BaseTest {
25 |
26 | @Test
27 | public void testSelectById() {
28 | try (SqlSession sqlSession = getSqlSession()) {
29 | UserAutoMapper userMapper = sqlSession.getMapper(UserAutoMapper.class);
30 | UserAuto user = userMapper.getById(1);
31 | Assert.assertNotNull(user);
32 | Assert.assertEquals("sjz", user.getUserName());
33 | Assert.assertNotNull(user.getAddress());
34 | Assert.assertEquals("河北省", user.getAddress().getSheng());
35 | Assert.assertEquals("石家庄市", user.getAddress().getShi());
36 |
37 | UserAuto.Address address = user.getAddress();
38 | address.setShi("秦皇岛市");
39 | user.setUserName("qhd");
40 | user.setId(null);
41 | userMapper.insertSelective(user);
42 | Assert.assertNotNull(user.getId());
43 |
44 | UserAuto qhd = userMapper.getById(user.getId());
45 | Assert.assertEquals("河北省", qhd.getAddress().getSheng());
46 | Assert.assertEquals("秦皇岛市", qhd.getAddress().getShi());
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserBaseMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.mapper.BaseMapper;
20 |
21 | import java.io.Serializable;
22 |
23 | public interface UserBaseMapper extends BaseMapper {
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserBaseMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.mapper.example.Example;
20 | import io.mybatis.provider.BaseTest;
21 | import org.apache.ibatis.session.SqlSession;
22 | import org.junit.Assert;
23 | import org.junit.Test;
24 |
25 | public class UserBaseMapperTest extends BaseTest {
26 |
27 | class UserExtend extends User {
28 | private String extend;
29 |
30 | public String getExtend() {
31 | return extend;
32 | }
33 |
34 | public void setExtend(String extend) {
35 | this.extend = extend;
36 | }
37 | }
38 |
39 | @Test
40 | public void testSelectById() {
41 | try (SqlSession sqlSession = getSqlSession()) {
42 | UserBaseMapper userMapper = sqlSession.getMapper(UserBaseMapper.class);
43 |
44 | User user = userMapper.selectByPrimaryKey(1L).get();
45 | Assert.assertNotNull(user);
46 | Assert.assertEquals("张无忌", user.getUsername());
47 | Assert.assertNull(user.getSex());
48 |
49 | user.setId(999L);
50 | Assert.assertEquals(1, userMapper.insert(user));
51 |
52 | int count = userMapper.deleteByPrimaryKey(user.getId());
53 | Assert.assertEquals(1, count);
54 | count = userMapper.deleteByPrimaryKey(user.getId());
55 | Assert.assertEquals(0, count);
56 | sqlSession.rollback();
57 | }
58 | }
59 |
60 | @Test
61 | public void testExtend() {
62 | try (SqlSession sqlSession = getSqlSession()) {
63 | UserBaseMapper userMapper = sqlSession.getMapper(UserBaseMapper.class);
64 | UserExtend userExtend = new UserExtend();
65 | userExtend.setId(999L);
66 | userExtend.setUsername("张无忌");
67 | userExtend.setExtend("扩展字段");
68 | Assert.assertEquals(1, userMapper.insert(userExtend));
69 |
70 | Example example = userMapper.example();
71 | example.createCriteria().andEqualTo(User::getId, userExtend.getId());
72 | userExtend.setUsername("test");
73 | Assert.assertEquals(1, userMapper.updateByExample(userExtend, example));
74 |
75 |
76 | int count = userMapper.deleteByPrimaryKey(userExtend.getId());
77 | Assert.assertEquals(1, count);
78 | sqlSession.rollback();
79 | }
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.DeleteProvider;
21 | import org.apache.ibatis.annotations.InsertProvider;
22 | import org.apache.ibatis.annotations.Lang;
23 | import org.apache.ibatis.annotations.SelectProvider;
24 |
25 | public interface UserMapper {
26 |
27 | @Lang(Caching.class)
28 | @SelectProvider(type = BaseProvider.class, method = "getById")
29 | User getById(Long id);
30 |
31 | @Lang(Caching.class)
32 | @InsertProvider(type = BaseProvider.class, method = "insertSelective")
33 | int insert(User user);
34 |
35 | @Lang(Caching.class)
36 | @DeleteProvider(type = BaseProvider.class, method = "deleteById")
37 | int deleteById(User user);
38 | }
39 |
--------------------------------------------------------------------------------
/jpa/src/test/java/io/mybatis/provider/jpa/UserMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.provider.jpa;
18 |
19 | import io.mybatis.provider.BaseTest;
20 | import org.apache.ibatis.session.SqlSession;
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 |
24 | public class UserMapperTest extends BaseTest {
25 |
26 | @Test
27 | public void testSelectById() {
28 | try (SqlSession sqlSession = getSqlSession()) {
29 | UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
30 |
31 | User user = userMapper.getById(1L);
32 | Assert.assertNotNull(user);
33 | Assert.assertEquals("张无忌", user.getUsername());
34 | Assert.assertNull(user.getSex());
35 |
36 | user.setId(999L);
37 | Assert.assertEquals(1, userMapper.insert(user));
38 |
39 | int count = userMapper.deleteById(user);
40 | Assert.assertEquals(1, count);
41 | sqlSession.rollback();
42 | }
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/jpa/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/jpa/src/test/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
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 |
--------------------------------------------------------------------------------
/jpa/src/test/resources/testdb.sql:
--------------------------------------------------------------------------------
1 | drop table user if exists;
2 | drop table user_auto if exists;
3 |
4 | create table user
5 | (
6 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
7 | name VARCHAR(32) DEFAULT 'DEFAULT',
8 | sex VARCHAR(2)
9 | );
10 |
11 |
12 | insert into user(id, name, sex)
13 | values (1, '张无忌', '男'),
14 | (2, '赵敏', '女'),
15 | (3, '周芷若', '女'),
16 | (4, '小昭', '女'),
17 | (5, '殷离', '女');
18 |
19 | -- 自动映射
20 | create table user_auto
21 | (
22 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
23 | user_name VARCHAR(32) DEFAULT 'DEFAULT',
24 | address VARCHAR(64)
25 | );
26 | insert into user_auto(id, user_name, address)
27 | values (1, 'sjz', '河北省/石家庄市'),
28 | (2, 'hd', '河北省/邯郸市'),
29 | (3, 'xt', '河北省/邢台市');
30 |
--------------------------------------------------------------------------------
/logo-bird.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mybatis-mapper/mapper/eaa78e8dbae41a4813768d55bb7e987c851aa8b0/logo-bird.png
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mybatis-mapper/mapper/eaa78e8dbae41a4813768d55bb7e987c851aa8b0/logo.png
--------------------------------------------------------------------------------
/mapper/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-mapper
29 |
30 |
31 |
32 | io.mybatis
33 | mybatis-provider
34 |
35 |
36 | io.mybatis
37 | mybatis-common
38 | ${project.version}
39 |
40 |
41 |
42 | com.github.pagehelper
43 | pagehelper
44 | 6.1.0
45 | test
46 |
47 |
48 | junit
49 | junit
50 |
51 |
52 | ch.qos.logback
53 | logback-classic
54 |
55 |
56 | org.hsqldb
57 | hsqldb
58 |
59 |
60 | com.h2database
61 | h2
62 |
63 |
64 |
65 |
66 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/Mapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.base.EntityProvider;
20 | import io.mybatis.provider.Caching;
21 | import org.apache.ibatis.annotations.InsertProvider;
22 | import org.apache.ibatis.annotations.Lang;
23 | import org.apache.ibatis.annotations.Options;
24 |
25 | import java.io.Serializable;
26 |
27 | /**
28 | * 自定义 Mapper 示例,这个 Mapper 基于主键自增重写了 insert 方法,主要用作示例
29 | *
56 | * 这个方法是个示例,你可以在自己的接口中使用相同的方式覆盖父接口中的配置
57 | *
58 | * @param entity 实体类
59 | * @return 1成功,0失败
60 | */
61 | @Override
62 | @Lang(Caching.class)
63 | //@SelectKey(statement = "SELECT SEQ.NEXTVAL FROM DUAL", keyProperty = "id", before = true, resultType = long.class)
64 | @Options(useGeneratedKeys = true, keyProperty = "id")
65 | @InsertProvider(type = EntityProvider.class, method = "insertSelective")
66 | int insertSelective(S entity);
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/config/MapperUserConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.config;
18 |
19 | import io.mybatis.config.defaults.UserConfig;
20 |
21 | /**
22 | * mybatis-mapper 用户配置,优先级高于版本配置,可以覆盖版本默认值
23 | *
24 | * 可以通过 io.mybatis.mapper.properties 属性指定自定义的配置路径或文件名
25 | *
26 | * @author liuzh
27 | */
28 | public class MapperUserConfig extends UserConfig {
29 |
30 | /**
31 | * 优先级高于 mybatis-provider 自定义配置
32 | */
33 | public int getOrder() {
34 | return USER_ORDER + 50;
35 | }
36 |
37 | @Override
38 | protected String getConfigKey() {
39 | return "io.mybatis.mapper.properties";
40 | }
41 |
42 | @Override
43 | protected String getConfigName() {
44 | return "mybatis-mapper";
45 | }
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/cursor/CursorMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.cursor;
18 |
19 | import io.mybatis.mapper.base.EntityProvider;
20 | import io.mybatis.mapper.example.ExampleProvider;
21 | import io.mybatis.provider.Caching;
22 | import org.apache.ibatis.annotations.Lang;
23 | import org.apache.ibatis.annotations.SelectProvider;
24 | import org.apache.ibatis.cursor.Cursor;
25 |
26 | /**
27 | * 游标查询方法
28 | *
29 | * @param 实体类
30 | * @param 符合Example数据结构的对象,例如 {@link io.mybatis.mapper.example.Example},也可以是 MBG 生成 XXXExample 对象。
31 | * @author liuzh
32 | */
33 | public interface CursorMapper {
34 |
35 | /**
36 | * 根据实体字段条件查询
37 | *
38 | * @param entity 实体类
39 | * @return 实体列表
40 | */
41 | @Lang(Caching.class)
42 | @SelectProvider(type = EntityProvider.class, method = "select")
43 | Cursor selectCursor(T entity);
44 |
45 | /**
46 | * 根据 Example 条件查询
47 | *
48 | * @param example 条件
49 | * @return 实体列表
50 | */
51 | @Lang(Caching.class)
52 | @SelectProvider(type = ExampleProvider.class, method = "selectByExample")
53 | Cursor selectCursorByExample(E example);
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/fn/FnMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.fn;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.Lang;
21 | import org.apache.ibatis.annotations.Param;
22 | import org.apache.ibatis.annotations.SelectProvider;
23 | import org.apache.ibatis.annotations.UpdateProvider;
24 |
25 | import java.util.List;
26 | import java.util.Optional;
27 |
28 | /**
29 | * 可指定字段的方法
30 | *
31 | * @param 实体类类型
32 | * @author liuzh
33 | */
34 | public interface FnMapper {
35 |
36 | /**
37 | * 根据主键更新实体中不为空的字段,强制字段不区分是否 null,都更新
38 | *
39 | * @param entity 实体类
40 | * @param forceUpdateFields 强制更新的字段,不区分字段是否为 null,通过 {@link Fn#of(Fn...)} 创建 {@link Fn.Fns}
41 | * @return 1成功,0失败
42 | */
43 | @Lang(Caching.class)
44 | @UpdateProvider(type = FnProvider.class, method = "updateByPrimaryKeySelectiveWithForceFields")
45 | int updateByPrimaryKeySelectiveWithForceFields(@Param("entity") S entity, @Param("fns") Fn.Fns forceUpdateFields);
46 |
47 | /**
48 | * 根据实体字段条件查询唯一的实体({@link io.mybatis.mapper.example.ExampleMapper} 可以实现一样的功能,当前方法只是示例)
49 | *
50 | * @param entity 实体类
51 | * @param selectFields 查询的字段,不区分字段是否为 null,通过 {@link Fn#of(Fn...)} 创建 {@link Fn.Fns}
52 | * @return 单个实体,查询结果由多条时报错
53 | */
54 | @Lang(Caching.class)
55 | @SelectProvider(type = FnProvider.class, method = "selectColumns")
56 | Optional selectColumnsOne(@Param("entity") T entity, @Param("fns") Fn.Fns selectFields);
57 |
58 | /**
59 | * 根据实体字段条件批量查询({@link io.mybatis.mapper.example.ExampleMapper} 可以实现一样的功能,当前方法只是示例)
60 | *
61 | * @param entity 实体类
62 | * @param selectFields 查询的字段,不区分字段是否为 null,通过 {@link Fn#of(Fn...)} 创建 {@link Fn.Fns}
63 | * @return 实体列表
64 | */
65 | @Lang(Caching.class)
66 | @SelectProvider(type = FnProvider.class, method = "selectColumns")
67 | List selectColumns(@Param("entity") T entity, @Param("fns") Fn.Fns selectFields);
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/list/ListMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.list;
18 |
19 | import io.mybatis.provider.Caching;
20 | import org.apache.ibatis.annotations.InsertProvider;
21 | import org.apache.ibatis.annotations.Lang;
22 | import org.apache.ibatis.annotations.Param;
23 | import org.apache.ibatis.annotations.UpdateProvider;
24 |
25 | import java.util.List;
26 |
27 | /**
28 | * 批量操作方法
29 | *
30 | * @param 实体类类型
31 | * @author liuzh
32 | */
33 | public interface ListMapper {
34 |
35 | /**
36 | * 批量保存实体,需要数据库支持批量插入的语法
37 | *
38 | * @param entityList 实体列表
39 | * @return 结果数等于 entityList.size() 时成功,不相等时失败
40 | */
41 | @Lang(Caching.class)
42 | @InsertProvider(type = ListProvider.class, method = "insertList")
43 | int insertList(@Param("entityList") List entityList);
44 |
45 | /**
46 | * 批量更新
47 | *
48 | * @author dengsd
49 | * @date 2022/9/27 11:49
50 | */
51 | @Lang(Caching.class)
52 | @UpdateProvider(type = ListProvider.class, method = "updateList")
53 | int updateList(@Param("entityList") List entityList);
54 |
55 |
56 | /**
57 | * 批量更新
58 | *
59 | * @author dengsd
60 | * @date 2022/9/27 11:49
61 | */
62 | @Lang(Caching.class)
63 | @UpdateProvider(type = ListProvider.class, method = "updateListSelective")
64 | int updateListSelective(@Param("entityList") List entityList);
65 | }
66 |
--------------------------------------------------------------------------------
/mapper/src/main/java/io/mybatis/mapper/logical/LogicalColumn.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.logical;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | /**
9 | * 标记该字段为逻辑状态列
10 | *
11 | *
12 | * @author hzw
13 | */
14 | @Retention(RetentionPolicy.RUNTIME)
15 | @Target(ElementType.FIELD)
16 | public @interface LogicalColumn {
17 | /**
18 | * 表示逻辑删除的值,比如null、0
19 | */
20 | String delete();
21 | }
22 |
--------------------------------------------------------------------------------
/mapper/src/main/resources/META-INF/services/io.mybatis.config.Config:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2020-2022 the original author or authors.
3 | #
4 | # Licensed under the Apache License, Version 2.0 (the "License");
5 | # you may not use this file except in compliance with the License.
6 | # You may obtain a copy of the License at
7 | #
8 | # http://www.apache.org/licenses/LICENSE-2.0
9 | #
10 | # Unless required by applicable law or agreed to in writing, software
11 | # distributed under the License is distributed on an "AS IS" BASIS,
12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | # See the License for the specific language governing permissions and
14 | # limitations under the License.
15 | #
16 |
17 | io.mybatis.mapper.config.MapperUserConfig
18 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/BaseMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import org.apache.ibatis.io.Resources;
20 | import org.apache.ibatis.jdbc.ScriptRunner;
21 | import org.apache.ibatis.session.SqlSession;
22 | import org.apache.ibatis.session.SqlSessionFactory;
23 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24 | import org.junit.BeforeClass;
25 |
26 | import java.io.IOException;
27 | import java.io.Reader;
28 | import java.sql.Connection;
29 |
30 | public class BaseMapperTest {
31 | private static SqlSessionFactory sqlSessionFactory;
32 |
33 | @BeforeClass
34 | public static void init() {
35 | if (sqlSessionFactory == null) {
36 | try {
37 | Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
38 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39 | reader.close();
40 |
41 | //创建数据库
42 | SqlSession session = null;
43 | try {
44 | session = sqlSessionFactory.openSession();
45 | Connection conn = session.getConnection();
46 | reader = Resources.getResourceAsReader("testdb.sql");
47 | ScriptRunner runner = new ScriptRunner(conn);
48 | runner.setLogWriter(null);
49 | runner.runScript(reader);
50 | reader.close();
51 | } finally {
52 | if (session != null) {
53 | session.close();
54 | }
55 | }
56 | } catch (IOException ignore) {
57 | ignore.printStackTrace();
58 | }
59 | }
60 | }
61 |
62 | public SqlSession getSqlSession() {
63 | return sqlSessionFactory.openSession();
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/H2BaseMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import org.apache.ibatis.io.Resources;
20 | import org.apache.ibatis.jdbc.ScriptRunner;
21 | import org.apache.ibatis.session.SqlSession;
22 | import org.apache.ibatis.session.SqlSessionFactory;
23 | import org.apache.ibatis.session.SqlSessionFactoryBuilder;
24 | import org.junit.BeforeClass;
25 |
26 | import java.io.IOException;
27 | import java.io.Reader;
28 | import java.sql.Connection;
29 |
30 | public class H2BaseMapperTest {
31 | private static SqlSessionFactory sqlSessionFactory;
32 |
33 | @BeforeClass
34 | public static void init() {
35 | if (sqlSessionFactory == null) {
36 | try {
37 | Reader reader = Resources.getResourceAsReader("mybatis-config-h2.xml");
38 | sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
39 | reader.close();
40 |
41 | //创建数据库
42 | SqlSession session = null;
43 | try {
44 | session = sqlSessionFactory.openSession();
45 | Connection conn = session.getConnection();
46 | reader = Resources.getResourceAsReader("testdb-h2.sql");
47 | ScriptRunner runner = new ScriptRunner(conn);
48 | runner.setLogWriter(null);
49 | runner.runScript(reader);
50 | reader.close();
51 | } finally {
52 | if (session != null) {
53 | session.close();
54 | }
55 | }
56 | } catch (IOException ignore) {
57 | ignore.printStackTrace();
58 | }
59 | }
60 | }
61 |
62 | public SqlSession getSqlSession() {
63 | return sqlSessionFactory.openSession();
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/SumMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.fn.Fn;
20 | import io.mybatis.provider.Caching;
21 | import io.mybatis.provider.EntityTable;
22 | import io.mybatis.provider.SqlScript;
23 | import org.apache.ibatis.annotations.Lang;
24 | import org.apache.ibatis.annotations.Param;
25 | import org.apache.ibatis.annotations.SelectProvider;
26 | import org.apache.ibatis.builder.annotation.ProviderContext;
27 |
28 | import java.util.stream.Collectors;
29 |
30 | public interface SumMapper {
31 |
32 | /**
33 | * 根据 entity 查询条件,查询 sum(column) 总数
34 | *
35 | * @param column 指定的查询列
36 | * @param entity 查询条件
37 | * @return 总数
38 | */
39 | @Lang(Caching.class)
40 | @SelectProvider(type = SumMapperProvider.class, method = "sum")
41 | long sum(@Param("column") Fn column, @Param("entity") T entity);
42 |
43 | class SumMapperProvider {
44 | public static String sum(ProviderContext providerContext) {
45 | return SqlScript.caching(providerContext, new SqlScript() {
46 | @Override
47 | public String getSql(EntityTable entity) {
48 | return "SELECT SUM(${column.toColumn()}) FROM " + entity.tableName()
49 | + ifTest("entity != null", () ->
50 | where(() ->
51 | entity.whereColumns().stream().map(column ->
52 | ifTest(column.notNullTest("entity."), () -> "AND " + column.columnEqualsProperty("entity."))
53 | ).collect(Collectors.joining(LF)))
54 | );
55 | }
56 | });
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/TestBatchUpdateMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.base.EntityMapper;
20 | import io.mybatis.mapper.list.ListMapper;
21 | import io.mybatis.mapper.model.UserIds;
22 |
23 |
24 | public interface TestBatchUpdateMapper extends ListMapper, EntityMapper {
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/UserAutoBaseMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.model.UserAuto;
20 |
21 | public interface UserAutoBaseMapper extends BaseMapper{
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/UserAutoMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.base.EntityMapper;
20 | import io.mybatis.mapper.base.EntityProvider;
21 | import io.mybatis.mapper.cursor.CursorMapper;
22 | import io.mybatis.mapper.example.Example;
23 | import io.mybatis.mapper.example.ExampleMapper;
24 | import io.mybatis.mapper.fn.FnMapper;
25 | import io.mybatis.mapper.list.ListMapper;
26 | import io.mybatis.mapper.model.UserAuto;
27 | import io.mybatis.provider.Caching;
28 | import org.apache.ibatis.annotations.InsertProvider;
29 | import org.apache.ibatis.annotations.Lang;
30 | import org.apache.ibatis.annotations.SelectKey;
31 |
32 | public interface UserAutoMapper extends
33 | EntityMapper,
34 | CursorMapper>,
35 | FnMapper,
36 | ExampleMapper>,
37 | ListMapper,
38 | SumMapper {
39 |
40 |
41 | @Override
42 | @Lang(Caching.class)
43 | @SelectKey(statement = "CALL IDENTITY()", keyProperty = "id", resultType = Long.class, before = false)
44 | @InsertProvider(type = EntityProvider.class, method = "insertSelective")
45 | int insertSelective(UserAuto entity);
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/UserIdsMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.base.EntityMapper;
20 | import io.mybatis.mapper.base.EntityProvider;
21 | import io.mybatis.mapper.cursor.CursorMapper;
22 | import io.mybatis.mapper.example.Example;
23 | import io.mybatis.mapper.example.ExampleMapper;
24 | import io.mybatis.mapper.fn.FnMapper;
25 | import io.mybatis.mapper.list.ListMapper;
26 | import io.mybatis.mapper.model.UserIds;
27 | import io.mybatis.provider.Caching;
28 | import org.apache.ibatis.annotations.InsertProvider;
29 | import org.apache.ibatis.annotations.Lang;
30 | import org.apache.ibatis.annotations.Options;
31 |
32 | public interface UserIdsMapper extends
33 | EntityMapper,
34 | FnMapper,
35 | CursorMapper>,
36 | ExampleMapper>,
37 | ListMapper {
38 |
39 | @Override
40 | @Lang(Caching.class)
41 | @Options(useGeneratedKeys = true, keyProperty = "id")
42 | @InsertProvider(type = EntityProvider.class, method = "insert")
43 | int insert(UserIds entity);
44 |
45 | @Override
46 | @Lang(Caching.class)
47 | @InsertProvider(type = EntityProvider.class, method = "insertSelective")
48 | int insertSelective(UserIds entity);
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.base.EntityProvider;
20 | import io.mybatis.mapper.example.Example;
21 | import io.mybatis.mapper.list.ListMapper;
22 | import io.mybatis.mapper.logical.LogicalMapper;
23 | import io.mybatis.mapper.model.User;
24 | import io.mybatis.provider.Caching;
25 | import org.apache.ibatis.annotations.InsertProvider;
26 | import org.apache.ibatis.annotations.Lang;
27 | import org.apache.ibatis.annotations.SelectKey;
28 | import org.apache.ibatis.annotations.SelectProvider;
29 |
30 | import java.util.List;
31 |
32 | public interface UserMapper extends
33 | ListMapper,
34 | LogicalMapper,
35 | SumMapper {
36 |
37 | @Override
38 | @Lang(Caching.class)
39 | @InsertProvider(type = EntityProvider.class, method = "insert")
40 | int insert(User entity);
41 |
42 | @Override
43 | @Lang(Caching.class)
44 | @SelectKey(statement = "CALL IDENTITY()", keyProperty = "id", resultType = Long.class, before = false)
45 | @InsertProvider(type = EntityProvider.class, method = "insertSelective")
46 | int insertSelective(User entity);
47 |
48 | default int deleteByIds(List ids) {
49 | Example example = new Example<>();
50 | example.createCriteria().andIn(User::getId, ids);
51 | return deleteByExample(example);
52 | }
53 |
54 | /**
55 | * 不支持的方法
56 | */
57 | @SelectProvider(type = EntityProvider.class, method = "unsupported")
58 | int unsupported();
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/UserMapper2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper;
18 |
19 | import io.mybatis.mapper.model.User;
20 |
21 | public interface UserMapper2 extends Mapper {
22 |
23 | }
24 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/cursor/UserCursorMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.cursor;
18 |
19 | import io.mybatis.mapper.BaseMapperTest;
20 | import io.mybatis.mapper.UserMapper;
21 | import io.mybatis.mapper.example.Example;
22 | import io.mybatis.mapper.model.User;
23 | import org.apache.ibatis.cursor.Cursor;
24 | import org.apache.ibatis.session.SqlSession;
25 | import org.junit.Assert;
26 | import org.junit.Test;
27 |
28 | import java.util.Iterator;
29 |
30 | public class UserCursorMapperTest extends BaseMapperTest {
31 |
32 | @Test
33 | public void testSelectCursor() {
34 | SqlSession sqlSession = getSqlSession();
35 | try {
36 | CursorMapper> mapper = sqlSession.getMapper(UserMapper.class);
37 | User user = new User();
38 | user.setSex("女");
39 | Cursor userCursor = mapper.selectCursor(user);
40 | Iterator userIterator = userCursor.iterator();
41 | int count = 0;
42 | while (userIterator.hasNext()) {
43 | count++;
44 | User u = userIterator.next();
45 | System.out.println(u.getUserName());
46 | Assert.assertEquals(count, userCursor.getCurrentIndex() + 1);
47 | }
48 | Assert.assertEquals(16, count);
49 | Assert.assertTrue(userCursor.isConsumed());
50 | } finally {
51 | //不要忘记关闭sqlSession
52 | sqlSession.close();
53 | }
54 | }
55 |
56 | @Test
57 | public void testSelectCursorByExample() {
58 | SqlSession sqlSession = getSqlSession();
59 | try {
60 | CursorMapper> mapper = sqlSession.getMapper(UserMapper.class);
61 | Example example = new Example<>();
62 | example.createCriteria().andEqualTo(User::getSex, "女").andLessThan(User::getId, 10);
63 | Cursor userCursor = mapper.selectCursorByExample(example);
64 | Iterator userIterator = userCursor.iterator();
65 | int count = 0;
66 | while (userIterator.hasNext()) {
67 | count++;
68 | User u = userIterator.next();
69 | System.out.println(u.getUserName());
70 | Assert.assertEquals(count, userCursor.getCurrentIndex() + 1);
71 | }
72 | Assert.assertEquals(5, count);
73 | Assert.assertTrue(userCursor.isConsumed());
74 | } finally {
75 | //不要忘记关闭sqlSession
76 | sqlSession.close();
77 | }
78 | }
79 |
80 | }
81 |
82 |
83 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/cursor/UserIdsCursorMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.cursor;
18 |
19 | import io.mybatis.mapper.BaseMapperTest;
20 | import io.mybatis.mapper.UserIdsMapper;
21 | import io.mybatis.mapper.example.Example;
22 | import io.mybatis.mapper.model.UserIds;
23 | import org.apache.ibatis.cursor.Cursor;
24 | import org.apache.ibatis.session.SqlSession;
25 | import org.junit.Assert;
26 | import org.junit.Test;
27 |
28 | import java.util.Iterator;
29 |
30 | public class UserIdsCursorMapperTest extends BaseMapperTest {
31 |
32 | @Test
33 | public void testSelectCursor() {
34 | SqlSession sqlSession = getSqlSession();
35 | try {
36 | CursorMapper> mapper = sqlSession.getMapper(UserIdsMapper.class);
37 | UserIds user = new UserIds();
38 | user.setId1(1L);
39 | Cursor userCursor = mapper.selectCursor(user);
40 | Iterator userIterator = userCursor.iterator();
41 | int count = 0;
42 | while (userIterator.hasNext()) {
43 | count++;
44 | UserIds u = userIterator.next();
45 | System.out.println(u.getName());
46 | Assert.assertEquals(count, userCursor.getCurrentIndex() + 1);
47 | }
48 | Assert.assertEquals(4, count);
49 | Assert.assertTrue(userCursor.isConsumed());
50 | } finally {
51 | //不要忘记关闭sqlSession
52 | sqlSession.close();
53 | }
54 | }
55 |
56 | @Test
57 | public void testSelectCursorByExample() {
58 | SqlSession sqlSession = getSqlSession();
59 | try {
60 | CursorMapper> mapper = sqlSession.getMapper(UserIdsMapper.class);
61 | Example example = new Example<>();
62 | example.createCriteria().andEqualTo(UserIds::getId1, 1).andLessThan(UserIds::getId2, 4);
63 | Cursor userCursor = mapper.selectCursorByExample(example);
64 | Iterator userIterator = userCursor.iterator();
65 | int count = 0;
66 | while (userIterator.hasNext()) {
67 | count++;
68 | UserIds u = userIterator.next();
69 | System.out.println(u.getName());
70 | Assert.assertEquals(count, userCursor.getCurrentIndex() + 1);
71 | }
72 | Assert.assertEquals(3, count);
73 | Assert.assertTrue(userCursor.isConsumed());
74 | } finally {
75 | //不要忘记关闭sqlSession
76 | sqlSession.close();
77 | }
78 | }
79 |
80 | }
81 |
82 |
83 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/CommonDateBase.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | import java.time.LocalDateTime;
4 |
5 | public class CommonDateBase {
6 | LocalDateTime insertTime = LocalDateTime.now();
7 | LocalDateTime updateTime = LocalDateTime.now();
8 | }
9 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/CommonEntity.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | public class CommonEntity extends CommonEntityUuidId {
4 | String ahId;
5 | //xxx
6 | }
7 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/CommonEntityUuidId.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | import io.mybatis.provider.Entity;
4 |
5 | public class CommonEntityUuidId extends CommonDateBase {
6 | @Entity.Column(id = true, updatable = false)
7 | private String dataId;
8 |
9 | public String getDataId() {
10 | return dataId;
11 | }
12 |
13 | public void setDataId(String dataId) {
14 | this.dataId = dataId;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/Item.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | import io.mybatis.provider.Entity;
4 |
5 | @Entity.Table(value = "yahoo_items")
6 | public class Item extends CommonEntity {
7 | String itemId;
8 | // xxx
9 |
10 | public String getItemId() {
11 | return itemId;
12 | }
13 |
14 | public void setItemId(String itemId) {
15 | this.itemId = itemId;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/ItemMapper.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | import io.mybatis.mapper.Mapper;
4 |
5 | public interface ItemMapper extends Mapper {
6 |
7 | }
8 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/issues119/ItemTest.java:
--------------------------------------------------------------------------------
1 | package io.mybatis.mapper.issues119;
2 |
3 | import io.mybatis.mapper.BaseMapperTest;
4 | import org.apache.ibatis.session.SqlSession;
5 | import org.junit.Assert;
6 | import org.junit.Test;
7 |
8 | public class ItemTest extends BaseMapperTest {
9 |
10 | @Test
11 | public void testUpdateByPrimaryKeySelective() {
12 | SqlSession sqlSession = getSqlSession();
13 | try {
14 | ItemMapper itemMapper = sqlSession.getMapper(ItemMapper.class);
15 | Item item = new Item();
16 | item.setItemId("123");
17 | item.setDataId("456");
18 | Assert.assertEquals(0, itemMapper.updateByPrimaryKeySelective(item));
19 | sqlSession.rollback();
20 | } finally {
21 | //不要忘记关闭sqlSession
22 | sqlSession.close();
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/list/BatchUpdateMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.list;
18 |
19 | import io.mybatis.mapper.H2BaseMapperTest;
20 | import io.mybatis.mapper.TestBatchUpdateMapper;
21 | import io.mybatis.mapper.model.UserIds;
22 | import org.apache.ibatis.session.SqlSession;
23 | import org.junit.Assert;
24 | import org.junit.Test;
25 |
26 | import java.util.ArrayList;
27 | import java.util.List;
28 |
29 | public class BatchUpdateMapperTest extends H2BaseMapperTest {
30 |
31 | @Test
32 | public void testUpdateList() {
33 | SqlSession sqlSession = getSqlSession();
34 | try {
35 | TestBatchUpdateMapper batchUpdateUserIdsMapper = sqlSession.getMapper(TestBatchUpdateMapper.class);
36 | List users = new ArrayList<>(10);
37 | for (int i = 0; i < 2; i++) {
38 | UserIds user = new UserIds();
39 | user.setId1(1L);
40 | user.setId2(i + 1L);
41 | user.setName("测试" + i);
42 | users.add(user);
43 | }
44 | Assert.assertEquals(2, batchUpdateUserIdsMapper.updateList(users));
45 | sqlSession.rollback();
46 | } finally {
47 | //不要忘记关闭sqlSession
48 | sqlSession.close();
49 | }
50 | }
51 |
52 | @Test
53 | public void testUpdateListSelective() {
54 | SqlSession sqlSession = getSqlSession();
55 | try {
56 | TestBatchUpdateMapper batchUpdateUserIdsMapper = sqlSession.getMapper(TestBatchUpdateMapper.class);
57 |
58 | List users = new ArrayList<>(10);
59 | UserIds user3 = new UserIds();
60 | user3.setId1(1L);
61 | user3.setId2(3L);
62 | user3.setName(null);
63 | users.add(user3);
64 | UserIds beforeData = batchUpdateUserIdsMapper.selectByPrimaryKey(user3).get();
65 | for (int i = 0; i < 2; i++) {
66 | UserIds user = new UserIds();
67 | user.setId1(1L);
68 | user.setId2(i + 1L);
69 | user.setName("测试" + i);
70 | users.add(user);
71 | }
72 |
73 | Assert.assertEquals(3, batchUpdateUserIdsMapper.updateListSelective(users));
74 | UserIds afterData = batchUpdateUserIdsMapper.selectByPrimaryKey(user3).get();
75 | Assert.assertEquals(beforeData.getName(), afterData.getName());
76 | sqlSession.rollback();
77 | } finally {
78 | //不要忘记关闭sqlSession
79 | sqlSession.close();
80 | }
81 | }
82 | }
83 |
84 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/list/UserIdsListMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.list;
18 |
19 | import io.mybatis.mapper.BaseMapperTest;
20 | import io.mybatis.mapper.UserIdsMapper;
21 | import io.mybatis.mapper.model.UserIds;
22 | import org.apache.ibatis.session.SqlSession;
23 | import org.junit.Assert;
24 | import org.junit.Test;
25 |
26 | import java.util.ArrayList;
27 | import java.util.List;
28 |
29 | public class UserIdsListMapperTest extends BaseMapperTest {
30 |
31 | @Test
32 | public void testInsertList() {
33 | SqlSession sqlSession = getSqlSession();
34 | try {
35 | UserIdsMapper insertListMapper = sqlSession.getMapper(UserIdsMapper.class);
36 | List users = new ArrayList<>(10);
37 | for (int i = 0; i < 10; i++) {
38 | UserIds user = new UserIds();
39 | //注释后使用genId自动生成,测试批量插入genId
40 | //user.setId1(2L);
41 | user.setId2((long) i);
42 | user.setName("测试" + i);
43 | users.add(user);
44 | }
45 | Assert.assertEquals(10, insertListMapper.insertList(users));
46 | sqlSession.rollback();
47 | } finally {
48 | //不要忘记关闭sqlSession
49 | sqlSession.close();
50 | }
51 | }
52 |
53 | }
54 |
55 |
56 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/list/UserListMapperTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.list;
18 |
19 | import io.mybatis.mapper.BaseMapperTest;
20 | import io.mybatis.mapper.UserMapper;
21 | import io.mybatis.mapper.model.User;
22 | import org.apache.ibatis.session.SqlSession;
23 | import org.junit.Assert;
24 | import org.junit.Test;
25 |
26 | import java.util.ArrayList;
27 | import java.util.List;
28 |
29 | public class UserListMapperTest extends BaseMapperTest {
30 |
31 | @Test
32 | public void testInsertList() {
33 | SqlSession sqlSession = getSqlSession();
34 | try {
35 | UserMapper insertListMapper = sqlSession.getMapper(UserMapper.class);
36 | List users = new ArrayList<>(10);
37 | for (int i = 0; i < 10; i++) {
38 | User user = new User();
39 | user.setUserName("测试" + i);
40 | users.add(user);
41 | }
42 | Assert.assertEquals(10, insertListMapper.insertList(users));
43 | users.stream().map(User::getId).forEach(System.out::println);
44 | sqlSession.rollback();
45 | } finally {
46 | //不要忘记关闭sqlSession
47 | sqlSession.close();
48 | }
49 | }
50 |
51 | }
52 |
53 |
54 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/model/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.model;
18 |
19 | import io.mybatis.mapper.logical.LogicalColumn;
20 | import io.mybatis.provider.Entity;
21 |
22 | @Entity.Table(value = "user",
23 | props = {
24 | @Entity.Prop(name = "deleteByExample.allowEmpty", value = "false"),
25 | @Entity.Prop(name = "updateByExample.allowEmpty", value = "false"),
26 | @Entity.Prop(name = "updateByExampleSelective.allowEmpty", value = "false")
27 | }
28 | )
29 | public class User {
30 | @Entity.Column(id = true, useGeneratedKeys = true)
31 | private Long id;
32 | @Entity.Column("name")
33 | private String userName;
34 | @Entity.Column
35 | private String sex;
36 | @LogicalColumn(delete = "0")
37 | @Entity.Column(updatable = false, insertable = false)
38 | private Boolean status;
39 |
40 | public Long getId() {
41 | return id;
42 | }
43 |
44 | public void setId(Long id) {
45 | this.id = id;
46 | }
47 |
48 | public String getUserName() {
49 | return userName;
50 | }
51 |
52 | public void setUserName(String userName) {
53 | this.userName = userName;
54 | }
55 |
56 | public String getSex() {
57 | return sex;
58 | }
59 |
60 | public void setSex(String sex) {
61 | this.sex = sex;
62 | }
63 |
64 | public Boolean getStatus() {
65 | return status;
66 | }
67 |
68 | public void setStatus(Boolean status) {
69 | this.status = status;
70 | }
71 |
72 | @Override
73 | public String toString() {
74 | return "User{" +
75 | "id=" + id +
76 | ", userName='" + userName + '\'' +
77 | ", sex='" + sex + '\'' +
78 | ", status=" + status +
79 | '}';
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/model/UserIds.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.model;
18 |
19 | import io.mybatis.provider.Entity;
20 | import io.mybatis.provider.EntityColumn;
21 | import io.mybatis.provider.EntityTable;
22 | import io.mybatis.provider.keysql.GenId;
23 |
24 | import java.util.concurrent.atomic.AtomicLong;
25 |
26 | @Entity.Table("user_ids")
27 | public class UserIds {
28 |
29 | public static class GenUserId implements GenId {
30 | AtomicLong index = new AtomicLong(10000);
31 | @Override
32 | public Long genId(EntityTable table, EntityColumn column) {
33 | return index.incrementAndGet();
34 | }
35 | }
36 |
37 | @Entity.Column(id = true, genIdExecuteBefore = true, genId = GenUserId.class)
38 | private Long id1;
39 | @Entity.Column(id = true)
40 | private Long id2;
41 | @Entity.Column
42 | private String name;
43 |
44 | public UserIds() {
45 | }
46 |
47 | public UserIds(Long id1, Long id2) {
48 | this.id1 = id1;
49 | this.id2 = id2;
50 | }
51 |
52 | public UserIds(Long id1, Long id2, String name) {
53 | this.id1 = id1;
54 | this.id2 = id2;
55 | this.name = name;
56 | }
57 |
58 | public Long getId1() {
59 | return id1;
60 | }
61 |
62 | public void setId1(Long id1) {
63 | this.id1 = id1;
64 | }
65 |
66 | public Long getId2() {
67 | return id2;
68 | }
69 |
70 | public void setId2(Long id2) {
71 | this.id2 = id2;
72 | }
73 |
74 | public String getName() {
75 | return name;
76 | }
77 |
78 | public void setName(String name) {
79 | this.name = name;
80 | }
81 |
82 | @Override
83 | public String toString() {
84 | return "UserIds{" +
85 | "id1=" + id1 +
86 | ", id2=" + id2 +
87 | ", name='" + name + '\'' +
88 | '}';
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/mapper/src/test/java/io/mybatis/mapper/model/UserIs.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.mapper.model;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | @Entity.Table
22 | public class UserIs {
23 |
24 | @Entity.Column(id = true)
25 | private Long id;
26 |
27 | @Entity.Column("is_admin")
28 | private boolean admin;
29 |
30 | public Long getId() {
31 | return id;
32 | }
33 |
34 | public void setId(Long id) {
35 | this.id = id;
36 | }
37 |
38 | public boolean isAdmin() {
39 | return admin;
40 | }
41 |
42 | public void setAdmin(boolean admin) {
43 | this.admin = admin;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/mapper/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/mapper/src/test/resources/mybatis-config-h2.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
--------------------------------------------------------------------------------
/mapper/src/test/resources/mybatis-config.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
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 |
--------------------------------------------------------------------------------
/mapper/src/test/resources/testdb-h2.sql:
--------------------------------------------------------------------------------
1 | drop table if exists user;
2 | drop table if exists user_ids;
3 | drop table if exists user_auto;
4 |
5 | create table user
6 | (
7 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
8 | name VARCHAR(32) DEFAULT 'DEFAULT',
9 | sex VARCHAR(2),
10 | status INTEGER DEFAULT 1
11 | );
12 |
13 | insert into user(id, name, sex, status)
14 | values (1, '张无忌', '男', 1),
15 | (2, '赵敏', '女', 1),
16 | (3, '周芷若', '女', 1),
17 | (4, '小昭', '女', 1),
18 | (5, '殷离', '女', 1),
19 | (6, '张翠山', '男', 1),
20 | (7, '殷素素', '女', 1),
21 | (8, '金毛狮王', '男', 1),
22 | (9, '张三丰', '男', 1),
23 | (10, '宋远桥', '男', 1),
24 | (11, '俞莲舟', '男', 1),
25 | (12, '俞岱岩', '男', 1),
26 | (13, '张松溪', '男', 1),
27 | (14, '殷梨亭', '男', 1),
28 | (15, '莫声谷', '男', 1),
29 | (16, '纪晓芙', '女', 1),
30 | (17, '成昆', '男', 1),
31 | (18, '杨逍', '男', 1),
32 | (19, '范遥', '男', 1),
33 | (20, '殷天正', '男', 1),
34 | (21, '殷野王', '男', 1),
35 | (22, '黛绮丝', '女', 1),
36 | (23, '灭绝师太', '女', 1),
37 | (24, '韦一笑', '男', 1),
38 | (25, '周颠', '男', 1),
39 | (26, '说不得', '男', 1),
40 | (27, '谦卑', '男', 1),
41 | (28, '彭莹玉', '男', 1),
42 | (29, '常遇春', '男', 1),
43 | (30, '胡青牛', '男', 1),
44 | (31, '王难姑', '女', 1),
45 | (32, '朱元璋', '男', 1),
46 | (33, '杨不悔', '女', 1),
47 | (34, '鹿杖客', '男', 1),
48 | (35, '鹤笔翁', '男', 1),
49 | (36, '丁敏君', '女', 1),
50 | (37, '宋青书', '男', 1),
51 | (38, '何太冲', '男', 1),
52 | (39, '朱长龄', '男', 1),
53 | (40, '朱九真', '女', 1),
54 | (41, '武青婴', '女', 1),
55 | (42, '卫璧', '男', 1),
56 | (43, '汝阳王', '男', 1),
57 | (44, '王保保', '男', 1),
58 | (45, '觉远', '男', 1),
59 | (46, '郭襄', '女', 1),
60 | (47, '张君宝', '男', 1),
61 | (48, '何足道', '男', 1),
62 | (49, '都大锦', '男', 1),
63 | (50, '韩姬', '女', 1),
64 | (51, '黄衫女子', '女', NULL),
65 | (52, '陈友谅', '男', 1),
66 | (53, '韩千叶', '男', 1);
67 |
68 | -- 联合主键
69 | create table user_ids
70 | (
71 | id1 INTEGER,
72 | id2 INTEGER,
73 | name VARCHAR(32) DEFAULT 'DEFAULT',
74 | PRIMARY KEY (id1, id2)
75 | );
76 |
77 | insert into user_ids(id1, id2, name)
78 | values (1, 1, '张无忌1'),
79 | (1, 2, '张无忌2'),
80 | (1, 3, '张无忌3'),
81 | (1, 4, '张无忌4');
82 |
83 | -- 自动映射
84 | create table user_auto
85 | (
86 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
87 | user_name VARCHAR(32) DEFAULT 'DEFAULT',
88 | address VARCHAR(64)
89 | );
90 | insert into user_auto(id, user_name, address)
91 | values (1, 'sjz', '河北省/石家庄市'),
92 | (2, 'hd', '河北省/邯郸市'),
93 | (3, 'xt', '河北省/邢台市');
94 |
95 | CREATE TABLE yahoo_items (
96 | data_id VARCHAR(255) NOT NULL,
97 | item_id VARCHAR(255) NOT NULL,
98 | insert_time TIMESTAMP,
99 | update_time TIMESTAMP,
100 | PRIMARY KEY (data_id)
101 | );
102 |
--------------------------------------------------------------------------------
/mapper/src/test/resources/testdb.sql:
--------------------------------------------------------------------------------
1 | drop table user if exists;
2 | drop table user_ids if exists;
3 | drop table user_auto if exists;
4 |
5 | create table user
6 | (
7 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
8 | name VARCHAR(32) DEFAULT 'DEFAULT',
9 | sex VARCHAR(2),
10 | status INTEGER DEFAULT 1
11 | );
12 |
13 | insert into user(id, name, sex, status)
14 | values (1, '张无忌', '男', 1),
15 | (2, '赵敏', '女', 1),
16 | (3, '周芷若', '女', 1),
17 | (4, '小昭', '女', 1),
18 | (5, '殷离', '女', 1),
19 | (6, '张翠山', '男', 1),
20 | (7, '殷素素', '女', 1),
21 | (8, '金毛狮王', '男', 1),
22 | (9, '张三丰', '男', 1),
23 | (10, '宋远桥', '男', 1),
24 | (11, '俞莲舟', '男', 1),
25 | (12, '俞岱岩', '男', 1),
26 | (13, '张松溪', '男', 1),
27 | (14, '殷梨亭', '男', 1),
28 | (15, '莫声谷', '男', 1),
29 | (16, '纪晓芙', '女', 1),
30 | (17, '成昆', '男', 1),
31 | (18, '杨逍', '男', 1),
32 | (19, '范遥', '男', 1),
33 | (20, '殷天正', '男', 1),
34 | (21, '殷野王', '男', 1),
35 | (22, '黛绮丝', '女', 1),
36 | (23, '灭绝师太', '女', 1),
37 | (24, '韦一笑', '男', 1),
38 | (25, '周颠', '男', 1),
39 | (26, '说不得', '男', 1),
40 | (27, '谦卑', '男', 1),
41 | (28, '彭莹玉', '男', 1),
42 | (29, '常遇春', '男', 1),
43 | (30, '胡青牛', '男', 1),
44 | (31, '王难姑', '女', 1),
45 | (32, '朱元璋', '男', 1),
46 | (33, '杨不悔', '女', 1),
47 | (34, '鹿杖客', '男', 1),
48 | (35, '鹤笔翁', '男', 1),
49 | (36, '丁敏君', '女', 1),
50 | (37, '宋青书', '男', 1),
51 | (38, '何太冲', '男', 1),
52 | (39, '朱长龄', '男', 1),
53 | (40, '朱九真', '女', 1),
54 | (41, '武青婴', '女', 1),
55 | (42, '卫璧', '男', 1),
56 | (43, '汝阳王', '男', 1),
57 | (44, '王保保', '男', 1),
58 | (45, '觉远', '男', 1),
59 | (46, '郭襄', '女', 1),
60 | (47, '张君宝', '男', 1),
61 | (48, '何足道', '男', 1),
62 | (49, '都大锦', '男', 1),
63 | (50, '韩姬', '女', 1),
64 | (51, '黄衫女子', '女', 1),
65 | (52, '陈友谅', '男', 1),
66 | (53, '韩千叶', '男', 1);
67 |
68 | -- 联合主键
69 | create table user_ids
70 | (
71 | id1 INTEGER,
72 | id2 INTEGER,
73 | name VARCHAR(32) DEFAULT 'DEFAULT',
74 | PRIMARY KEY (id1, id2)
75 | );
76 |
77 | insert into user_ids(id1, id2, name)
78 | values (1, 1, '张无忌1'),
79 | (1, 2, '张无忌2'),
80 | (1, 3, '张无忌3'),
81 | (1, 4, '张无忌4');
82 |
83 | -- 自动映射
84 | create table user_auto
85 | (
86 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
87 | user_name VARCHAR(32) DEFAULT 'DEFAULT',
88 | address VARCHAR(64)
89 | );
90 | insert into user_auto(id, user_name, address)
91 | values (1, 'sjz', '河北省/石家庄市'),
92 | (2, 'hd', '河北省/邯郸市'),
93 | (3, 'xt', '河北省/邢台市');
94 |
95 | CREATE TABLE yahoo_items (
96 | data_id VARCHAR(255) NOT NULL,
97 | item_id VARCHAR(255) NOT NULL,
98 | insert_time TIMESTAMP,
99 | update_time TIMESTAMP,
100 | PRIMARY KEY (data_id)
101 | );
102 |
--------------------------------------------------------------------------------
/service/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
21 |
22 | mybatis-parent
23 | io.mybatis
24 | ${revision}
25 |
26 | 4.0.0
27 |
28 | mybatis-service
29 |
30 |
31 |
32 | io.mybatis
33 | mybatis-mapper
34 | ${project.version}
35 |
36 |
37 | io.mybatis
38 | mybatis-common
39 | ${project.version}
40 |
41 |
42 | org.mybatis
43 | mybatis-spring
44 |
45 |
46 | org.springframework
47 | spring-context
48 | true
49 |
50 |
51 | org.springframework.boot
52 | spring-boot-starter
53 | true
54 |
55 |
56 | org.springframework.boot
57 | spring-boot-configuration-processor
58 | true
59 |
60 |
61 |
62 | junit
63 | junit
64 |
65 |
66 | ch.qos.logback
67 | logback-classic
68 |
69 |
70 | org.springframework
71 | spring-jdbc
72 | test
73 |
74 |
75 | org.hsqldb
76 | hsqldb
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/service/src/main/java/io/mybatis/service/BaseService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service;
18 |
19 | import io.mybatis.mapper.example.ExampleWrapper;
20 |
21 | import java.io.Serializable;
22 |
23 | /**
24 | * 基础接口,包含实体类基本接口和 Example 接口
25 | *
26 | * 自己的接口不一定要实现这个接口,直接继承会暴露过多的接口,
27 | * 可以直接在实现类中继承 AbstractService 实现,对外暴露的接口在自己接口中定义,
28 | * 自己定义的接口和 AbstractService 实现方法的定义一样时,不需要提供实现方法
29 | *
30 | * @param 实体类类型
31 | * @param 主键类型
32 | * @author liuzh
33 | */
34 | public interface BaseService extends EntityService, ExampleService {
35 |
36 | ExampleWrapper wrapper();
37 | }
38 |
--------------------------------------------------------------------------------
/service/src/main/java/io/mybatis/service/ExampleService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service;
18 |
19 | import io.mybatis.mapper.example.Example;
20 |
21 | import java.io.Serializable;
22 | import java.util.List;
23 |
24 | /**
25 | * Example 接口
26 | *
27 | * @param 实体类类型
28 | * @param 主键类型
29 | * @author liuzh
30 | */
31 | public interface ExampleService {
32 |
33 | /**
34 | * 获取 Example 对象
35 | *
36 | * @return Example 对象
37 | */
38 | default Example example() {
39 | return new Example<>();
40 | }
41 |
42 | /**
43 | * 根据 example 条件批量删除
44 | *
45 | * @param example 查询条件
46 | * @return 返回大于0成功,0失败
47 | */
48 | int delete(Example example);
49 |
50 | /**
51 | * 根据 example 查询条件批量更新(所有字段)
52 | *
53 | * @param entity 实体类
54 | * @param example 查询条件
55 | * @return 返回大于0成功,0失败
56 | */
57 | int update(T entity, Example example);
58 |
59 | /**
60 | * 根据 example 查询条件批量更新(非空字段)
61 | *
62 | * @param entity 实体类
63 | * @param example 查询条件
64 | * @return 返回大于0成功,0失败
65 | */
66 | int updateSelective(T entity, Example example);
67 |
68 | /**
69 | * 根据 example 条件查询一个,当结果多于1个时出错
70 | *
71 | * @param example 查询条件
72 | * @return 实体
73 | */
74 | T findOne(Example example);
75 |
76 | /**
77 | * 根据 example 条件查询
78 | *
79 | * @param example 查询条件
80 | * @return 实体集合
81 | */
82 | List findList(Example example);
83 |
84 | /**
85 | * 根据 example 查询总数
86 | *
87 | * @param example 查询条件
88 | * @return 总数
89 | */
90 | long count(Example example);
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/MyBaseService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service;
18 |
19 | /**
20 | * 个人的实现,屏蔽具体业务和 BaseService 的直接依赖
21 | *
22 | * @param
23 | */
24 | public interface MyBaseService extends BaseService {
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/RoleService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service;
18 |
19 | import io.mybatis.mapper.fn.Fn;
20 | import io.mybatis.service.model.Role;
21 |
22 | import java.util.Collection;
23 |
24 | public interface RoleService {
25 |
26 | /**
27 | * 保存(所有字段)
28 | *
29 | * @param entity 实体类
30 | * @return 返回保存成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
31 | */
32 | Role save(Role entity);
33 |
34 | /**
35 | * 更新(所有字段)
36 | *
37 | * @param entity 实体类
38 | * @return 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
39 | */
40 | Role update(Role entity);
41 |
42 | /**
43 | * 更新(非空字段),指定的强制更新字段不区分是否为空
44 | *
45 | * @param entity 实体类
46 | * @param forceUpdateFields 强制更新的字段,不区分字段是否为 null
47 | * @return 返回更新成功后的实体,远程服务调用时,由于序列化和反序列化,入参和返回值不是同一个对象
48 | */
49 | Role updateSelective(Role entity, Fn... forceUpdateFields);
50 |
51 | /**
52 | * 根据主键进行删除
53 | *
54 | * @param id 指定的主键
55 | * @return 返回 1成功,0失败抛出异常
56 | */
57 | int deleteById(Integer id);
58 |
59 | /**
60 | * 根据指定字段集合删除
61 | *
62 | * @param field 字段
63 | * @param fieldValueList 字段值集合
64 | * @param 字段值类型
65 | * @return 删除数据的条数
66 | */
67 | int deleteByFieldList(Fn field, Collection fieldValueList);
68 |
69 | /**
70 | * 根据指定的主键查询
71 | *
72 | * @param id 主键
73 | * @return 实体
74 | */
75 | Role findById(Integer id);
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/UserService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service;
18 |
19 | import io.mybatis.service.model.User;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | * 暴露所有通用方法
25 | */
26 | public interface UserService extends MyBaseService {
27 |
28 | int deleteByIdList(List ids);
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/database-schema-role.sql:
--------------------------------------------------------------------------------
1 | drop table role if exists;
2 |
3 | create table role
4 | (
5 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
6 | name VARCHAR(32) DEFAULT 'DEFAULT'
7 | );
8 |
9 | insert into role(id, name)
10 | values (1, '管理员'),
11 | (2, '游客');
12 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/database-schema-user.sql:
--------------------------------------------------------------------------------
1 | drop table user if exists;
2 |
3 | create table user
4 | (
5 | id INTEGER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
6 | name VARCHAR(32) DEFAULT 'DEFAULT',
7 | role_id INTEGER
8 | );
9 |
10 | insert into user(id, name, role_id)
11 | values (1, 'admin', 1),
12 | (2, 'guest', 2);
13 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/impl/BaseIdService.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.impl;
18 |
19 | import io.mybatis.mapper.BaseMapper;
20 | import io.mybatis.service.AbstractService;
21 | import io.mybatis.service.model.BaseId;
22 |
23 | import java.util.List;
24 |
25 | public abstract class BaseIdService, M extends BaseMapper> extends AbstractService {
26 |
27 | /**
28 | * 根据ID列表进行删除,issues #50
29 | */
30 | public int deleteByIdList(List ids) {
31 | return deleteByFieldList(T::getId, ids);
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/impl/RoleServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.impl;
18 |
19 | import io.mybatis.service.AbstractService;
20 | import io.mybatis.service.RoleService;
21 | import io.mybatis.service.mapper.RoleMapper;
22 | import io.mybatis.service.model.Role;
23 | import org.springframework.stereotype.Service;
24 |
25 | @Service
26 | public class RoleServiceImpl extends AbstractService implements RoleService {
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/impl/UserServiceImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.impl;
18 |
19 | import io.mybatis.service.UserService;
20 | import io.mybatis.service.mapper.UserMapper;
21 | import io.mybatis.service.model.User;
22 | import org.springframework.stereotype.Service;
23 |
24 | @Service
25 | public class UserServiceImpl extends BaseIdService implements UserService {
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/BaseMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.service.mapper;
17 |
18 | import io.mybatis.mapper.Mapper;
19 |
20 | public interface BaseMapper extends Mapper {
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/RoleMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.service.mapper;
17 |
18 | import io.mybatis.mapper.Mapper;
19 | import io.mybatis.service.model.Role;
20 |
21 | public interface RoleMapper extends Mapper, RoleMarker {
22 |
23 | Role findById(Integer id);
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/RoleMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
23 |
24 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/RoleMarker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.mapper;
18 |
19 | public interface RoleMarker {
20 | }
21 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/UserMapper.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package io.mybatis.service.mapper;
17 |
18 | import io.mybatis.service.model.User;
19 |
20 | public interface UserMapper extends BaseMapper, UserMarker {
21 |
22 | User findById(Long id);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/UserMapper.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
23 |
24 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/mapper/UserMarker.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.mapper;
18 |
19 | public interface UserMarker {
20 | }
21 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/model/BaseId.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.model;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | public class BaseId {
22 | @Entity.Column(id = true, insertable = false)
23 | private Integer id;
24 |
25 | public Integer getId() {
26 | return id;
27 | }
28 |
29 | public void setId(Integer id) {
30 | this.id = id;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/model/Role.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.model;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | @Entity.Table
22 | public class Role {
23 | @Entity.Column(id = true, insertable = false)
24 | private Integer id;
25 |
26 | @Entity.Column
27 | private String name;
28 |
29 | public Integer getId() {
30 | return id;
31 | }
32 |
33 | public void setId(Integer id) {
34 | this.id = id;
35 | }
36 |
37 | public String getName() {
38 | return name;
39 | }
40 |
41 | public void setName(String name) {
42 | this.name = name;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/model/User.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2020-2022 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package io.mybatis.service.model;
18 |
19 | import io.mybatis.provider.Entity;
20 |
21 | @Entity.Table
22 | public class User extends BaseId {
23 |
24 | @Entity.Column
25 | private String name;
26 |
27 | @Entity.Column("role_id")
28 | private Integer roleId;
29 |
30 | public User() {
31 | }
32 |
33 | public User(String name) {
34 | this.name = name;
35 | }
36 |
37 | public User(String name, Integer roleId) {
38 | this.name = name;
39 | this.roleId = roleId;
40 | }
41 |
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | public void setName(String name) {
47 | this.name = name;
48 | }
49 |
50 | public Integer getRoleId() {
51 | return roleId;
52 | }
53 |
54 | public void setRoleId(Integer roleId) {
55 | this.roleId = roleId;
56 | }
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/service/src/test/java/io/mybatis/service/spring.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
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 |
--------------------------------------------------------------------------------
/service/src/test/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
20 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------