springboot代码只能放到 Application的同级或下级目录
集成的框架
自动集成mvc
springboot代码只能放到 Application的同级或下级目录
@Transactional 事务注解方法里同成功同失败
集成mybatis导包就完了
mybatis逆向工程,生成实体bean,映射文件,DAO接口 仅单表
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.6
GeneratorMapper.xml
true
true
加GeneratorMapper.xml文件
maven-plugins-mybatis-generator-mybatis-generator:generate
sqlxml抽取相同片段
id, type, name, description
select
from tbl_book
server:
port: 80 //配置端口号
server:
servlet:
context-path: /springboot //设置访问网页前缀
多环境开发 2.4后
spring:
config:
activate: dev //表示用 application-dev.yml的配制
同一个yml里可以
spring:
config:
activate:
on-profile: dev 这段是dev
自定义属性配置
ads:
aaa: 82
azz:
- 83
- 99
@value("${ads.aaa}")
private int a; a=82
@value("${ads.azz[0]}")
private int a; a=83
…
@Autowired
private Environment a; 封装了这个配置里所有数据的对象
int c=a.getProperty(“ads.azz[0]”)
德鲁伊连数据库
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///ssm
username: root
password: 126917
redis
spring.redis.host=localhost
spring.redis.port=6379
视图解析器
spring:
mvc:
view:
prefix: /
suffix: .jsp
导包,配属性
spring.redis.host=localhost
spring.redis.port=6379
@Autowired
private RedisTemplate
redisTemplate.opsForValue().set(“TOKEN_”+token, JSON.toJSONString(sysUser),1, TimeUnit.DAYS); 失效时间一天
String userJson=redisTemplate.opsForValue().get(“TOKEN_”+token);
dubbo接口工程:存放实体bean和业务接口 pojo dao service 实体类 implements Serialization 序列化
生产者 impl @Service(interfaceClass=asdService,version=“对应版本号”,timeout=150000) 用dubbo的
消费者 controller 视图 @Reference(interfaceClass=asdService,version=“对应版本号”,check=false) <–>@Autowire 用dubbo的
生产消费Application + @EnableDubboConfiguration 开启dubbo配置
生产者
com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 com.101tec zkclient 0.10 org.example c02 自己建的对应的接口工程 1.0-SNAPSHOT #端口 server.port=80 #上下文根 service.servlet.context-path=/#设置dubbo配置
spring.application.name=c02p
#表示当前工程是个生产者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost
消费者
com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 com.101tec zkclient 0.10 org.example c02 1.0-SNAPSHOT #端口 server.port=80 #上下文根 service.servlet.context-path=/#设置dubbo配置
spring.application.name=c02c
#设置注册中心
spring.dubbo.registry=zookeeper://localhost
定义应该拦截器实现HandlerInterceptor
创建配置类(相当于在SpringMVC的配置文件中进行配置< mvc:interceptor> )
@Component
public class Ljq implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//编写业务拦截的规则
//从service中获取用户信息
User user = (User) request.getSession().getAttribute("user");
//判断用户是否登录
if(null==user){
//未登录调整页面
response.sendRedirect(request.getContextPath()+"/nologin");
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//拦a放d 放的字符串数组
registry.addInterceptor(new Ljq()).addPathPatterns("/a/**").excludePathPatterns("/a/d");
}
}
servlet @WebServlet/使用配置类
filter过滤器 入口加扫描@ServletComponentScan(basePackages=“…”)
@WebFilter(urlPatterns=“…”) implements Filter
doFilter(…){
//进入对应url执行方法
}
也可以配置类
设置字符编码法1
定义配置类
@Bean
public FilterRegistrationBean A(){
CharacterEncodingFilter cef=new CharacterEncodingFilter();//创建字符编码过滤器
cef.setForceEncoding(true);//设置强制使用指定字符编码
cef.SetEncoding(“utf-8”)//设置指定字符集
FilterRegistrationBean f=new FilterRegistrationBean();
f.setFilter(cef);//设置字符编码过滤器
f.addUrlPatterns(“/*”);//设置字符编码过滤器路径
return f;
}
resp.setContentType(“text/html;character=utf-8”);//统一浏览器编码格式
yml里关闭http字符编码支持,只有关了自己设置的才能生效
spring:
http:
encoding:
enabled: false
设置字符编码法2
yml 设置请求访问字符编码
spring:
http:
encoding:
enabled: true
force: true
charset: utf-8
war
入口 extends SpringBootServletInitializer
重写 SpringApplicationBuilder
return builder.source(Application.class);
点package
加个logback-spring.xml 现成的



