1.创建maven 工程
2.引入springboot 必须的依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!-- 配置数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>com.carrotsearch.randomizedtesting</groupId>
<artifactId>junit4-ant</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置springboot的配置文件application.properties
#配置数据库连接参数
spring.datasourc.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/student
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#jpa配置
spring.jpa.database=MYSQL
spirng.jpa.showsql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#mybatis的配置
mybatis.config=classpath:/config/sqlMapConfig.xml
mybatis.type-handlers-package=classpath:com/example/demo
mybatis.mapper-locations.classpath=classpath:/mapper/*.xml
#配置jsp的映射
4.加入mybatis 的核心配置文件和mapper映射文件
5.配置扫描器
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.HttpStatus;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
@EnableJpaRepositories(basePackages = "cn.example.demo.dao.StudentDao")
@EntityScan(basePackages = "cn.example.demo.pojo")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
使用spring的 时候该怎样注入OK!
springboot 的application.property文件
#配置mybatis参数
mybatis.type-aliases-package=com.example.demo.vo
mybatis.mapper-locations=classpath:mapper/*.xml
#配置jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
logging.level.com.example.demo.dao.level=debug
#配置redis缓存
#spring.redis.host=192.168.242.128
#spring.redis.port=6379
#spring.redis.password=123456
#spring.redis.timeout.seconds=0
#spring.redis.jedis.pool.max-idle=8
#logging.level.com.example.demo.dao=debug
#spring.redis.jedis.pool.min-idle=0
#spring.redis.jedis.pool.max-active=8
#spring.redis.jedis.pool.max-wait=-1
使用spirngboot的时候遇到的异常
1.404静态资源 文件访问不到
处理方式一
1.spring.mvc.static-path-pattern=/static/**
2.添加后依旧访问不到,文件没有复制pom 中添加复制文件
3.不需要EnableMvc
2.mapper 注入失败的解决方式
在启动类上添加 MapperScan注解