boot环境搭建

  1. 创建Maven工程,在其pom文件中导入父依赖工程

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.2</version>
            <relativePath/> 
    </parent>
    
  2. 基于整合Web开发和MyBatis导入相应依赖

        <dependencies>
            	<!--web启动器-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            	<!-- Springboot整合MyBatis启动器 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>2.0.0</version>
            </dependency>
            	<!-- 分页启动器 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.12</version>
            </dependency>
             	<!--驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.23</version>
            </dependency>
            	<!-- 阿里连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.21</version>
            </dependency>
    			<!-- 测试框架 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            	<!-- SpringBoot 测试框架-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
  3. 提供启动类

    @SpringBootApplication // 表明该类为SpringBoot程序启动类
    @MapperScan("com.project.dao")  // 扫描指定目录下的映射文件
    public class MainServer {
        public static void main(String[] args) {
            SpringApplication.run(MainServer.class,args);
        }
    }
    
  4. 提供配置文件,放在resources目录下,有两种配置文件写法

    1. application.properties

      server.port=8000
      
      spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
      spring.datasource.url=jdbc:mysql://localhost:3306/bootdemo?characterEncoding=utf-8
      spring.datasource.username=root
      spring.datasource.password=root
      spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
      spring.datasource.maxActive=50
      spring.datasource.maxWait=2000
      spring.datasource.minIdle=10
      
      mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
      mybatis.type-aliases-package=com.project.bean
      
      pagehelper.helperDialect=mysql
      pagehelper.reasonable=true
      pagehelper.supportMethodsArguments=true
      pagehelper.params=count=countSql
      
    2. application.yml(推荐)

      server:
        port: 8080
      
      spring:
        datasource:
          driverClassName: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/bootdemo?characterEncoding=utf-8
          username: root
          password: root
          type: com.alibaba.druid.pool.DruidDataSource
          maxActive: 50
          maxWait: 2000
          minIdle: 10
      
      mybatis:
        configuration:
          log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
        type-aliases-package: com.project.bean
      
      pagehelper:
        helperDialect: mysql
        reasonable: true
        supportMethodsArguments: true
        params: count=countSql
      
    3. 自定义名称的配置文件(假设此时配置文件名为boot.properties),此时启动类为:

      @SpringBootApplication
      public class MainServer {
          public static void main(String[] args) {
              new SpringApplicationBuilder(MainServer.class)
                      .properties("spring.config.name:boot").run(args);
          }
      }
      
  5. 根据需求拟定业务接口及业务方法

    public interface IUserService {
        void addUser(User userBean);
        PageInfo<User> cut(int pageNO, int pageSize, String userName);
    }
    
  6. 根据业务方法分析出的数据库操作,给出持久层接口及持久方法

    void addUser(User userBean);
    List<User> cut(String userName);
    
  7. 实现持久接口后,完成业务层实现类的实现并完成测试

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MainServer.class)
    public class TestService {
        @Resource
        private IUserService userService;
    
        @Test
        public void testAll(){
        }
    }
    
  8. 根据需求给出应用控制器,提供控制方法。

PS:静态资源文件搜索顺序:

​ /META-INF/resources —> /resources —> /static —> /public

​ 假如:此时在/static目录下有index.html,访问该页面http://localhost:8080/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值