文章目錄
- 一、創建SpringBoot項目
- 二、添加Mybatis相關依賴
- 三、數據源配置
- 四、創建事務的模型實體類
- 五、創建和數據庫交互聯系的映射關系類
- 六、創建業務接口和實現類
- 七、創建控制器類
- 八、請求驗證
一、創建SpringBoot項目
如何創建詳見:IDEA 創建 SpringBoot 項目
二、添加Mybatis相關依賴
以前開發Web項目我們都知道要想把數據添加到數據庫,不僅必須要數據庫的驅動程序,還要有各種各樣的配置文件,像java Bean配置,數據源配置,對象和數據庫字段的映射配置等等。使用SpringBoot開發,我們只需要加入依賴文件就可以了,SpringBoot已經都幫我配置好了
。配置如下圖所示:
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.1</version>
</dependency>
三、數據源配置
在application.properties中配置數據庫連接的相關信息:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:18103/db_test?characterEncoding=GBK
spring.datasource.username=root
spring.datasource.password=root
四、創建事務的模型實體類
編程是利用面向對象的思想把自然界中的事物抽象成模型,利用模型來解決實際中的問題。如下圖:
package com.springboottest.bean;public class StudentBean {private int id;private String name;public StudentBean() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}
注:這里的字段名稱與數據庫表字段名稱一致。
五、創建和數據庫交互聯系的映射關系類
這個類主要是和數據進行交互聯系的,需要配置好實體類和數據庫字段的映射關系。由于SpringBoot已經做了大量的工作,我們只需要做好相關注解就可以使用了。如下圖所示:
package com.springboottest.sql.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;@Mapper
public interface StudentMapper {@Select("select * from tb_student where name=#{name}")StudentBean getStudentInfoByName(String name);
}
@Mapper
表明該類是一個Mapper接口,使用@Select
、@Insert
等注解我們可以直接在類中書寫sql語句來實現我們的目的。
六、創建業務接口和實現類
我們在接口類里定義要實現的業務功能接口,在它的實現類里實現接口。接口類如下圖:
package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;public interface StudentService {StudentBean getStudentInfoByName(String name);
}
實現類如下圖:
package com.springboottest.sql.service;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.mapper.StudentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class StudentServiceImpl implements StudentService{@Autowiredprivate StudentMapper studentMapper;@Override@Transactionalpublic StudentBean getStudentInfoByName(String name) {return studentMapper.getStudentInfoByName(name);}
}
@Service
注解表明它是一個服務類Bean,可以被SpringBoot識別使用,相當于以前在xml里配置的bean。
七、創建控制器類
Web項目的請求經過映射找到控制器類里對應的方法,然后再實現完業務返回響應信息。如下圖:
package com.springboottest.controller;import com.springboottest.bean.StudentBean;
import com.springboottest.sql.MySQLProcessor;
import com.springboottest.sql.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/mysql")
public class SqlController {@Autowiredprivate StudentService studentService;@RequestMapping(value = "/student")public String studentSelect(@RequestParam String name){StudentBean bean = studentService.getStudentInfoByName(name);if(bean != null){return "Name = " + bean.getName();} else {return "null";}}
}
八、請求驗證
請求地址:http://localhost:8991/mysql/student?name=tom