欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot学习随笔

程序员文章站 2022-04-19 19:08:12
...

1.使用配置

   eclipse、jdk1.8、maven3.5.4、tomcat8、MySQL

2.新建maven web项目,容易出现新建不成功,缺少maven-archetype-webapp-1.0-alpha-4.jar之类的,百度搜索该jar包,保存之后,cmd管理员权限进入到该jar包保存路径下,执行以下命令(注:后边jar包名称为下载名称),如果该命令执行失败,检查maven、jdk配置是否完成,jdk和maven版本是否匹配,之后执行命令mvn clean;最后再执行以下命令,再去eclipse新建项目即可

mvn install:install-file -DgroupId=org.apache.maven.archetypes -DartifactId=maven-archetype-webapp -Dversion=1.0 -Dpackaging=jar -Dfile=maven-archetype-webapp-1.0-alpha-4.jar

3.新建完毕之后应不会报错

修改pom文件,也可一个一个添加,在这我的pom文件做参考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>cn.miliyf</groupId>
	<artifactId>yiyang</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Maven Webapp Archetype</name>
	<url>http://maven.apache.org</url>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
		<mysql-connector>5.1.39</mysql-connector>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- Spring Boot Mybatis 依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-legacy</artifactId>
			<version>1.1.0.RELEASE</version>
		</dependency>
		<!-- MySQL 连接驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector}</version>
		</dependency>
	</dependencies>
	<!--<build> <finalName>yiyang</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> 
		<artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> 
		<mainClass>com.miliyf.ServletInitializer</mainClass> </configuration> <executions> 
		<execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> 
		</plugin> </plugins> </build> -->

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<encoding>utf8</encoding>
					<skipTests>true</skipTests>
					<!-- 打成war包时名子 -->
					<warName>yang</warName>
					<!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
					<mainClass>com.miliyf.YiyangApplication</mainClass>
					<skip>true</skip><!-- 跳过测试 -->
					<testFailureIgnore>true</testFailureIgnore>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

新建YiyangApplication类该类一定要在controller层级之上,不然springboot会扫描不到

//Spring Boot 应用的标识
@SpringBootApplication
@ComponentScan("com.miliyf")
//@MapperScan("com.miliyf.fan.dao")
@EnableScheduling
public class YiyangApplication {

	protected final static Logger logger = LoggerFactory.getLogger(YiyangApplication.class);

	public static void main(String[] args) {
		System.out.println("--------start--------");
		SpringApplication.run(YiyangApplication.class, args);
		logger.info("YiyangApplication is sussess!");
		System.out.println("--------end--------");
	}

}

新建controller控制类(@RequestMapping可以不写,运行main方法也可以运行访问http://localhost:8080即可;写的话类名,方法名之上都要写,不然会访问不到)--至此可以main方法运行访问

@Controller
@RequestMapping("/yiyang")
public class SampleController {

	@Autowired
	private userService userService;

	@RequestMapping("/yang")
	public String sayHello() {
		System.out.println("------------------123");
		return "index";
	}
}

4.部署到Tomcat上

新建ServletInitializer类extends SpringBootServletInitializer类,与YiyangApplication类同级别

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ServletInitializer extends SpringBootServletInitializer{

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		// TODO Auto-generated method stub
		System.out.println("======================start==========================");
		return builder.sources(YiyangApplication.class);
	}

}

5,使用eclipse打成war包,将war包复制到tomcat的webapp下,启动bin目录下的startup.bat--至此访问http://localhost:8080/yiyang/yang

6.记得在resources下新建文件templates/index.html;application.properties

该文件内容作参考

## shujuku
spring.datasource.url=jdbc:mysql://localhost:3306/yiyang
spring.datasource.username=yang
spring.datasource.password=yang
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true

spring:
  thymeleaf:
    encoding: UTF-8
    mode: HTML5
    prefix: classpath:/templates/
    suffix: .html
    check-template: true
    cache: false
    
mybatis.typeAliasesPackage=com.miliyf.fan.entity
mybatis.mapperLocations=classpath:/mybatis/mapper/*.xml

# 
spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

application.hello=Hello GOD

logging.path=/user/local/log
logging.level.com.favorites=DEBUG
logging.level.org.springframework.web=INFO
logging.level.org.hibernate=ERROR
logging.level.com.miliyf=debug

每个人有每个人遇到的问题,仅适合本人,共享作为参考