前言
旨在浏览器键入Url就能放回JSON数据
安装
简单结构
这个目录结构图比较简陋,可以去官网配置生成,idea生成.
依赖
在文件pom.xml
中添加一下三个依赖,SpringBoot
的Web模块,Mybatis
快速配置模块,以及Mysql
驱动
<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.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置
在默认配置文件application.properties
中配置mysql连接信息
spring.datasource.url=jdbc:mysql://xxx:33061/test
spring.datasource.username=test
spring.datasource.password=test123!
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
映射实体类
定义属性
/**
* @author terry
* @since 19/07/2019
*/
public class City {
private Long id;
private String name;
private String state;
private String country;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Mapper接口
定制访问数据库的sql
/**
* @author terry
* @since 19/07/2019
*/
@Mapper
public interface CityMapper {
@Select("select id, name, state, country from city where state = #{state}")
City findByState(@Param("state") String state);
}
接口控制器
注入资源CityMapper
,返回给浏览器
/**
* @author terry
* @since 19/07/2019
*/
@RestController
public class CityController {
@Resource
private CityMapper cityMapper;
@GetMapping(value = "/index")
public City info(){
return this.cityMapper.findByState("CA");
}
}
项目启动类
运行入口
/**
* https://github.com/tengxing/WebFrameCollection/SpringBootMybatis-Simple
* @author terry
* @since 04/06/2019
*/
@SpringBootApplication
public class SbApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(SbApplication.class, args);
}
}
数据库(跳过)
对数据库比较陌生的话,可以把如下代码放入项目启动类SbApplication
@Resource
SqlSessionFactory factory;
@Override
@SuppressWarnings("squid:S106")
public void run(String... args) throws SQLException {
initDb();
}
private void initDb() throws SQLException {
Connection conn = factory.openSession().getConnection();
Statement statement = conn.createStatement();
statement.addBatch("drop table if exists city;");
statement.addBatch("CREATE TABLE `city` (\n" +
" `id` bigint(20) DEFAULT NULL,\n" +
" `name` varchar(255) DEFAULT NULL,\n" +
" `state` varchar(255) DEFAULT NULL,\n" +
" `country` varchar(255) DEFAULT NULL\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" );
statement.addBatch("insert into city (name, state, country) values ('San Francisco', 'CA', 'US');");
System.out.println("Creating table in given database...");
statement.executeBatch();
System.out.println("Created table in given database...");
statement.close();
conn.close();
}
测试
代码示例:Github
后记
郑重说明:使用适用于对项目比较陌生,或者刚刚开始入手的开发人员,便于跑通项目流程,理解开发本质,快速上手,不适用于牛逼的老鸟。代码结构,业务逻辑隔离等等都先不考虑。
参考文章
mybatis官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html