1.如何通过maven的形式创建springboot工程
①pom文件继承springboot的父工程(idea必须是2020以上的版本且本地仓库必须存在该依赖)
<parent>
<groupId>org.springframework.boot</groupId>
<version>2.5.1</version>
<artifactId>spring-boot-starter-parent</artifactId>
</parent>
②引入web启动依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
③ 创建启动类并加入注解
@SpringBootApplication
public class MyMaven {
public static void main(String[] args) {
SpringApplication.run(MyMaven.class,args);
}
}
*注意:controller service mapper必须在主启动类所在的包下或子包下。
2.springboot 工程中两种常见的配置文件类型
在resources层下
①application.propreties
#1.设置服务器端口号
server.port=8081
②application.yml
server:
port: 8082
properties比yml的优先级高
3.Java如何读取springboot配置文件中自己定义的内容
①创建一个student类,通过@ConfigurationProperties( prefix ="student")
@Data //产生get,set方法以及toString
@Component //由spring的容器创建该类的对象
@ConfigurationProperties(prefix = "student") //读取配置文件中前缀为student的配置内容
public class Student {
private String name;
// @Value("${student.age}")
private int age;
private List<String> hoppys;
private Map<String,String> map;
private Set<String> set;
private Class cla;
}
定义的Class类
@Data
public class Class {
private String name;
private int claid;
}
②配置application.properties文件的内容
#1.设置服务器端口号
server.port=8081
#设置属性值
student.name=gpf
student.age=18
student.hoppys=ddd,fff,ggg
student.map.k1=tt
student.map.k2=yy
student.set=ii,oo
student.cla.name=qy
student.cla.claid=2
yml形式
server:
port: 8082
student:
name: pgf
age: 12
hoppys:
- yy
- oo
- pp
map:
k1: ww
k2: yy
set:
- ll
- bb
cla:
name: tyt
claid: 132
运行启动类
4.用springboot整合数据源
创建spring项目时
配置pom
<!--引入druid的数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!-- 引入mybatis和springboot的依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
配置application.properties文件
spring.datasource.druid.username=root
spring.datasource.druid.password=012932
spring.datasource.druid.url=jdbc:mysql://localhost:3306/emp?serverTimezone=Asia/Shanghai
spring.datasource.druid.driver-class-name=com.mysql.cj.jdbc.Driver
在test文件中测试是否连接数据库成功
@SpringBootTest
class Springboot031ApplicationTests {
@Autowired
private DataSource dataSource;
@Test
void contextLoads() throws SQLException {
System.out.println(dataSource);
}
}
运行结果有内容即为连接成功
在properties配置映射文件所在的路径
#配置映射文件所在的路径
mybatis.mapper-locations=classpath:/mapper/*.xml
创建相应的entry,mapper,controller
entry:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private Integer id;
private String name;
}
mapper:
public interface EmpMapper {
public List<Emp> findAll();
}
controller
@RestController
public class EmpController {
@Autowired
private EmpMapper empMapper;
@GetMapping("/emp")
public List<Emp> emp(){
return empMapper.findAll();
}
}
在resource下建映射文件EmpMapper.xml
<mapper namespace="com.gpf.qy132.demo.mapper.EmpMapper">
<select id="findAll" resultType="com.gpf.qy132.demo.entry.Emp">
select * from test1
</select>
</mapper>
在主启动类上扫描mapper接口所在的包
@SpringBootApplication
@MapperScan(basePackages = {"com.gpf.qy132.demo.mapper"})
public class Springboot031Application {
public static void main(String[] args) {
SpringApplication.run(Springboot031Application.class, args);
}
}
输入localhost:8080/emp 运行。
5.springboot如何注册web三大组件
web三大组件:Servlet , Filter 过滤器 ,Listener 监听器。
①注册Servlet
创建一个servlet类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("经过了doGet方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("经过了doPost方法");
}
}
创建一个配置类
@Configuration //表示该类为配置类 等价于spring的配置文件
public class MyConfig {
//注册servlet
@Bean //ServletRegistrationBean serlvet的注册器
public ServletRegistrationBean servletBean(){
ServletRegistrationBean servletRegistrationBean=new ServletRegistrationBean();
servletRegistrationBean.setServlet(new MyServlet());
servletRegistrationBean.setName("myServlet");
servletRegistrationBean.addUrlMappings("/my");
return servletRegistrationBean;
}
②注册Filter
创建一个过滤器类
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~经过了过滤器");
filterChain.doFilter(servletRequest,servletResponse);//放行
}
}
注册该过滤器到Springboot容器中
//注册filter
@Bean
public FilterRegistrationBean filterBean(){
FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean();
filterRegistrationBean.setName("myFilter");
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
③注册监听器Listener
创建一个监听器类
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("监听");
}
}
注册监听
@Bean
public ServletListenerRegistrationBean listenerBean(){
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean();
servletListenerRegistrationBean.setListener(new MyListener());
return servletListenerRegistrationBean;
}