在上一篇文章《SpringBoot进阶教程(六十)intellij idea project下建多个module(上)》中,我们已经介绍了在intellij idea中创建project之后再分化多个module,今天再大致介绍介绍各个module之间详细工作的细分。 如果是不考虑细分多个module的话,可以看看这篇文章《SpringBoot入门教程(一)详解intellij idea搭建SpringBoot》。
v设计数据库
CREATE TABLE`useraccount` (
`id`int(10) unsigned NOT NULLAUTO_INCREMENT,
`username`varchar(255) NOT NULL,
`age`int(10) NOT NULL,
`phone`bigint NOT NULL,
`email`varchar(255) NOT NULL,
`account`varchar(100) NOT NULL UNIQUE,
`pwd`varchar(255) NOT NULL,PRIMARY KEY(`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;insert into `useraccount` values(1,'赵(dev)',23,158,'qq@qq.com','test001','test001');insert into `useraccount` values(2,'钱(dev)',27,136,'126@126.com','test002','test002');insert into `useraccount` values(3,'孙(dev)',31,159,'163@163.com','test003','test003');insert into `useraccount` values(4,'李(dev)',35,130,'sina@sina.com','test004','test004');select * from `useraccount`;
v引入mybatis
1.0 添加mybatis插件
在learn-persist的pom.xml中添加mybatis插件。
org.mybatis.generator
mybatis-generator-maven-plugin
${mybatis-generator.version}
mysql
mysql-connector-java
${mysql.version}
org.mybatis.generator
mybatis-generator-core
${mybatis-generator.version}
src/main/resources/mybatis-config/mybatis-generator.xml
true
false
如上图,在learn-persist的resources下分别创建mapper和mybatis-config文件目录,并添加jdbc.properties和mybatis-generator.xml文件。
1.1 jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mytest?useSSL=false
jdbc.username=toutou
jdbc.password=demo123456
1.2 mybatis-generator.xml
/p>
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
mybatis-generator.xml更多介绍可以看这里。
1.3 版本号统一配置
为了方便后续管理,将所有引入插件的版本号统一放在project(hellolearn)的pom.xml中统一控制,各子module(如learn-persist)的pom.xml中直接用${}的方法使用。
例如:在hellolearn的pom.xml 属性中添加< mybatis-generator.version>1.3.6< /mybatis-generator.version>,在learn-persist的pom.xml中直接< version>${mybatis-generator.version}< /version>使用即可。
注意上面的"例如"中代码标签部分有空格是为了转义的,防止浏览器将"mybatis-generator.version"和"version"识别为html标签。
1.4 Maven Project 插件
如上图,在idea右侧的maven project中就可以了对应的找到mybatis-generator的插件,如果找不到就右键maven project中的learn-persist,然后点击generate sources...。
1.5 运行mybatis generator插件
如上图,点击运行mybatis generator:generator插件,会对应生成左侧标红的三个文件。
v编写controller
2.0 添加result返回实体类
packagelearn.model.vo;importjava.io.Serializable;/***@authortoutou
* @date by 2019/07*/
public class Result implementsSerializable {public intcode;publicString message;publicT data;public intgetCode() {returncode;
}public void setCode(intcode) {this.code =code;
}publicString getMessage() {returnmessage;
}public voidsetMessage(String message) {this.message =message;
}publicT getData() {returndata;
}public voidsetData(T data) {this.data =data;
}public static ResultsetSuccessResult(T t){
Result r = new Result();
r.setCode(200);
r.setData(t);
r.setMessage("success");returnr;
}public static Result setErrorResult(inttempCode, String messageTemp){
Result r = new Result();
r.setCode(tempCode);
r.setMessage(messageTemp);returnr;
}
}
2.1 添加controller
package learn.web.controller;
import learn.model.vo.Result;
import learn.model.vo.UserAccountVO;
import learn.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author toutou
* @date by 2019/07
*/
@RestController
public class UserController {
@Autowired
UserAccountService userAccountService;
@GetMapping("/user/hello")
public String helloWorld() {
return "hello world.";
}
@GetMapping("/user/getuser")
public Result getUserAccountById(@RequestParam("uid") int id){
UserAccountVO user = userAccountService.getUserAccountById(id);
if(user != null){
return Result.setSuccessResult(user);
}else{
return Result.setErrorResult(404, "用户不存在");
}
}
}
注意:getUserAccountById暂时用不上,可以先不添加。
2.2 添加aplication启动类
packagelearn.web;importorg.mybatis.spring.annotation.MapperScan;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.context.annotation.ComponentScan;/*** Created by toutou on 2019/7*/@SpringBootApplication
@ComponentScan(basePackages= {"learn.*"})
@MapperScan(basePackages= {"learn.persist"})public classApplication {public static voidmain(String[] args) {
SpringApplication.run(Application.class, args);
}
}
2.3 添加aplication.properties
server.port=8300
spring.profiles.active=@env@
#mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
2.4 配置启动类
2.5 测试效果
v编写service
3.1 添加Service
packagelearn.service;importlearn.model.vo.UserAccountVO;/***@authortoutou
* @date by 2019/07*/
public interfaceUserAccountService {
UserAccountVO getUserAccountById(Integer id);
}
3.2 实现Service
packagelearn.service.impl;importlearn.model.po.UserAccount;importlearn.model.vo.UserAccountVO;importlearn.persist.UserAccountMapper;importlearn.service.UserAccountService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;/***@authortoutou
* @date by 2019/07*/@Servicepublic class UserAccountServiceImpl implementsUserAccountService{
@Autowired
UserAccountMapper userMapper;publicUserAccountVO getUserAccountById(Integer id){
UserAccountVO accountVO= null;
UserAccount account=userMapper.selectByPrimaryKey(id);if (account != null) {
accountVO= newUserAccountVO();
accountVO.setId(account.getId());
accountVO.setAccount(account.getAccount());
accountVO.setAge(account.getAge());
accountVO.setEmail(account.getEmail());
accountVO.setUsername(account.getUsername());
accountVO.setPhone(account.getPhone());
}returnaccountVO;
}
}
v配置设置
4.1 添加application.properties
4.2 添加application.dev.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2b8
spring.datasource.username=toutou
spring.datasource.password=demo123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
4.3 添加pom.xml配置
4.3.1 hellolearn pom.xml
4.3.2 learn-web pom.xml
src/main/resources/config/application-${env}.properties
4.4 更新application启动类
4.5 测试效果
vlinux部署springboot
5.1 learn-web pom.xml中添加插件
src/main/resources/config/application-${env}.properties
org.springframework.boot
spring-boot-maven-plugin
learn.web.Application
exec
repackage
5.2 hellolearn pom.xml中添加插件
org.apache.maven.plugins
maven-jar-plugin
3.0.2
**/profiles/
**/mybatis-generator/
org.apache.maven.plugins
maven-compiler-plugin
1.8
1.8
注意:如果有用到profiles文件目录则exclude。
5.3 打包部署
java -jar learn-web-0.0.1-SNAPSHOT-exec.jar --server.port=95
你可能会遇到的问题:
v源码地址
作 者:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!