2.1 maven配置
基本设置:拿到老师给的仓库文件夹repository-spring,然后安装好maven后,在idea配置好maven,以及本地仓库地址。
2.2 虚拟机配置
主要是拿到老师给的现成的配置好的虚拟机centos7,里面已经搞好了docker容器,和一些必要的数据;自己配的话太吃力,到公司这些也是用现成的。
方法是:打开VMware,然后选择file,打开,选择老师提供的虚拟机centos7文件夹,找到.vmx,确定,选择获取所有权,ok,运行虚拟机,输入命令:docker ps -a,发现,已经提供好了。
注意:这个虚拟机的ip地址,要和NAT模式的ip网段匹配。不然连接不到外界。
测试远程连接docker中的软件:再windows上面访问试试。
3.1 搭建gmall-parent
桌面创建一个文件夹gmall,打开idea,选择File–>Open–>Project,
然后在gmall下new一个Module:gmall-parent作为商城项目父工程。删掉src,配置pom.xml;
这里的pom里面只做版本控制,没有引入实际依赖。
3.2 搭建common父模块
在gmall-parent下创建子模块common,写好pom.xml;同时他又作为common-util 和service-util 的父模块。
common:公共模块父节点
common-util:工具类模块,所有模块都可以依赖于它
service-util:service服务的工具包,包含service服务的公共配置类,所有service模块依赖于它
搭建common-util模块
搭建好后,在src/java里面粘贴,现成的公共工具类,在,项目资料里。 里面主要包含exception包,result包,util包,
分别作用是:
1:exception,全局自定义异常类,以后所有代码里面出异常都可以调用它来处理异常,就不用写try-catch了。
2;result,统一controller层响应类,在响应json时就用它作为返回值。
3;util,一些常用的使用工具类,入MD5加密
搭建service-util模块
搭建好后,在src/java里面粘贴,现成的公共工具类,在,项目资料里。里面面主要包含config包,constant包,handler包;全都是一些公共的常用的配置类(代替配置文件)
搭建model模块
在gmall-parent 父工程下创建 model;导入提供好的包,主要作用是存放所有微服务的pojo实体类。
搭建service父模块
作为所有微服务的父亲 ,在他的pom里面添加service-util和model的jar包依赖,这样做的原因是使得它的子模块都能拥有基础的公共的哪些个东西。
搭建service-product
一个微服务。配置好pom,配置好application.yml ,application-dev.yml (开发环境使用),写好启动类。打开虚拟机,然后运行微服务,看看微服务是否能运行成功。
代码实现,简单增删改查
8个基本操作:
-
id查询
-
查询全部
-
新增
-
修改
-
删除
-
条件查询
-
分页查询
-
分页条件查询
创建好mapper包,controller包,service包。
先搞mapper,
@Mapper
public interface BaseCategory1Mapper extends BaseMapper<BaseCategory1> {
}
再搞service
public interface BaseCategory1Service {
/**
* 根据主键id查询
* @param id id的数据类型应该去数据库中的表对应的id字段查询得到
* @return
*/
public BaseCategory1 selectById(Long id);
/**
* 查询全部
*/
public List<BaseCategory1> selectAll();
/**
* 新增数据
*/
public void save(BaseCategory1 baseCategory1);
/**
* 修改
* @param baseCategory1
*/
public void update(BaseCategory1 baseCategory1);
/**
* 删除
* @param id
*/
public void delete(Long id);
/**
* 条件查询
*/
public List<BaseCategory1> search(BaseCategory1 baseCategory1);
/**
* 分页查询
*
* @return
*/
public IPage<BaseCategory1> page(Integer page, Integer size);
/**
* 条件分页查询
*/
public IPage<BaseCategory1> search(Integer page, Integer size,BaseCategory1 baseCategory1);
}
实现类:
@Service
public class BaseCategory1ServiceImpl implements BaseCategory1Service {
@Autowired
BaseCategory1Mapper baseCategory1Mapper;
/**
* 根据主键id查询
* @param id id的数据类型应该去数据库中的表对应的id字段查询得到
* @return
*/
@Override
public BaseCategory1 selectById(Long id) {
//调用mybatis-plus提供的查询方法,自己没写,我想它的出现是对标springdata的
return baseCategory1Mapper.selectById(id);
}
/**
* 查询全部
*/
@Override
public List<BaseCategory1> selectAll() {
return baseCategory1Mapper.selectList(null);//全部查询不需要参数,所以写null;
}
/**
* 新增数据
*/
@Override
public void save(BaseCategory1 baseCategory1) {
int row = baseCategory1Mapper.insert(baseCategory1);
//if条件判断:当新增失败时抛异常
if(row <= 0){
throw new RuntimeException("新增失败");
}
}
/**
* 修改
* @param baseCategory1
*/
@Override
public void update(BaseCategory1 baseCategory1) {
int row = baseCategory1Mapper.updateById(baseCategory1);
//if条件判断:当修改失败时抛异常
if(row <= 0){
throw new RuntimeException("新增失败");
}
}
/**
* 删除
* @param id
*/
@Override
public void delete(Long id) {
int row = baseCategory1Mapper.deleteById(id);
//if条件判断:当删除失败时抛异常
if(row <= 0){
throw new RuntimeException("新增失败");
}
}
/**
* 条件查询
*/
@Override
public List<BaseCategory1> search(BaseCategory1 baseCategory1) {
LambdaQueryWrapper wrapper = getWrapper(baseCategory1);
return baseCategory1Mapper.selectList(wrapper);
}
/**
* 分页查询
* @param page
* @param size
* @return
*/
@Override
public IPage<BaseCategory1> page(Integer page, Integer size) {
return baseCategory1Mapper.selectPage(new Page<>(page,size),null);
}
/**
* 条件分页查询
* @param page
* @param size
* @param baseCategory1
* @return
*/
@Override
public IPage<BaseCategory1> search(Integer page, Integer size, BaseCategory1 baseCategory1) {
LambdaQueryWrapper wrapper = getWrapper(baseCategory1);
return baseCategory1Mapper.selectPage(new Page<>(page,size),wrapper);
}
/**
* 私有方法:提供构造..
*/
private LambdaQueryWrapper getWrapper(BaseCategory1 baseCategory1){
//
LambdaQueryWrapper<BaseCategory1> wrapper = new LambdaQueryWrapper<>();
//
if(baseCategory1.getId() != null){
wrapper.eq(BaseCategory1::getId,baseCategory1.getId());
}
//
if(baseCategory1.getName() != null){
wrapper.like(BaseCategory1::getName,baseCategory1.getName());
}
return wrapper;
}
}
搞controller:
//@Controller//响应一个页面(视图)
@RestController//响应一个json格式的字符串对象
@RequestMapping("/api/basecategory")
public class BaseCategory1Controller {
@Autowired
BaseCategory1Service baseCategory1Service;
/**
* 根据主键id查询表baseCategory1
*
* @param id 关于此参数的修饰的区别:
* 如果是@RequestParam(默认的):哪浏览器的请求写法:localhost:8206/api/basecategory/selectbyid?id=1
* 如果是@PathVariable,叫做从路径取值:哪浏览器的请求写法:localhost:8206/api/basecategory/selectbyid/1
* 还有一种从body(应该是post的请求实体那种),用,@RequestBody
* @return
*/
@GetMapping("/selectbyid/{id}")
public Result selectById(@PathVariable(value = "id") Long id){
BaseCategory1 baseCategory1 = baseCategory1Service.selectById(id);
return Result.ok(baseCategory1);//统一result对象返回,他自己会转成json的
}
/**
* 查询全部
* localhost:8206/api/basecategory/selectall
*/
@GetMapping("/selectall")
public Result selectAll(){
List<BaseCategory1> list = baseCategory1Service.selectAll();
return Result.ok(list);
}
/**
* 新增
*/
@PostMapping
public Result add(@RequestBody BaseCategory1 baseCategory1){
baseCategory1Service.save(baseCategory1);
return Result.ok();
}
/**
* 修改
*/
@PutMapping
public Result update(@RequestBody BaseCategory1 baseCategory1){
baseCategory1Service.update(baseCategory1);
return Result.ok();
}
/**
* 删除
*/
@RequestMapping("/delete/{id}")
public Result delete(@PathVariable(value = "id") Long id){
baseCategory1Service.delete(id);
return Result.ok();
}
/**
* 条件查询
*/
@RequestMapping("/select")
public Result select(@RequestBody BaseCategory1 baseCategory1){
List<BaseCategory1> li = baseCategory1Service.search(baseCategory1);
return Result.ok(li);
}
/**
* 分页查询
*/
@GetMapping("/page/{page}/{size}")
public Result page(@PathVariable(value = "page") Integer page,
@PathVariable(value = "size") Integer size){
IPage<BaseCategory1> page1 = baseCategory1Service.page(page, size);
return Result.ok(page1);
}
/**
* 条件分页查询
*/
@PostMapping("/search/{page}/{size}")
public Result search(@PathVariable(value = "page") Integer page,
@PathVariable(value = "size") Integer size,
@RequestBody BaseCategory1 baseCategory1){
IPage<BaseCategory1> page1 = baseCategory1Service.search(page, size,baseCategory1);
return Result.ok(page1);
}
}
然后postman软件,发送请求,实现增删改查。
git:代码的推送与拉取
安装好git,集成到idea,码云(gitee)官网注册好,新建仓库(在公司一般用公司提供的仓库,自己作为开发成员加入仓库),选择仓库的克隆按钮,复制链接,idea打开file,new,project from version control...。把链接粘贴进URL,就能从gitee仓库里面下载项目代码了,之后进行项目代码的推送与拉取。