springboot3

2024-05-11 1360阅读

springboot3

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
    @RequestMapping("/helloworld")
    public String helloworld(){
        return "hello";
    }
}
maven下载地址Index of /dist/maven/maven-3/3.9.6/binaries

注意版本  jdk 17

下载地址Java Downloads | Oracle

Index of /spring-boot/docs/current/reference

Common Application Properties

#虚拟路径
server.servlet.context-path=/start

springboot3@Value注解

@ConfigurationProperties(prefix="前缀")

springboot3

整合mybatis

实现第一个findbyid

实体类  pojo

public class User {
    private Integer id;
    private String name;
    private Integer age;
    private String phone;
    private String gender;
    public User(){
    }

接口mapper

@Mapper
public interface UserMapper {
    @Select("select * from user where id = #{id}")
    public  User findById(Integer id);
}

service

public interface UserService {
    public User findById(Integer id);
}

实现service

//将实现类交给容器
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
//调用usermapper需要注入usermapper
    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }
}

控制器

@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/findById")
    public User findById(Integer id){
       return userService.findById(id);
    }
}
最新依赖
  
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            3.0.0
        
        
            com.mysql
            mysql-connector-j
            runtime
        

springboot3

mybatis:
  mapper-locations: classpath:mappers/*xml
  type-aliases-package: com.example.demo.mybatis.entity
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mybatis
    username: 
    password:

Bean扫描

@ComponentScan(basePackages="com.xxxx")

@SpringBootApplication(组合注解)  默认扫描启动类所在的包.

springboot3

@ComponentScan(basePackages="com.xxxx") 指定扫描哪个包

Bean注册

@Component

@Controller  控制器

@Service    业务类

@Repository  数据访问类

第三方类包注册  (不是自定义的) 不能用@Component

下面两种可以访问第三方类包

@Bean

@Import

mvn install:file -Dfile = jar 包本地磁盘路径

-DgroupId = 组织名称

-Dartifactid = 项目名

-Dversion = 版本号

-Dpackaging 打包方式

nvm install:install-file -Dartifactid=xxxx -Dfile=C:\user\xx.jar -DgroupId=cn.xx.xx -Dversion=1.0 -Dpackaging=jar

    cn.xx.xx DgroupId
    xxxx Dartifactid
    1.0
@Bean
public Counter counter() {
    return  new Counter();
}
在启动类中接收
ApplicationContext context = SpringApplication.run(Demo1Application.class, args);
xx xx=context.getBean(xx.class)

springboot3

不建议  ,建议在配置类中集中注册

config配置类

@Configuration

@Bean

springboot3

springboot3

springboot3

导入 @import

springboot3

如果不在同一个包里面找不到的情况下,可以使用这个@import

public class CommonImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.example.demo.config","com.example.demo.dao"};
    }
}

springboot3多个import

springboot3

@Import(CommonImportSelector.class)

读取配置文件

public class CommonImportSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        List list = new ArrayList();
        InputStream   is =  CommonImportSelector.class.getClassLoader().getResourceAsStream("common.imports");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (is != null){
                    br.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return list.toArray(new String[0]);
    }
}
package com.example.demo.anno;
import com.example.demo.config.CommonImportSelector;
import org.springframework.context.annotation.Import;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import(CommonImportSelector.class)
public @interface EnableCommonConfig {
}

注解

注册条件

@Value("${country.name}")
private  String name;

读取配置文件

配置文件不要忘记空格

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]