1.SprintBootj集成mybaits 连接数据库
pom.xml文件添加依赖
<!-- mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<!-- mybatis 整合Sprintboot框架起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
mapper文件 数据持久层
- RESTFul风格
简单的说就是没有方法体,直接拼接地址
@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<String,Object> 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<String,Object> 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<String,Object> retMap = new HashMap<>();
retMap.put("id",id);
retMap.put("city",city);
return retMap;
}
}
- 集成redis
1.添加依赖 (pom文件)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
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<Object,Object> redisTemplate; @Override public Student querStudengById(Integer id) { return studentMapper.selectByPrimaryKey(id); } @Transactional //事物,int a = 10/0; 执行出错,数据库不改变 @Override public int updateStudentById(Student student) { int i = studentMapper.updateByPrimaryKeySelective(student); // int a = 10/0; return i; } @Override public void put(String key,String value){ redisTemplate.opsForValue().set(key,value); //操作string } @Override public String get(String key){ String count = (String) redisTemplate.opsForValue().get(key); return count; } }
- SpringBoot 集成Dubbo 分布式框架 (服务提供者consumer)
pom文件
<!-- dubbo集成springboot起步依赖--> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!-- 注册中心--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency>
配置文件
#设置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); } }