SpringBoot的搭建

1.第一步:登录网址  https://start.spring.io/ 下载SpringBoot的标准工程模板



2.将下载的ZIP压缩包放到工作空间文件夹下,并Import到eclipse中。


当把所有的jar包都导入成功后工程目录的结构如图所示。

3.启动该工程下的 DemoApplication中的main方法,表示该springboot已经正常启动了。


4.配置热启动,方便项目开发过程中进行快速地调试。

 在 pom.xml中添加如下代码

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
</plugins>
</build>

5.开发一个文件 在页面输出 Hello world 。

package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorld {
	 
	 @RequestMapping("/getUser")
	 public String getUser() {
		 return "你好我是leen";
	 }
	 
	 @RequestMapping("/Hello")
	 public String hello() {
		 return "hello world";
	 }
	  
}

在浏览器中输入 localhost:8080/Hello


这样一个最简单的通过Springboot输出文字到页面就完成了。


6.自定义一个Filter,对请求进行过滤。

①随意创建一个class文件在其头部位置加上@Configuration的注解,代码如下:

@Configuration
public class WebConfiguration {
    @Bean
    public RemoteIpFilter remoteIpFilter() {
        return new RemoteIpFilter();
    }
    
    @Bean
    public FilterRegistrationBean testFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new MyFilter());
        registration.addUrlPatterns("/*");
        registration.addInitParameter("paramName", "paramValue");
        registration.setName("MyFilter");
        registration.setOrder(1);
        return registration;
    }
    
    public class MyFilter implements Filter {
		@Override
		public void destroy() {
			// TODO Auto-generated method stub
		}

		@Override
		public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain)
				throws IOException, ServletException {
			// TODO Auto-generated method stub
			HttpServletRequest request = (HttpServletRequest) srequest;
			System.out.println("this is MyFilter,url :"+request.getRequestURI());
			filterChain.doFilter(srequest, sresponse);
		}

		@Override
		public void init(FilterConfig arg0) throws ServletException {
			// TODO Auto-generated method stub
		}
    }
}

② 在doFilter方法中打断点查看是否生效


7. 自定义Property

在application.properties中添加相应的键值对

com.neo.title=纯洁的微笑
com.neo.description=分享生活和技术

使用注解@Value将键值引入到工程中

@Component
public class NeoProperties {
	@Value("${com.neo.title}")
	private String title;
	@Value("${com.neo.description}")
	private String description;

	//省略getter settet方法

}

8.数据库连接

① 第一步需要添加ORACLE依赖。

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.1.0</version>
</dependency> 

tip: 在添加该依赖的过程中可能会遇到添加不到jar包的问题。这时需要手动下载ojdbc6.jar并使用mvn命令对jar包进行编译。cmd 切换到存放ojdbc6.jar的文件夹目录下并执行以下命令:

mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.1.0 -Dpackaging=jar -Dfile=ojdbc6.jar

② 第二步需要在排至文件中添加相应的配置参数。

# 连接数据库 oracel 
spring.jpa.database=oracle
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.username=his
spring.datasource.password=his
spring.jpa.hibernate.ddl-auto=update
③ 测试启动。 

    依次解决了如下BUG

     Not a managed type解决方法 

     https://blog.csdn.net/heyewu4107/article/details/78942393



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值