4.0.0
spring-cloud-microservice
org.example
1.0-SNAPSHOT
com.example
microservice-cloud-provider-dept-8001
0.0.1-SNAPSHOT
microservice-cloud-provider-dept-8001
Demo project for Spring Boot
1.8
org.example
microservice-cloud-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
com.alibaba
druid
org.projectlombok
lombok
true
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
junit
junit
test
org.springframework
springloaded
1.2.8.RELEASE
ch.qos.logback
logback-core
org.apache.logging.log4j
log4j-core
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
server:
port: 8001
spring:
application:
name: microservice-cloud-provider-dept-8001 #微服务名称,对外暴漏的微服务名称,十分重要
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/springcloud_db_core?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimeZone=UTC
type: com.alibaba.druid.pool.DruidDataSource
#SpringBoot默认是不注入这些的,需要自己绑定
#druid数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许报错,java.lang.ClassNotFoundException: org.apache.Log4j.Priority
#则导入log4j 依赖就行
filters: stat,wall,log4j2
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
#整合mybatis
mybatis:
type-aliases-package: com.zk.springcloud.entity
mapper-locations: classpath:mybatis/mapper/*.xml
#config-location: classpath:mybatis/mybatis-config.xml
configuration:
map-underscore-to-camel-case: true #默认开启驼峰命名法,可以不用设置该属性
注意:数据源中的url不需要对:进行转义
package com.example.mapper;
import com.zk.springcloud.entity.Dept;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface DeptMapper {
boolean addDept(Dept dept);
//根据主键获取数据
Dept queryDeptByID(Integer deptNo);
//获取表中的全部数据
List queryAll();
}
dept_no,dept_name,db_source
INSERT INTO springcloud_db_core.dept(dept_name,db_source)
VALUES(#{deptName},DATABASE())
问题:将jdbcType="Integer"类型写为小写的类型
连续按两次Shift键,开启IDEA的全局搜索,并搜索JdbcType,查找给定的常量。
结果发现给定的枚举常量为INTEGER,而不是Integer
将Mapper.xml中所有的jdbcType=“Integer”,改为jdbcType="INTEGER"大写的类型即可成功解决
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
package org.apache.ibatis.type;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
/**
* @author Clinton Begin
*/
public enum JdbcType {
/*
* This is added to enable basic support for the
* ARRAY data type - but a custom type handler is still required
*/
ARRAY(Types.ARRAY),
BIT(Types.BIT),
TINYINT(Types.TINYINT),
SMALLINT(Types.SMALLINT),
INTEGER(Types.INTEGER),
BIGINT(Types.BIGINT),
FLOAT(Types.FLOAT),
REAL(Types.REAL),
DOUBLE(Types.DOUBLE),
NUMERIC(Types.NUMERIC),
DECIMAL(Types.DECIMAL),
CHAR(Types.CHAR),
VARCHAR(Types.VARCHAR),
LONGVARCHAR(Types.LONGVARCHAR),
DATE(Types.DATE),
TIME(Types.TIME),
TIMESTAMP(Types.TIMESTAMP),
BINARY(Types.BINARY),
VARBINARY(Types.VARBINARY),
LONGVARBINARY(Types.LONGVARBINARY),
NULL(Types.NULL),
OTHER(Types.OTHER),
BLOB(Types.BLOB),
CLOB(Types.CLOB),
BOOLEAN(Types.BOOLEAN),
CURSOR(-10), // Oracle
UNDEFINED(Integer.MIN_VALUE + 1000),
NVARCHAR(Types.NVARCHAR), // JDK6
NCHAR(Types.NCHAR), // JDK6
NCLOB(Types.NCLOB), // JDK6
STRUCT(Types.STRUCT),
JAVA_OBJECT(Types.JAVA_OBJECT),
DISTINCT(Types.DISTINCT),
REF(Types.REF),
DATALINK(Types.DATALINK),
ROWID(Types.ROWID), // JDK6
LONGNVARCHAR(Types.LONGNVARCHAR), // JDK6
SQLXML(Types.SQLXML), // JDK6
DATETIMEOFFSET(-155), // SQL Server 2008
TIME_WITH_TIMEZONE(Types.TIME_WITH_TIMEZONE), // JDBC 4.2 JDK8
TIMESTAMP_WITH_TIMEZONE(Types.TIMESTAMP_WITH_TIMEZONE); // JDBC 4.2 JDK8
public final int TYPE_CODE;
private static Map codeLookup = new HashMap<>();
static {
for (JdbcType type : JdbcType.values()) {
codeLookup.put(type.TYPE_CODE, type);
}
}
JdbcType(int code) {
this.TYPE_CODE = code;
}
public static JdbcType forCode(int code) {
return codeLookup.get(code);
}
}
package com.example.service;
import com.zk.springcloud.entity.Dept;
import java.util.List;
public interface DeptService {
boolean addDept(Dept dept);
//根据主键获取数据
Dept queryDeptByID(Integer deptNo);
//获取表中的全部数据
List queryAll();
}
package com.example.service;
import com.example.mapper.DeptMapper;
import com.zk.springcloud.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author CNCLUKZK
* @create 2022/9/7-1:42
*/
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptMapper deptMapper;
@Override
public boolean addDept(Dept dept) {
return deptMapper.addDept(dept);
}
@Override
public Dept queryDeptByID(Integer deptNo) {
return deptMapper.queryDeptByID(deptNo);
}
@Override
public List queryAll() {
return deptMapper.queryAll();
}
}
package com.example.controller;
import com.example.service.DeptService;
import com.zk.springcloud.entity.Dept;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author CNCLUKZK
* @create 2022/9/7-1:43
*/
@RestController //提供restful服务,@RestController需要spring-boot-starter-web
//@Slf4j
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/add") //GET跟安不安全没关系的,RESTful规范问题
public boolean addDept(@RequestBody Dept dept) {
return deptService.addDept(dept);
}
@GetMapping("/get/{deptNo}")
public Dept getDeptByID(@PathVariable("deptNo") Integer deptNo) {
return deptService.queryDeptByID(deptNo);
}
@GetMapping("/list")
public List queryAll() {
return deptService.queryAll();
}
}
开发期间存在的问题1:Spring Boot项目中含有Mybatis,打Jar包运行之后,报如下错误.
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
问题原因:Mybatis没有找到合适的驱动,即spring :datasource:url没有加载成功
- DataSourceAutoConfiguration会自动加载.
- spring :datasource:url 属性配置有误,如地址格式有问题
- spring :datasource:url属性配置所在的yml,properties文件没有被加载
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
spring:
application:
name: microservice-cloud-provider-dept-8001
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
url: jdbc:mysql://localhost:3306/springcloud_db_core?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimeZone=UTC
type: com.alibaba.druid.pool.DruidDataSource
//错误示例
spring.datasource.url = jdbc:mysql\://localhost\:3306/springcloud_db_core?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimeZone=UTC
src/main/resources
**/*.yml
**/*.properties
**/*.xml
true
src/main/java
**/*.yml
**/*.properties
**/*.xml
true
问题2:新导入项目,提示报 Cannot access org.springframework.context.ConfigurableApplicationContext的错