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
@ConfigurationProperties(prefix="前缀")
整合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
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(组合注解) 默认扫描启动类所在的包.
@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)
不建议 ,建议在配置类中集中注册
config配置类
@Configuration
@Bean
导入 @import
如果不在同一个包里面找不到的情况下,可以使用这个@import
public class CommonImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.example.demo.config","com.example.demo.dao"};
}
}
@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;
读取配置文件
配置文件不要忘记空格












