码云地址:https://gitee.com/hjc2/springboot.git
需要添加的依赖如下:
<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>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
配置文件:
server.port=8081
#配置链接地址
#用户名
spring.datasource.druid.username=root
#密码
spring.datasource.druid.password=123456
#连接地址
spring.datasource.druid.url=jdbc:mysql://localhost:3306/spider_factory?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#驱动
spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
#设置mybatis
#实体映射的包
mybatis.type-aliases-package=com.dream.entity
#xml文件所在的包
mybatis.mapper-locations=classpath:static/mapper/*.xml
#展示SQL
logging.level.com.dream.dao=debug
controller层:
@RestController
public class IndexController {
@Autowired
IndexService indexService;
@RequestMapping(value = "/index")
public String index() {
return String.valueOf(indexService.getCount());
}
}
service层:
public interface IndexService {
int getCount();
}
实现层:
@Service
public class IndexServiceImpl implements IndexService {
@Autowired
UserMapper UserMapper;
@Override
public int getCount() {
return UserMapper.getCount1();
}
}
dao层:映射xml文件的需要加注解 @Mapper
@Mapper
public interface UserMapper {
int getCount1();
}
欢迎关注我的微信公众号,更多内容将在微信公众号中发布: