bkt项目 搭建环境并测试
Posted fastfn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bkt项目 搭建环境并测试相关的知识,希望对你有一定的参考价值。
简介:这个项目是为了学习SpringBoot以及学习SpringCloud用的,如果对你有什么帮助,还是非常高兴的。
GitHub : https://github.com/fankf/bkt.git
技术使用 :SpringBoot + SSM + mysql
IDE : STS
代码:(此处只是测试代码,没有涉及到具体业务)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
配置:(SpringBoot省略了XML的配置,但是任何少许的配置都是要有的)
# mybatis mapper 配置
mybatis:
type-aliases-package: com.example.dao
# mysql 数据库链接配置
spring:
http:
encoding:
charset: UTF-8
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
#补充 druid数据库连接池配置
platform: mysql
type: com.alibaba.druid.pool.DruidDataSource
dbcp2:
initial-size: 2
max-idle: 10
min-idle: 2
max-wait-millis: 10000
Application入口代码:
@SpringBootApplication
@ComponentScan("com.example")
@MapperScan("com.example.dao")
public class BktApplication {
public static void main(String[] args) {
SpringApplication.run(BktApplication.class, args);
}
}
MyBatis注解方式映射数据库表:
public interface TestDao {
@Select("select * from test")
@Results({
@Result(property="id",column="id"),
@Result(property="testName",column="name")
})
List<Test> getTestList();
}
Entity:
public class Test {
private int id;
private String testName;
...此处省略构造器和get/set方法
}
Service:
public interface TestService {
public List<Test> getTestList();
}
ServiceImpl:
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestDao testDao;
@Override
public List<Test> getTestList() {
List<Test> list = testDao.getTestList();
return list;
}
}
Controller:
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping(value= {"/list",""},method=RequestMethod.GET)
public List<Test> getTestList(){
List<Test> testList = testService.getTestList();
return testList;
}
}
之后运行测试就ok!
以上是关于bkt项目 搭建环境并测试的主要内容,如果未能解决你的问题,请参考以下文章