Skip to content

Commit 6cdf2b6

Browse files
mybatis自动生成
1 parent 9e95a4b commit 6cdf2b6

12 files changed

Lines changed: 669 additions & 18 deletions

File tree

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@
7474
<version>4.12</version>
7575
<scope>test</scope>
7676
</dependency>
77+
<dependency>
78+
<groupId>com.github.pagehelper</groupId>
79+
<artifactId>pagehelper-spring-boot-starter</artifactId>
80+
<version>1.2.5</version>
81+
</dependency>
82+
<dependency>
83+
<groupId>org.springframework.boot</groupId>
84+
<artifactId>spring-boot-devtools</artifactId>
85+
</dependency>
86+
<dependency>
87+
<groupId>org.mybatis.generator</groupId>
88+
<artifactId>mybatis-generator-core</artifactId>
89+
<version>1.3.6</version>
90+
</dependency>
7791

7892

7993
</dependencies>
@@ -84,6 +98,18 @@
8498
<groupId>org.springframework.boot</groupId>
8599
<artifactId>spring-boot-maven-plugin</artifactId>
86100
</plugin>
101+
102+
<!-- mybatis generator 自动生成代码插件 -->
103+
<plugin>
104+
<groupId>org.mybatis.generator</groupId>
105+
<artifactId>mybatis-generator-maven-plugin</artifactId>
106+
<version>1.3.6</version>
107+
<configuration>
108+
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
109+
<overwrite>true</overwrite>
110+
<verbose>true</verbose>
111+
</configuration>
112+
</plugin>
87113
</plugins>
88114
</build>
89115

src/main/java/com/xh/basic/controller/UserInfoController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ public ResponseBean selectById(Integer id){
3131
responseBean = responseBean.rtnSuccess(userInfoService.selectById(id));
3232
return responseBean;
3333
}
34+
35+
@PostMapping("/selectByUserName")
36+
@MCache(keyOrIdx = "0", cacheGroup = "userInfo")
37+
public ResponseBean selectByUserName(String userName){
38+
ResponseBean responseBean = new ResponseBean();
39+
responseBean = responseBean.rtnSuccess(userInfoService.selectByUserName(userName));
40+
return responseBean;
41+
}
3442
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.xh.basic.dao;
2+
3+
import com.xh.basic.model.StudentInfo;
4+
5+
public interface StudentInfoMapper {
6+
int deleteByPrimaryKey(Integer id);
7+
8+
int insert(StudentInfo record);
9+
10+
int insertSelective(StudentInfo record);
11+
12+
StudentInfo selectByPrimaryKey(Integer id);
13+
14+
int updateByPrimaryKeySelective(StudentInfo record);
15+
16+
int updateByPrimaryKey(StudentInfo record);
17+
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package com.xh.basic.dao;
22

33
import com.xh.basic.model.UserInfo;
4-
import org.apache.ibatis.annotations.Mapper;
5-
import org.apache.ibatis.annotations.Param;
6-
import org.springframework.beans.factory.annotation.Qualifier;
74

8-
@Mapper
95
public interface UserInfoMapper {
6+
int deleteByPrimaryKey(Integer id);
107

11-
UserInfo selectById(@Param("id") Integer id);
12-
}
8+
int insert(UserInfo record);
9+
10+
int insertSelective(UserInfo record);
11+
12+
UserInfo selectByPrimaryKey(Integer id);
13+
14+
int updateByPrimaryKeySelective(UserInfo record);
15+
16+
int updateByPrimaryKey(UserInfo record);
17+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.xh.basic.model;
2+
3+
import java.util.Date;
4+
5+
public class StudentInfo {
6+
private Integer id;
7+
8+
private String userName;
9+
10+
private String password;
11+
12+
private Date birthday;
13+
14+
private Byte sex;
15+
16+
public Integer getId() {
17+
return id;
18+
}
19+
20+
public void setId(Integer id) {
21+
this.id = id;
22+
}
23+
24+
public String getUserName() {
25+
return userName;
26+
}
27+
28+
public void setUserName(String userName) {
29+
this.userName = userName == null ? null : userName.trim();
30+
}
31+
32+
public String getPassword() {
33+
return password;
34+
}
35+
36+
public void setPassword(String password) {
37+
this.password = password == null ? null : password.trim();
38+
}
39+
40+
public Date getBirthday() {
41+
return birthday;
42+
}
43+
44+
public void setBirthday(Date birthday) {
45+
this.birthday = birthday;
46+
}
47+
48+
public Byte getSex() {
49+
return sex;
50+
}
51+
52+
public void setSex(Byte sex) {
53+
this.sex = sex;
54+
}
55+
}

src/main/java/com/xh/basic/model/UserInfo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.xh.basic.model;
22

33
public class UserInfo {
4-
54
private Integer id;
65

76
private String userName;
@@ -19,6 +18,6 @@ public String getUserName() {
1918
}
2019

2120
public void setUserName(String userName) {
22-
this.userName = userName;
21+
this.userName = userName == null ? null : userName.trim();
2322
}
24-
}
23+
}

src/main/java/com/xh/basic/service/UserInfoService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@
55
public interface UserInfoService {
66

77
UserInfo selectById(Integer id);
8+
9+
UserInfo selectByUserName(String userName);
810
}

src/main/java/com/xh/basic/service/impl/UserInfoServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public class UserInfoServiceImpl implements UserInfoService {
1818
@Override
1919
@DataSourceTarget("slave")
2020
public UserInfo selectById(Integer id) {
21-
return userInfoMapper.selectById(id);
21+
return userInfoMapper.selectByPrimaryKey(id);
22+
}
23+
24+
@Override
25+
public UserInfo selectByUserName(String userName) {
26+
return null;
2227
}
2328
}
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,60 @@
11
package com.xh.basic.utils;
22

3+
import org.mybatis.generator.api.MyBatisGenerator;
4+
import org.mybatis.generator.config.Configuration;
5+
import org.mybatis.generator.config.xml.ConfigurationParser;
6+
import org.mybatis.generator.exception.InvalidConfigurationException;
7+
import org.mybatis.generator.exception.XMLParserException;
8+
import org.mybatis.generator.internal.DefaultShellCallback;
9+
import org.springframework.util.ResourceUtils;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.net.URL;
14+
import java.sql.SQLException;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
318
/**
419
* @author szq
520
* @Package com.xh.basic.utils
6-
* @Description: to do ...
21+
* @Description: mybatis generator 代码生成
722
* @date 2018/4/2517:16
823
*/
924
public class MyBatisGeneratorTool {
25+
public static void main(String[] args){
26+
List<String> warnings = new ArrayList<String>();
27+
boolean overwrite = true;
28+
String genCfg = "generatorConfig.xml";
29+
URL url = MyBatisGeneratorTool.class.getResource(genCfg);
30+
//File configFile = new File(MyBatisGeneratorTool.class.getResource(genCfg).getFile());
31+
File configFile = new File(genCfg);
32+
ConfigurationParser cp = new ConfigurationParser(warnings);
33+
Configuration config = null;
34+
try{
35+
config = cp.parseConfiguration(configFile);
36+
}catch (IOException e){
37+
e.printStackTrace();
38+
}catch (XMLParserException e){
39+
e.printStackTrace();
40+
}
41+
42+
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
43+
MyBatisGenerator myBatisGenerator = null;
44+
try{
45+
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
46+
}catch(InvalidConfigurationException e){
47+
e.printStackTrace();
48+
}
49+
50+
try{
51+
myBatisGenerator.generate(null);
52+
}catch (SQLException e){
53+
e.printStackTrace();
54+
}catch (IOException e){
55+
e.printStackTrace();
56+
}catch (InterruptedException e){
57+
e.printStackTrace();
58+
}
59+
}
1060
}

src/main/resources/application-local.properties

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,19 @@ spring.redis.pool.min-idle=0
3939
spring.redis.pool.max-idle=8
4040

4141

42+
#设置热部署
43+
#开启热部署
44+
spring.devtools.restart.enabled=true
45+
#重启范围
46+
spring.devtools.restart.additional-paths=src/main/java
47+
48+
#配置分页插件
49+
pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialect
50+
pagehelper.helper-dialect=mysql
51+
pagehelper.reasonable=true
52+
pagehelper.support-methods-arguments=true
53+
pagehelper.params=count=countSql
54+
55+
4256

4357

0 commit comments

Comments
 (0)