一、配置设置。
1.POM 引入相关依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/webapp</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
<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>
<configuration>
<webResources>
<resource>
<directory>src/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.文件结构如下
3.在启动类中添加对@MapperScan的扫描
@SpringBootApplication
@MapperScan("com.example.demo.mapper")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
}
4..在resources中创建application.yml文件,并编写配置
server:
# 端口号
port: 9011
spring:
thymeleaf.content-type: text/html
thymeleaf.cache: false
thymeleaf.mode: LEGACYHTML5
http:
multipart:
maxFileSize: 50Mb
maxRequestSize: 50Mb
profiles:
active: dev
logging:
level:
com.aisino.mapper: debug
# mybatis配置
mybatis:
mapper-locations: classpath:mapping/**/*.xml
type-aliases-package: com.example.entity
5.配置数据库
spring:
application:
name: service-ccic
datasource:
name: test
# 数据库连接
url: jdbc:mysql://fptyptxxxx
username: root
password: Root!cc # 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
filters: stat
# 最大活跃连接数
maxActive: 20
# 指定启动连接池时,初始建立的连接数量
initialSize: 1
# 最大等待时间
maxWait: 60000
# 指定必须保持连接的最小值
minIdle: 1
# 指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔
timeBetweenEvictionRunsMillis: 60000
# 指定一个空闲连接最少空闲多久后可被清除
minEvictableIdleTimeMillis: 300000
# 指定获取连接时连接校验的sql查询语句
validationQuery: select 'x'
# 当连接空闲时,是否执行连接测试
testWhileIdle: true
# 当从连接池借用连接时,是否测试该连接
testOnBorrow: false
# 设置当连接被归还时,是否要提交所有还未完成的事务
testOnReturn: false
# 指定是否池化statements
poolPreparedStatements: true
# 指定最大的打开的prepared statements数量
maxOpenPreparedStatements: 20
devtools:
restart:
# 不自动重启
enabled: true
livereload:
enabled: true
二、创建一个简单的查询接口完成菜品查询;
1..编写dao接口
public interface FoodMapper {
List<Map> selectFood(@Param("foodName")String foodName);
}
2.编写在resources文件中创建 mapping/FoodMapper.xml文件
注意:
1.namespace中需要与使用FoodMapper.java的接口对应
2.FoodMapper.xml文件名称必须与使用FoodMapper.java的接口一致
3.标签中的id必须与FoodMapper.java接口中的方法名一致,且参数一致
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.mapper.food.FoodMapper">
<select id="selectFood" parameterType="java.lang.String" resultType="map">
select * from t_dcxt_food where 1=1
and FOOD_NAME like #{foodName}
</select>
</mapper>
3.创建一个service
@Service
public class FoodService {
@Resource
private FoodMapper foodMapper;
public List<Map> foodService(String foodName) {
return foodMapper.selectFood(foodName);
}
}
4.创建一个接口Controller
@Controller
public class FoodController {
@Resource
private FoodService foodService;
@RequestMapping("/findFood")
@ResponseBody()
public List<Map> testFood() {
System.out.print(foodService.foodService("肉末茄子"));
return foodService.foodService("肉末茄子");
}
//http://localhost:9011/findFood
}