标签:ref 配置 jar factory 执行 请求 connector 字符 驱动
5.等待项目下载完成,下面的读条全部完成后,我们就可以点开项目查看项目的结构是否完整
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 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>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--Druid 数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--使用的MySQL是8.0 使用5.*的请忽略-->
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
# 配置数据库连接 8.0
spring:
datasource:
# =====================MySQL相关配置=====================
# 数据库类型
name: mysql
#数据库连接驱动
driver-class-name: com.mysql.cj.jdbc.Driver
#数据库连接url
url: jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
#数据库连接账号
username: root
#数据库连接密码
password: root
#使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
#==================druid相关配置=========================================
druid:
#监控统计用的filter:stat 日志用filter:log4j 防御sql注入用filter:wall
filters: stat,log4j,wall
#最大链接数量
max-active: 20
#初始化时建立物理连接的个数 初始化发生在显示调用init() 或者第一次getConnection时
initial-size: 1
#获取连接时的最大等待时间 毫秒
max-wait: 60000
#最小连接池数量
min-idle: 1
time-between-eviction-runs-millis: 60000
#链接保持空闲而不被驱逐的最长时间
min-evictable-idle-time-millis: 300000
#用来检验连接是否有效的sql mysql为select 1
# 如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用
validation-query: select 1
# 申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
test-while-idle: true
# 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
test-on-borrow: false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
test-on-return: false
# 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
pool-prepared-statements: false
# 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true
max-open-prepared-statements: -1
# ====================mybatis==============================
mybatis:
#配置mybatis的mapper xml文件映射
mapper-locations: classpath*:mapper/*.xml
#配置pojo映射
type-aliases-package: com.example.demo.pojo
#服务端口
server:
port: 8080
DruidConfig
package com.example.demo.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
//标记这是一个配置类
@Configuration
public class DruidConfig {
//配置Druid的Bean 读取yml配置中spring.datasource为前缀的配置
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
public DataSource druid(){
return new DruidDataSource();
}
}
12.创建pojo,使用lombok的注解生成get和set方法
package com.example.demo.pojo;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
@Setter
@Getter
public class FtbAdmin implements Serializable {
private Integer id;
private String username;
private String password;
}
13.在dao下创建mapper接口(别忘了打上mapper注解),在resources/mapper下创建xml文件
mapper接口
package com.example.demo.dao;
import com.example.demo.pojo.FtbAdmin;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* @author 11699
* @date 2020/2/15 - 13:04
*/
@Mapper
public interface FtbAdminMapper {
public List<FtbAdmin> findAll();
}
mapper.xml
<?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.dao.FtbAdminMapper" >
<resultMap id="BaseResultMap" type="com.example.demo.pojo.FtbAdmin" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
select * from ftb_admin
</select>
</mapper>
14.在service包下创建service接口,在此包下新建impl包,创建service的实现类(别忘记打上service注解)
service接口
package com.example.demo.service;
import com.example.demo.pojo.FtbAdmin;
import java.util.List;
/**
* @author 11699
* @date 2020/2/15 - 13:14
*
*/
public interface FtbAdminService {
public List<FtbAdmin> findAll();
}
service实现类
package com.example.demo.service.impl;
import com.example.demo.dao.FtbAdminMapper;
import com.example.demo.pojo.FtbAdmin;
import com.example.demo.service.FtbAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author 11699
* @date 2020/2/15 - 13:16
*
* 服务层实现,不要忘记打上service注解
*/
@Service
public class FtbAdminServiceImpl implements FtbAdminService {
//注入mapper
@Autowired
private FtbAdminMapper adminMapper;
@Override
public List<FtbAdmin> findAll() {
return adminMapper.findAll();
}
}
package com.example.demo.controller;
import com.example.demo.pojo.FtbAdmin;
import com.example.demo.service.FtbAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author 11699
* @date 2020/2/15 - 13:29
*
* @RestController:返回整个controller返回的都是json字符串
* @RequestMapping("/admin"):请求映射
*/
@RestController
@RequestMapping("/admin")
public class FtbAdminController {
//注入服务层
@Autowired
private FtbAdminService adminService;
//添加映射 无需与方法名一样
@RequestMapping("/findAll")
public List<FtbAdmin> findAll(){
return adminService.findAll();
}
}
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//开启mapper接口扫描,指定扫描基础包
@MapperScan(basePackages = "com.example.demo.dao")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
18 启动没有报错的话,我们可以开始测试了在浏览器中输入地址↓
http://localhost:8080/admin/findAll
附上项目的完整结构
快速构建一个Spring Boot+MyBatis的项目IDEA(附源码下载)
标签:ref 配置 jar factory 执行 请求 connector 字符 驱动
原文地址:https://www.cnblogs.com/erkye/p/12311943.html