Skip to content

Commit b79e537

Browse files
committed
feat: mapper、mapperXML更新、增加常用方法、增加mapper测试方法
1 parent 24e6cf0 commit b79e537

File tree

31 files changed

+1668
-585
lines changed

31 files changed

+1668
-585
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
node_modules
22

33
demo/mybatis-cli-springboot-demo/.idea
4-
demo/mybatis-cli-test/.idea
4+
demo/mybatis-cli-test/.idea
5+
.idea
6+
*.log
7+
*.class

demo/mybatis-cli-springboot-demo/mp-api/mp-api.iml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
<facet type="web" name="Web">
88
<configuration>
99
<webroots />
10+
<sourceRoots>
11+
<root url="file://$MODULE_DIR$/src/main/java" />
12+
<root url="file://$MODULE_DIR$/src/main/resources" />
13+
</sourceRoots>
14+
</configuration>
15+
</facet>
16+
<facet type="jpa" name="JPA">
17+
<configuration>
18+
<setting name="validation-enabled" value="true" />
19+
<setting name="provider-name" value="Hibernate" />
20+
<datasource-mapping>
21+
<factory-entry name="entityManagerFactory" />
22+
</datasource-mapping>
23+
<naming-strategy-map />
1024
</configuration>
1125
</facet>
1226
</component>
@@ -16,6 +30,8 @@
1630
<content url="file://$MODULE_DIR$">
1731
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
1832
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
33+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
34+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
1935
<excludeFolder url="file://$MODULE_DIR$/target" />
2036
</content>
2137
<orderEntry type="inheritedJdk" />
@@ -86,11 +102,32 @@
86102
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:2.3.1.RELEASE" level="project" />
87103
<orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.5" level="project" />
88104
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:5.2.7.RELEASE" level="project" />
89-
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.7.RELEASE" level="project" />
90105
<orderEntry type="library" name="Maven: org.mybatis.spring.boot:mybatis-spring-boot-autoconfigure:2.2.2" level="project" />
91106
<orderEntry type="library" name="Maven: org.mybatis:mybatis:3.5.9" level="project" />
92107
<orderEntry type="library" name="Maven: org.mybatis:mybatis-spring:2.0.7" level="project" />
93108
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.20" level="project" />
109+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-jpa:2.3.1.RELEASE" level="project" />
110+
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:2.3.1.RELEASE" level="project" />
111+
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.9.5" level="project" />
112+
<orderEntry type="library" name="Maven: jakarta.transaction:jakarta.transaction-api:1.3.3" level="project" />
113+
<orderEntry type="library" name="Maven: jakarta.persistence:jakarta.persistence-api:2.2.3" level="project" />
114+
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:5.4.17.Final" level="project" />
115+
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.4.1.Final" level="project" />
116+
<orderEntry type="library" name="Maven: org.javassist:javassist:3.24.0-GA" level="project" />
117+
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
118+
<orderEntry type="library" name="Maven: org.jboss:jandex:2.1.3.Final" level="project" />
119+
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.5.1" level="project" />
120+
<orderEntry type="library" name="Maven: org.dom4j:dom4j:2.1.3" level="project" />
121+
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.1.0.Final" level="project" />
122+
<orderEntry type="library" name="Maven: org.glassfish.jaxb:jaxb-runtime:2.3.3" level="project" />
123+
<orderEntry type="library" name="Maven: org.glassfish.jaxb:txw2:2.3.3" level="project" />
124+
<orderEntry type="library" name="Maven: com.sun.istack:istack-commons-runtime:3.0.11" level="project" />
125+
<orderEntry type="library" scope="RUNTIME" name="Maven: com.sun.activation:jakarta.activation:1.2.2" level="project" />
126+
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:2.3.1.RELEASE" level="project" />
127+
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:2.3.1.RELEASE" level="project" />
128+
<orderEntry type="library" name="Maven: org.springframework:spring-orm:5.2.7.RELEASE" level="project" />
129+
<orderEntry type="library" name="Maven: org.springframework:spring-tx:5.2.7.RELEASE" level="project" />
130+
<orderEntry type="library" name="Maven: org.springframework:spring-aspects:5.2.7.RELEASE" level="project" />
94131
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper-spring-boot-starter:1.2.12" level="project" />
95132
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper-spring-boot-autoconfigure:1.2.12" level="project" />
96133
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper:5.1.10" level="project" />

demo/mybatis-cli-springboot-demo/mp-api/src/main/java/com/lucaswangdev/service/UserService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public interface UserService {
88
// 更加id查询
9-
User selectById(Integer id);
9+
User selectById(Long id);
1010

1111
// 插入数据
1212
Integer insert(User user);
@@ -15,7 +15,7 @@ public interface UserService {
1515
Integer updateByPrimaryKeySelective(User user);
1616

1717
// 删除数据
18-
Integer delete(Integer id);
18+
Integer delete(Long id);
1919

2020
// 通过地址查询用户
2121
List<User> queryByAddress(User user);

demo/mybatis-cli-springboot-demo/mp-api/src/main/java/com/lucaswangdev/service/impl/UserServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class UserServiceImpl implements UserService {
1616

1717

1818
@Override
19-
public User selectById(Integer id) {
19+
public User selectById(Long id) {
2020
return userMapper.query(id);
2121
}
2222

@@ -32,8 +32,8 @@ public Integer updateByPrimaryKeySelective(User user) {
3232

3333

3434
@Override
35-
public Integer delete(Integer id) {
36-
return userMapper.delete(id);
35+
public Integer delete(Long id) {
36+
return userMapper.deleteByPrimaryKey(id);
3737
}
3838

3939
@Override
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.lucaswangdev;
2+
3+
import com.lucaswangdev.entity.User;
4+
import com.lucaswangdev.mapper.UserMapper;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.data.domain.PageRequest;
11+
import org.springframework.data.domain.Pageable;
12+
import org.springframework.data.domain.Sort;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.sql.Timestamp;
18+
19+
@RunWith(SpringRunner.class)
20+
@SpringBootTest
21+
public class UserTest {
22+
23+
@Autowired
24+
private UserMapper userMapper;
25+
26+
// @Test
27+
// public void testDeleteByPrimaryKey() {
28+
// int result = userMapper.deleteByPrimaryKey(20L);
29+
// Assert.assertTrue(result >= 0);
30+
// }
31+
32+
@Test
33+
public void testSelectByPrimaryKey() {
34+
User user = userMapper.selectByPrimaryKey(2L);
35+
Assert.assertNotNull(user);
36+
}
37+
38+
@Test
39+
public void testInsert() {
40+
User user = new User();
41+
user.setUserName("TestUser");
42+
user.setSex("1");
43+
user.setAddress("Test Address");
44+
int result = userMapper.insert(user);
45+
Assert.assertTrue(result > 0);
46+
}
47+
48+
@Test
49+
public void testInsertSelective() {
50+
User user = new User();
51+
user.setUserName("SelectiveUser");
52+
int result = userMapper.insertSelective(user);
53+
Assert.assertTrue(result > 0);
54+
}
55+
56+
@Test
57+
public void testInsertBatch() {
58+
// Create a list of User entities to insert
59+
List<User> userList = new ArrayList<>();
60+
61+
// Prepare test data
62+
User user1 = new User();
63+
user1.setUserName("Lucas1");
64+
user1.setSex("M");
65+
user1.setAddress("Address1");
66+
67+
User user2 = new User();
68+
user2.setUserName("Lucas2");
69+
user2.setSex("M");
70+
user2.setAddress("Address2");
71+
72+
userList.add(user1);
73+
userList.add(user2);
74+
75+
// Call insertBatch method and capture the result
76+
int rowsInserted = userMapper.insertBatch(userList);
77+
78+
// Verify the result
79+
Assert.assertEquals(userList.size(), rowsInserted);
80+
System.out.println("Rows inserted: " + rowsInserted);
81+
}
82+
83+
@Test
84+
public void testUpdateByPrimaryKey() {
85+
User user = new User();
86+
user.setId(2L);
87+
user.setGmtCreate(new Timestamp(System.currentTimeMillis()));
88+
user.setGmtModified(new Timestamp(System.currentTimeMillis()));
89+
user.setUserName("xxxName");
90+
user.setSex("1");
91+
user.setAddress("Updated Address");
92+
int result = userMapper.updateByPrimaryKey(user);
93+
Assert.assertTrue(result > 0);
94+
}
95+
96+
@Test
97+
public void testUpdateByPrimaryKeySelective() {
98+
User user = new User();
99+
user.setId(2L);
100+
user.setUserName("UpdatedUser");
101+
int result = userMapper.updateByPrimaryKeySelective(user);
102+
Assert.assertTrue(result > 0);
103+
}
104+
105+
@Test
106+
public void testSelectCount() {
107+
User user = new User();
108+
user.setUserName("李四");
109+
int count = userMapper.selectCount(user);
110+
Assert.assertTrue(count >= 0);
111+
}
112+
113+
@Test
114+
public void testSelectPage() {
115+
User user = new User();
116+
// 设置需要查询的条件,例如 user.setUserName("Lucas");
117+
118+
// 创建分页对象,注意 page 是从 0 开始的
119+
Pageable pageable = PageRequest.of(0, 10, Sort.by("id").descending());
120+
List<User> users = userMapper.selectPage(user, pageable);
121+
Assert.assertNotNull(users);
122+
Assert.assertTrue(users.size() >= 0);
123+
}
124+
125+
@Test
126+
public void testDelete() {
127+
int result = userMapper.delete(1L);
128+
Assert.assertTrue(result >= 0);
129+
}
130+
131+
@Test
132+
public void testQueryByAddress() {
133+
User user = new User();
134+
user.setAddress("Test Address");
135+
List<User> users = userMapper.queryByAddress(user);
136+
Assert.assertNotNull(users);
137+
Assert.assertTrue(users.size() >= 0);
138+
}
139+
140+
@Test
141+
public void testQuery() {
142+
User user = userMapper.query(2L);
143+
Assert.assertNotNull(user);
144+
}
145+
146+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
server:
2+
port: 8083
3+
4+
spring:
5+
mvc:
6+
static-path-pattern: /**
7+
datasource:
8+
driver-class-name: com.mysql.cj.jdbc.Driver
9+
url: jdbc:mysql://127.0.0.1:3306/mybatis-cli?serverTimezone=Asia/Shanghai&characterEncoding=utf8
10+
username: root
11+
password: root
12+
jackson:
13+
serialization:
14+
write-dates-as-timestamps: true
15+
default-property-inclusion: non_null
16+
17+
18+
logging:
19+
file:
20+
name: mp-api.log
21+
path: ./
22+
level:
23+
root: debug
24+
com.lucaswangdev.mapper: debug
25+
pattern:
26+
console: log-%level %msg%n
27+
28+
pegehelper:
29+
help-dicalect: mysql
30+
reasonable: false
31+
support-methods-arguments: true
32+
params: count=countSql
33+
row-bounds-uewith-count: tr

demo/mybatis-cli-springboot-demo/mp-dao/jasmine/tpl/Mapper.tpl.vm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
#end
1313
#end
1414

15-
$processor.generate("${model.tplPath}/Mapper.vm", "${model.targetPath}/${table.className}Mapper.xml", true, $repoModel)
15+
$processor.generate("${model.tplPath}/Mapper.vm", "${model.targetPath}/${table.className}Mapper.java", true, $repoModel)
1616
#end

0 commit comments

Comments
 (0)