目录
1、建立数据库
(1)pom.xml
3、创建pojo项目
(1)com.example.pojo
Dept:
Emp:
4、创建mapper项目
(1)pom.xml
(2)application.yml
5、新建api项目
(1)pom.xml
6、新建provider项目
(1)pom.xml
(2)application.yml
(3)com.example
7、实现Dept查询工作
(1)api:com.example.dubbo.service
(2)provider:com.example.dubbo.service.impl
(3)在mapper项目中补充对数据库的 *** 作
com.example.mapper
mybatis:
(4)新建dept项目
pom.xml
application.yml
com.example
com.example.service
com.example.service.impl
com.example.controller
resources /templates/dept.html
8、实现部门列表工作、图片上传
(1)api:com.example.dubbo.service
(2)provider:com.example.dubbo.service.impl
(3)在mapper项目中补充对数据库的 *** 作
com.example.mapper
mybatis
(4)新建emp项目
pom.xml
application.yml
com.example
com.example.serever
com.example.serever.impl
com.example.controller
resources /templates/empadd-add.html
(5)补充dept项目代码实现部门员工查看
com.example.service
com.example.service,impl
com.example.controller
resources /templates/showEmp.html
resources /templates/dept.html
9、界面展示
1、建立数据库
create table dept( id int(11) primary key auto_increment, name varchar(20) ); insert into dept values(default,'开发部'); insert into dept values(default,'产品部'); create table emp( id int(11) primary key auto_increment, name varchar(20), photo varchar(200), did int(11), ConSTRAINT fk_emp_dept FOREIGN key (did) REFERENCES dept(id) );2、创建父项目 (Parent) (1)pom.xml
org.springframework.boot spring-boot-starter-parent2.1.10.RELEASE org.springframework.boot spring-boot-starter2.1.10.RELEASE org.springframework.boot spring-boot-starter-web2.1.10.RELEASE org.springframework.boot spring-boot-starter-thymeleaf2.1.10.RELEASE org.apache.dubbo dubbo-spring-boot-starter2.7.3 org.apache.curator curator-recipes4.2.0 org.apache.curator curator-framework4.2.0 org.mybatis.spring.boot mybatis-spring-boot-starter2.1.1 mysql mysql-connector-java8.0.21 commons-io commons-io2.6
public class Dept implements Serializable { private Integer id; private String name ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Dept{" + "id=" + id + ", name='" + name + ''' + '}'; } }Emp:
public class Emp implements Serializable { private Integer id ; private String name ; private String photo ; private Integer did ; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getDid() { return did; } public void setDid(Integer did) { this.did = did; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } @Override public String toString() { return "Emp{" + "id=" + id + ", name='" + name + ''' + ", photo='" + photo + ''' + ", did=" + did + '}'; } }4、创建mapper项目 (1)pom.xml
(2)application.ymlpojo com.example 1.0-SNAPSHOT org.mybatis.spring.boot mybatis-spring-boot-startermysql mysql-connector-java
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mydubbo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai username: root password: root mybatis: mapper-locations: classpath:mybatis/*.xml type-aliases-package: com.example.pojo5、新建api项目 (1)pom.xml
6、新建provider项目 (1)pom.xmlpojo com.example 1.0-SNAPSHOT
(2)application.ymlmapper com.example 1.0-SNAPSHOT api com.example 1.0-SNAPSHOT org.springframework.boot spring-boot-starterorg.apache.dubbo dubbo-spring-boot-starterorg.apache.curator curator-recipesorg.apache.curator curator-framework
dubbo: application: name: dubbo-provider registry: address: zookeeper://192.168.1.129:2181 #加载其它配置文件,加载其它的application-*.yml文件 #多个名称之间用逗号分隔 spring: profiles: active: mybatis(3)com.example
@SpringBootApplication @EnableDubbo @MapperScan("com.example.mapper") public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class,args); } }7、实现Dept查询工作 (1)api:com.example.dubbo.service
public interface DeptDubboService { public List(2)provider:com.example.dubbo.service.implfindAllDept(); }
@Service public class DeptDubboServiceImpl implements DeptDubboService{ @Autowired private DeptMapper deptMapper; @Override public List(3)在mapper项目中补充对数据库的 *** 作 com.example.mapperfindAllDept() { return deptMapper.findAll(); } }
public interface DeptMapper { public Listmybatis:findAll(); }
(4)新建dept项目 pom.xmlselect id , name from dept ;
application.ymlapi com.example 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleaforg.apache.dubbo dubbo-spring-boot-starterorg.apache.curator curator-recipesorg.apache.curator curator-framework
dubbo: application: name: dubbo-dept-consumer registry: address: zookeeper://192.168.1.129:2181com.example
@SpringBootApplication @EnableDubbo public class DeptApplication { public static void main(String[] args) { SpringApplication.run(DeptApplication.class,args); } }com.example.service
public interface DeptService { public Listcom.example.service.implfindAll(); }
@Service public class DeptServiceImpl implements DeptService { @Reference private DeptDubboService deptDubboService; @Override public ListfindAll() { return deptDubboService.findAllDept(); } }
在dept项目中新建接口,在其实现类中注入api接口对象,调用api中接口方法。
通过dept中接口的实现类调用api的方法。
com.example.controller@Controller public class DeptController { @Autowired private DeptService deptService; @GetMapping("/dept") public String showDept(Model model){ model.addAttribute("list",deptService.findAll()); return "dept"; } }
注入了deptService接口对象。
model.addAttribute:向前台传递数据,数据名为:list,数据内容为:deptService.findAll()。
resources /templates/dept.html8、实现部门列表工作、图片上传 (1)api:com.example.dubbo.serviceTitle 编号 部门名称 查看 查看
public interface EmpDubboService { public int insertEmp(Emp emp); public List(2)provider:com.example.dubbo.service.implfindEmpByDeptID(Integer did); }
@Service public class EmpDubboServiceImpl implements EmpDubboService { @Autowired private EmpMapper empMapper; @Override public int insertEmp(Emp emp) { return empMapper.insertEmp(emp); } @Override public List(3)在mapper项目中补充对数据库的 *** 作 com.example.mapperfindEmpByDeptID(Integer did) { return empMapper.findEmpByDeptId(did); } }
public interface EmpMapper { public int insertEmp(Emp emp); public ListmybatisfindEmpByDeptId(Integer did); }
(4)新建emp项目 pom.xmlinsert into emp (name,photo,did) values (#{name},#{photo},#{did})
application.ymlapi com.example 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-thymeleaforg.apache.dubbo dubbo-spring-boot-starterorg.apache.curator curator-recipesorg.apache.curator curator-framework
dubbo: application: name: dubbo-emp-consumer registry: address: zookeeper://192.168.1.129:2181 server: port: 8081com.example
@SpringBootApplication @EnableDubbo public class EmpApplication { public static void main(String[] args) { SpringApplication.run(EmpApplication.class , args); } }com.example.serever
public interface EmpService { public Listcom.example.serever.implshowAll() ; public int insert(Emp emp, MultipartFile file); }
@Service public class EmpServiceImpl implements EmpService { @Reference private DeptDubboService deptDubboService; @Reference private EmpDubboService empDubboService ; @Override public Listcom.example.controllershowAll() { return deptDubboService.findAllDept(); } @Override public int insert(Emp emp, MultipartFile file) { try { //通过spring容器获取HttpServletRequest对象 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); //通过HttpServletRequest对象 获取图片上传的路径 String path = request.getServletContext().getRealPath("/img"); System.out.println("path == " + path); //为了上传到服务器中的图片的名称不重复 编写随机数 long currentTimeMills = System.currentTimeMillis(); Random random = new Random(); String fileName = currentTimeMills + "" + random.nextInt(1000); String oldName = file.getOriginalFilename() ; //通过图片的原名称获取图片的后缀名 fileName += oldName.substring(oldName.lastIndexOf(".")); File pathFile = new File(path); //第一次上传图片 检查目录是否存在 如果不存在 创建响应目录 if(!pathFile.exists()) { pathFile.mkdirs(); } //图片上传 file.transferTo(new File(path , fileName)); //封住emp对象 把图片路径封装到emp对象中 emp.setPhoto("http://localhost:8081/img/"+fileName); //把Emp保存到数据库中 return empDubboService.insertEmp(emp); } catch (IOException e) { e.printStackTrace(); } return 0; } }
@Controller public class EmpController { @Autowired private EmpService empService ; @GetMapping("/empadd") public String empAdd(Model model) { model.addAttribute("list" , empService.showAll()); return "emp-add"; } @PostMapping("/add") public String add(Emp emp , MultipartFile file) { empService.insert(emp , file); return "emp-add"; } }resources /templates/empadd-add.html
(5)补充dept项目代码实现部门员工查看 com.example.serviceTitle
public interface DeptService { public Listcom.example.service,implfindAll(); public List findEmpByDeptId(Integer did); }
@Service public class DeptServiceImpl implements DeptService { @Reference private DeptDubboService deptDubboService; @Reference private EmpDubboService empDubboService; @Override public Listcom.example.controllerfindAll() { return deptDubboService.findAllDept(); } @Override public List findEmpByDeptId(Integer did) { return empDubboService.findEmpByDeptID(did); } }
@Controller public class DeptController { @Autowired private DeptService deptService; @GetMapping("/dept") public String showDept(Model model){ model.addAttribute("list",deptService.findAll()); return "dept"; } @GetMapping("/showEmp") public String showEmp(Integer did,Model model){ model.addAttribute("list",deptService.findEmpByDeptId(did)); return "showEmp"; } }resources /templates/showEmp.html
resources /templates/dept.htmlTitle 姓名 头像
9、界面展示Title 编号 部门名称 查看 查看
重新启动项目照片会丢失。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)