当前位置: 首页 > news >正文

有哪些做公司网站的深圳seo网站优化公司

有哪些做公司网站的,深圳seo网站优化公司,日照手机网站建设,一个做网站的团队需要哪些目录 一、springboot的应用 1、创建springboot项目 2、乱码问题配置 3、springboot日志配置 4、springboot整合mybatis 二、配置文件讲解及测试 1、全局配置文件参数读取 1.1 全局配置文件的位置 1.2 配置文件的读取 1.2.1 导包 1.2.2 编写配置对象Bean 1.2.3 编写配置文件 1.2…

目录

一、springboot的应用

1、创建springboot项目

2、乱码问题配置

3、springboot日志配置

4、springboot整合mybatis

二、配置文件讲解及测试

1、全局配置文件参数读取

1.1 全局配置文件的位置

1.2 配置文件的读取

1.2.1 导包

1.2.2 编写配置对象Bean

1.2.3 编写配置文件

1.2.3.1 properties文件方式

1.2.3.2 yml(yaml)文件方式

1.2.4 单测

2、四种属性注入方式

2.1 单个属性绑定@Value

2.2 批量属性绑定@ConfigurationProperties

2.3 第三方进行属性绑定@Bean

3、热部署


一、springboot的应用

1、创建springboot项目

将项目主程序启动类SpringbootTestApplication放到com.test目录下
创建src/main/java/com/test/controller/DemoController.java
@RestController // 该注解为组合注解,等同于Spring中@Controller+@ResponseBody注解
public class DemoController {@RequestMapping("/demo")public String demo(){System.out.println("你好");return "hello springBoot";}
}
启动项目: SpringbootTestApplication. main
访问网址: http://localhost:8080/demo

2、乱码问题配置

application,properties添加配置
# 乱码问题配置
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true

3、springboot日志配置

导包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><artifactId>spring-boot-starter-logging</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
配置log的形式
src/main/resources/application.properties
# 日志配置
# 指定具体包的日志级别
logging.level.com.test=debug
# 控制台和日志文件输出格式
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level%logger{50} - %msg%n
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}- %msg%n
# 日志输出路径,默认文件spring.log
logging.file.path=spring.log
#logging.file.name=log.log
单测:
@SpringBootTest
public class LoggerTests {Logger logger = LoggerFactory.getLogger(getClass());@Testpublic void testLog() {logger.trace("Trace 日志...");logger.debug("Debug 日志...");logger.info("Info 日志...");logger.warn("Warn 日志...");logger.error("Error 日志...");}
}

4、springboot整合mybatis

导包
<!-- 配置处理器-配置文件参数注入 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
<!-- 配置mysql数据驱动 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.25</version>
</dependency>
<!-- druid连接池 -->
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version>
</dependency>
<!-- 整合jdbc -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 整合mybatis -->
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>
配置数据库连接
src/main/resources/application.yml
spring:datasource:username: rootpassword: rooturl: jdbc:mysql://192.168.3.33:3306/springimpl?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceinitialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1 FROM DUALtestWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truemaxPoolPreparedStatementPerConnectionSize: 20useGlobalDataSourceStat: trueconnectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500sql:init:mode: always
编写整合 druid 的配置类 DruidConfig:为了使dataSource的非核心参数注入
@Configuration
public class DruidConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druid(){return new DruidDataSource();}
}
基础pojo
src/main/java/com/test/pojo/User.java
public class User {private Integer id;private String username;private String password;//省略set、get、toString
}

编写Mapper

src/main/java/com/test/mapper/UserMapper.java

public interface UserMapper {@Select("SELECT * FROM USER")List<User> findList();
}
在启动类上加扫描配置
@SpringBootApplication
@MapperScan("com.test.mapper")
public class SpringbootTestApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTestApplication.class, args);}
}

编写service
src/main/java/com/test/service/UserService.java
@Service
public class UserService {Logger logger = LoggerFactory.getLogger(UserService.class);@Autowiredprivate UserMapper userMapper;public List<User> findUsers(){List<User> userList = userMapper.findList();logger.info(userList.toString());return userList;}
}
单测
@SpringBootTest
public class MybatisTests {@Autowiredprivate UserService userService;@Testpublic void getUser() {userService.findUsers();}
}

二、配置文件讲解及测试

1、全局配置文件参数读取

1.1 全局配置文件的位置

全局配置文件可以在四个位置,其优先级高低的顺序为:
1. 先去项目根目录找config文件夹下找配置文件件
2. 再去根目录下找配置文件
3. 去resources下找cofnig文件夹下找配置文件
4. 去resources下找配置文件
配置文件中属性:
如果不冲突,则会共同存在-互补配置
如果冲突,默认使用第1个读取到的
如果配置文件的名称不叫application.properties或者application.yml,可以通过以下参数来指定 配置文件的名字,myproject是配置文件名
java -jar myproject.jar --spring.config.name=myproject
也可以指定其他位置的配置文件 和 默认加载的配置文件共同起作用
java -jar run-0.0.1-SNAPSHOT.jar --spring.config.location=D:/application.properties

1.2 配置文件的读取

配置文件有两种方式: application.properties application.yml
2.4.0之前版本,优先级properties>yaml
2.4.0及以后的版本,优先级yaml>properties
如果想继续使用 Spring Boot 2.3 的配置逻辑,也可以通过在 application.properties 或者  application.yml 配置文件中添加以下参数:
spring.config.use-legacy-processing = true
1.2.1 导包
<!-- 配置处理器 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
1.2.2 编写配置对象Bean
创建对象
src/main/java/com/test/pojo/Pet.java
public class Pet {private String type;private String name;//省略get、set、tostring
}
src/main/java/com/test/pojo/Person.java
@Component //用于将Person类作为Bean注入到Spring容器中
@ConfigurationProperties(prefix = "person") //将配置文件中以person开头的属性注入到该类中
public class Person {private int id; //idprivate String name; //名称private List hobby; //爱好private String[] family; //家庭成员private Map map;private Pet pet; //宠物//省略get、set、tostring
}
1.2.3 编写配置文件
1.2.3.1 properties文件方式
src/main/resources/application.properties
person.id=1
person.name=tom
person.hobby=吃饭,睡觉
person.family=大哥,大姐
person.map.k1=v1
person.map.k2=v2
person.pet.name=大白
person.pet.type=dog
1.2.3.2 yml(yaml)文件方式
YAML 文件格式是 Spring Boot 支持的一种 JSON 超集文件格式,以数据为中心,比 properties xml 等更 适合做配置文件
使用 “key: (空格) value” 格式配置属性,使用缩进控制层级关系
src/main/resources/application.yml
#对实体类对象Person进行属性配置
person:id: 1name: lucyhobby: [吃饭,睡觉]family: [father,mother]map: {k1: v1,k2: v2}pet: {type: dog,name: 旺财}
1.2.4 单测
src/test/java/com/test/springboottest/SpringbootTestApplicationTests.java
@SpringBootTest
class SpringbootTestApplicationTests {@Autowiredprivate Person person;@Testvoid contextLoads() {System.out.println(person);}
}

2、四种属性注入方式

注意,当配置文件不是默认配置文件名时:
@PropertySource("classpath:/jdbc.properties") 指定外部属性文件。在类上添加
添加默认配置信息
src/main/resources/application.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/springboot
jdbc.username=root
jdbc.password=123

2.1 单个属性绑定@Value

@Configuration:声明一个类作为配置类
@Value:属性注入
配置类
src/main/java/com/test/config/JdbcConfiguration.java
@Configuration
public class JdbcConfiguration {@Value("${jdbc.url}")String url;@Value("${jdbc.driverClassName}")String driverClassName;@Value("${jdbc.username}")String username;@Value("${jdbc.password}")String password;
}
单测:
@Autowired
private JdbcConfiguration jdbcConfiguration;
@Test
void test01() {System.out.println(jdbcConfiguration);
}

2.2 批量属性绑定@ConfigurationProperties

@Configuration:声明一个类作为配置类
@EnableConfigurationProperties:用于启用应用对另外一个注解 @ConfigurationProperties的支持
@ConfigurationProperties(prefix = "jdbc"):批量属性注入
导包
<!-- 配置处理器 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
配置类
src/main/java/com/test/config/JdbcConfiguration.java
@Configuration
@EnableConfigurationProperties(JdbcConfiguration.class)
@ConfigurationProperties(prefix = "jdbc")
public class JdbcConfiguration {String url;String driverClassName;String username;String password;//省略set、get、toString
}
单测:
@Autowired
private JdbcConfiguration jdbcConfiguration;
@Test
void test01() {System.out.println(jdbcConfiguration);
}

2.3 第三方进行属性绑定@Bean

将属性绑定到控件之外的第三方组件
@Configuration:声明一个类作为配置类
@ConfigurationProperties(prefix = "jdbc"):批量属性注入
@Bean:声明在方法上,将方法的返回值加入Bean容器
导包
<!-- 配置处理器 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
添加配置信息
src/main/resources/application.properties
another.enabled=true
another.remoteAddress=192.168.10.11
创建一个其他组件类
src/main/java/com/test/config/AnotherComponent.java
public class AnotherComponent {private boolean enabled;private InetAddress remoteAddress;//省略set、get、toString
}
创建绑定类
@Configuration
public class MyService {@ConfigurationProperties("another")@Beanpublic AnotherComponent anotherComponent(){return new AnotherComponent();}
}
单测
@Autowired
private AnotherComponent anotherComponent;
@Test
void test02() {System.out.println(anotherComponent);
}

3、热部署

目的:修改代码后,使项目可以自动替换更改文件并重新部署,解决本地验证缓慢的问题
导包
<!-- 引入热部署依赖 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId>
</dependency>

IDEA工具热部署设置
http://www.hkea.cn/news/731752/

相关文章:

  • 什么网站做简历模板宁德市医院
  • 用什么软件做公司网站游戏推广赚佣金的平台
  • 购物网站 后台模板河北seo技术培训
  • 聊城建设委员会官方网站google seo
  • 广西建设网郭业棚seo推广具体做什么
  • 武汉网站seo诊断谷歌下载官网
  • 做地方网站能赚钱吗免费seo网站诊断
  • 图片设计在线网站推广优化外包便宜
  • 武汉平价做网站网络软文推广案例
  • 新产品线上推广方案鞍山seo外包
  • 网站建网站建设和优佛山网络推广培训
  • 毕业设计做网站怎么样微信crm管理系统
  • 个人网站开发多少钱电脑培训班零基础
  • 互联网有哪些岗位宁波免费seo在线优化
  • 惠州做棋牌网站建设哪家技术好哪里的网络推广培训好
  • 如何做线上赌博的网站推广策略有哪些方法
  • 男的女的做那个视频网站百度收录需要多久
  • 大通县wap网站建设公司网站免费制作
  • 哪个网站教做公众号甘肃百度推广电话
  • 网站怎么让百度收录广告网络推广
  • 小型网站设计及建设论文定制网站制作公司
  • 视频网站建设费用排名优化网站seo排名
  • 怎么自己做网站服务器linux百度账号查询
  • 梧州网站推广方案百度热搜 百度指数
  • 网站不兼容ie6自助建站模板
  • 甘肃网站建设公司百中搜优化软件
  • 国内外贸网站建设公司seo教程 百度网盘
  • 一物一码二维码生成系统最好用的系统优化软件
  • 如何在大网站做外链镇江网站建站
  • 杭州网站建设公司导航短视频营销案例