1.SprintBootj集成mybaits 连接数据库
pom.xml文件添加依赖
mysql
mysql-connector-java
8.0.30
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
mapper文件 数据持久层
简单的说就是没有方法体,直接拼接地址
@GetMapping(value = "/studentrestfull/detail/{id}/{name}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("name") String name)
注意的点:
//以上代码restful请求不容易区分请求类型造成错误 //路径冲突;通常在RESTFUL风格中方法的请求方式会按照增删改查进行区分 //路径冲突;更改请求路径 //RESTFul请求风格要求路径中出现都是名词,不要是动词
@RestController
public class StudentControllerRestful {
@RequestMapping(value = "studentrestfull")
public Object studentrestfull(Integer id,String name){
Student student = new Student();
student.setId(id);
student.setName(name);
return student;
}
//restful请求风格 (get) http://127.0.0.1:8081/springboot/studentrestfull/detail/11/lisi
// @RequestMapping(value = "/studentrestfull/detail/{id}/{name}")
@GetMapping(value = "/studentrestfull/detail/{id}/{name}")
public Object student1(@PathVariable("id") Integer id,
@PathVariable("name") String name){
Map retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("name",name);
return retMap;
}
//restful请求风格 使用postman进行数据验证
// @RequestMapping(value = "/studentrestfull/detail/{id}/{status}")
@DeleteMapping(value = "/studentrestfull/detail/{id}/{status}")
public Object student2(@PathVariable("id") Integer id,
@PathVariable("status") String status){
Map retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("status",status);
return retMap;
}
//以上代码restful请求不容易区分请求类型造成错误
//通常在RESTFUL风格中方法的请求方式会按照增删改查进行区分
//city与status进行冲突
// @DeleteMapping(value = "/studentrestfull/detail/{id}/{city}")
@DeleteMapping(value = "/studentrestfull/{id}/detail/{city}")
public Object student3(@PathVariable("id") Integer id,
@PathVariable("city") String city){
Map retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("city",city);
return retMap;
}
}
1.添加依赖 (pom文件)
org.springframework.boot
spring-boot-starter-data-redis
2.核心配置文件中添加redis配置(application.properties文件)
spring.redis.host=127.0.0.1 spring.redis.port= 6379 spring.redis.password= 123456
redis控制器
@Controller public class StudentControllerRedis { @Autowired private StudentService studentService; @RequestMapping(value = "/put") public @ResponseBody Object putredis(String key,String value){ studentService.put(key,value); return "写入成功"; } @RequestMapping(value = "/get") public @ResponseBody String get(){ String count = studentService.get("name"); return "数据count为:" +count; } }
redis接口
public interface StudentService { // 根据学生id查询学生 这是接口 需要实现类StudentServiceImpl Student querStudengById(Integer id); //修改学生id int updateStudentById(Student student); //将值存放到server中 void put(String key, String value); //从redis获取count值 String get(String count); }
redis接口实现层
//StudentService 的实现类 业务层 @Service public class StudentServiceImpl implements StudentService { @Autowired //加载对象 private StudentMapper studentMapper; @Autowired //加载对象 private RedisTemplate
pom文件
com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 com.101tec zkclient 0.10
配置文件
#设置Dubbo的配置 spring.application.name=022-springboot-dubbo-consumer #当前工程是一个服务的提供者 spring.dubbo.server = true #设置注册中心 spring.dubbo.registry=zookeeper://127.0.0.1:2181
web中的控制文件
@Controller public class Studentconsumer { // dubbo:reference interface="" version="" check="" @Reference(interfaceClass = StudentServiceDobbu.class,version = "1.0.0",check = false) private StudentServiceDobbu studentServiceDobbu; //http://127.0.0.1:8081/student/count @RequestMapping(value = "student/count") public @ResponseBody Object studnetCount(){ Integer allStudnetCount = studentServiceDobbu.querAllStudentCount(); return "学生总人数" + allStudnetCount; } }
dubbo接口
package com.javawebs.springboot_014.service; public interface StudentServiceDobbu { // 获取学生总人数 Integer querAllStudentCount(); }
impl接口实现类
package com.javawebs.springboot_014.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.javawebs.springboot_014.service.StudentServiceDobbu; import org.springframework.stereotype.Component; @Component //加载到springboot @Service(interfaceClass = StudentServiceDobbu.class,version = "1.0.0",timeout = 15000) //暴露接口 public class StudentServiceDobbuImpl implements StudentServiceDobbu { @Override public Integer querAllStudentCount() { //调用数据持久层 return 10000; } }
启动器
@SpringBootApplication //开启spring配置 @EnableDubboConfiguration //开启Dobbu注解配置 @MapperScan(basePackages = "com.javawebs.springboot_014.mapper") //开启扫描mapper接口的包和子包 总的 //@EnableTransactionManagement //可选项 加不加事物都生效 public class Springboot014Application { public static void main(String[] args) { SpringApplication.run(Springboot014Application.class, args); } }