SpringBoot--springboot启动类和controller的配置

一、controller和启动类在同一个class中

使用eclipse创建springboot项目时,会在默认的包下自动创建一个*Application.java的类,然后可以直接在这个类中配置controller并使用这个类启动项目。

@SpringBootApplication
@RestController
public class DemoApplication {
	@RequestMapping("/index")
    public String index() {
      
        return "this is index!";
    }


	public static void main(String[] args) {
		//SpringApplication.run(DemoApplication.class, args);
		SpringApplication application = new SpringApplication(DemoApplication.class);
		application.setBannerMode(Mode.OFF);//关闭banner
		application.run(args);
	}

}

这就是将controller和启动类放在一个类中的配置,直接点右键运行这个类就可以访问了!
二、将controller和启动类分开配置

因为将controller和启动类分开,所以首先要新建一个controller类

@RestController
public class HelloWorldController {
	@RequestMapping("/hello")
	public String hello(){
		return "hello World!";
	}
}

我不改变启动类的位置,但是启动类中只要一个main方法即可,如下:

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//SpringApplication.run(DemoApplication.class, args);
		SpringApplication application = new SpringApplication(DemoApplication.class);
		application.setBannerMode(Mode.OFF);//关闭banner
		application.run(args);
	}

}

在这里插入图片描述
此时启动类的位置高于controller
三、启动类放在任意包下面,我是放在创建项目默认包中,这样只需在启动类上增加注解@ComponentScan即可,其中的(basePackages = {“com.xiao”})是需要扫描的包名,可以是多个,如(basePackages = {“com.xiao”,“com.example”})等,这就意味着mvc容器会去这些包下面扫描,进而找到controller等资源。
建一个测试controller在任意位置

package com.xiao.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class demoController {
	@RequestMapping("/demo")
	public String demo(){
		return "this is demo!";
	}
}

此时启动类增加注解

@ComponentScan(basePackages = {"com.xiao","com.example"})
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		//SpringApplication.run(DemoApplication.class, args);
		SpringApplication application = new SpringApplication(DemoApplication.class);
		application.setBannerMode(Mode.OFF);//关闭banner
		application.run(args);
	}

}
综上所述,springboot的配置有以下三种:

1、当启动类和controller在同一类中时,需要在该类上添加注解@Controller;

2、当启动类和controller分开时,启动目录高于controller目录,启动类上只有注解@SpringBootApplication;

3、当启动类和controller分开时,如果启动类在某个包下,需要在启动类中增加注解@ComponentScan,配置需要扫描的包名;

以下是使用Spring Boot结合MyBatis和Alibaba Druid连接池对ClickHouse进行数据操作的步骤: 1.在pom.xml文件中添加ClickHouse JDBC驱动和Alibaba Druid连接池的依赖: ```xml <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> ``` 2.在application.properties文件中配置ClickHouse和Druid的连接信息: ```properties # ClickHouse 数据库连接配置 spring.datasource.url=jdbc:clickhouse://localhost:8123/default spring.datasource.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver spring.datasource.username=username spring.datasource.password=password # Druid 连接池配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.initial-size=1 spring.datasource.min-idle=1 spring.datasource.max-active=20 spring.datasource.max-wait=60000 spring.datasource.time-between-eviction-runs-millis=60000 spring.datasource.min-evictable-idle-time-millis=300000 spring.datasource.validation-query=SELECT 1 FROM DUAL spring.datasource.test-while-idle=true spring.datasource.test-on-borrow=false spring.datasource.test-on-return=false spring.datasource.pool-prepared-statements=true spring.datasource.max-pool-prepared-statement-per-connection-size=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 ``` 3.在Spring Boot的启动上添加注解@EnableTransactionManagement和@MapperScan: ```java @SpringBootApplication @EnableTransactionManagement @MapperScan("com.example.demo.mapper") public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4.创建Mapper接口和对应的XML文件,使用MyBatis进行数据操作: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user") List<User> selectAll(); } ``` ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.example.demo.entity.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="age" property="age" jdbcType="INTEGER"/> </resultMap> <select id="selectAll" resultMap="BaseResultMap"> SELECT * FROM user </select> </mapper> ``` 5.在Controller中注入Mapper并进行数据操作: ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> getUsers() { return userMapper.selectAll(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值