springBoot整合mybatis、前端界面
pom.xml
<?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.baizhi</groupId>
<artifactId>springBoot-day2-song</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springBoot-day2-song Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<!--
maven工程是可以继承
作用:继承一个springboot的父工程,可以使用父工程中声明的依赖,不需要指定版本号
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--springBoot所需依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--使用mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!--结合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--让springboot识别webapp下的前端界面所需的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!--spring结合aop-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--java使用elasticsearch-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.4.1</version>
</dependency>
<!--springboot使用elasticsearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springBoot-day2-song</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.4.RELEASE</version>
<!--使用热部署出现中文乱码解决方案-->
<configuration>
<fork>true</fork>
<!--增加jvm参数-->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
#端口号
server.port=8082
#项目名
server.servlet.context-path=/springBoot-day1
#配置数据源
spring.datasource.url=jdbc:mysql://localhost:3306/third_stage?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis的相关配置
#1、注册mapper文件
mybatis.mapper-locations=classpath:mappers/*.xml
#2、实体类起别名
mybatis.type-aliases-package=com.baizhi.entity
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
application.yml(与application.properties二选一)
server:
port: 8082
servlet:
context-path: /spring-day2
spring:
datasource:
url: jdbc:mysql://localhost:3306/third_stage?useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#date格式时间
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
#设置上传大小
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
#配置上传目录
resources:
static-locations: file:${img-path}
#pagehelper分页插件配置
pagehelper:
helperDialect: mysql
#mybatis相关配置
#注册mapper文件
mybatis:
mapper-locations: classpath:mappers/*.xml
#实体类起别名,简化mapper的配置
type-aliases-package: com.baizhi.entity
#配置上传目录(用于文件上传下载)
img-path: d:\\images
springBoot内置tomcat需要写一个启动函数
package com.baizhi;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//在启动类上边增加注解,扫描该包下的DAO接口
@MapperScan("com.baizhi.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
使用注解测试时的代码
import com.baizhi.Application;
import com.baizhi.dao.BookDAO;
import com.baizhi.entity.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class TestBook {
@Autowired
private BookDAO bookDAO;
@Test
public void select(){
List<Book> books = bookDAO.selectAll();
System.out.println("books = " + books);
}
}
springBoot整合事务控制
添加@Transactional注解
@Transactional 注解可以加在类和方法之上,方法上优先级更高加在类上边对整个类中的方法起作用