Spring Boot + MyBatis + SQLite 基
2018-04-29 本文已影响476人
简了个书1993
IDE:IntelliJ IDEA 2017.3.5
Spring Boot:2.0.1
一、创建项目
1. 选择 Spring Initializr
09eb793a3caefb152. 要使用 JSP 开发,这里需要选择 war 包方式
4be014ad4a208d1c3. 这里选择 Web 和 MyBatis 两个依赖项
3a67ced14c401d35下一步,然后 Finish 完成
二、pom.xml 配置
1. 找到 spring-boot-starter-tomcat 删除 <scope>provided</scope>
2. 添加如下两个依赖
<!-- JSP 必要依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- SQLite 驱动 -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.21.0.1</version>
</dependency>
三、JSP 配置(不需要可直接跳过)
1. 在 src/main 目录下完善目录结构,如下图
28f55089c4a6165cviews 目录是用来存放 jsp 文件
2. 配置视图
打开配置文件 application.properties
配置路径和后缀
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
四:数据库配置
1. 创建数据库
2854C3B8-7EEC-4D26-8155-4F793D715703.png F474C110-7AAA-42CF-9686-20785325590A.png2. 创建表
30a804457d93f8283. 打开配置文件 application.properties
加入数据库配置(url 千万不要配置错了,可直接copy上边创建数据库的URL)
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:/Users/Chance/Documents/db_demo.db
spring.datasource.username=
spring.datasource.password=
五、正式进入coding
1. 创建包 controller、mapper、service、model
866ba178704de4222. 创建 model DemoModel 对应数据库字段
public class DemoModel {
private Integer id;
private String key;
private String value;
// 省略 getter 和 setter
}
3. 创建 mapper DemoMapper
@Mapper
@Component
public interface DemoMapper {
// 插入 并查询id 赋给传入的对象
@Insert("INSERT INTO tb_test(key, value) VALUES(#{key}, #{value})")
@SelectKey(statement = "SELECT seq id FROM sqlite_sequence WHERE (name = 'tb_test')", before = false, keyProperty = "id", resultType = int.class)
int insert(DemoModel model);
// 根据 ID 查询
@Select("SELECT * FROM tb_test WHERE id=#{id}")
DemoModel select(int id);
// 查询全部
@Select("SELECT * FROM tb_test")
List<DemoModel> selectAll();
// 更新 value
@Update("UPDATE tb_test SET value=#{value} WHERE id=#{id}")
int updateValue(DemoModel model);
// 根据 ID 删除
@Delete("DELETE FROM tb_test WHERE id=#{id}")
int delete(Integer id);
}
4. 创建 service DemoService
@Service
public class DemoService {
private final DemoMapper dao;
@Autowired
public DemoService(DemoMapper dao) {
this.dao = dao;
}
public boolean insert(DemoModel model) {
return dao.insert(model) > 0;
}
public DemoModel select(int id) {
return dao.select(id);
}
public List<DemoModel> selectAll() {
return dao.selectAll();
}
public boolean updateValue(DemoModel model) {
return dao.updateValue(model) > 0;
}
public boolean delete(Integer id) {
return dao.delete(id) > 0;
}
}
5. 创建控制器 controller DemoController
@Controller
public class DemoController {
private final DemoService service;
@Autowired
public DemoController(DemoService service) {
this.service = service;
}
/**
* JSP 测试
* @return JSP 视图
*/
@GetMapping("/demo")
public ModelAndView demo() {
ModelAndView mv = new ModelAndView("demo"); // 对应 demo.jsp 路径
mv.addObject("value", "测试值");
return mv;
}
/**
* 接口测试
* @return JSON 字符串
*/
@GetMapping("/demos")
@ResponseBody
public List<DemoModel> allDemo() {
return service.selectAll();
}
// @ResponseBody 如果返回的是对象 会自动转为json字符串,如果返回的是String 则返回该字符串
}
6. 创建 JSP 视图 demo.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
demo.jsp
value: ${value}
</body>
</html>
六、到这里,项目已经可以运行了
最终的项目结构
2a919fb12abd0b82七、简单的测试
1. 利用项目自动生成的测试类,对 DemoService 进行简单的单元测试
DemoApplicationTests 类代码如下
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private DemoService service;
@Test
public void contextLoads() {
DemoModel model = new DemoModel();
model.setKey("测试key1");
model.setValue("测试value1");
// 记录数
List<DemoModel> all = service.selectAll();
int size = all.size();
// insert
boolean result = service.insert(model);
Assert.assertTrue(result);
// select
DemoModel selectModel = service.select(model.getId());
Assert.assertNotNull(selectModel);
// selectAll
all = service.selectAll();
Assert.assertEquals(size + 1, all.size());
// updateValue
selectModel.setValue("测试更改value1");
result = service.updateValue(selectModel);
Assert.assertTrue(result);
// delete
result = service.delete(selectModel.getId());
Assert.assertTrue(result);
}
}
2. 启动测试
4da2bcb0f407b617如果没出现异常,说明测试通过
3. 启动服务器
选择 DemoApplication 并点击 ️ 启动
684c768e94daee8d在浏览器测试
查看 JSP 页面是否正常
http://localhost:8080/test
看到下面的界面说明成功
34ddb37989267eb2测试接口
http://localhost:8080/demos
应当返回数据库全部记录
[{"id":4,"key":"测试key1","value":"测试value1"},{"id":5,"key":"测试key1","value":"测试value1"},{"id":6,"key":"测试key1","value":"测试value1"},{"id":7,"key":"测试key1","value":"测试value1"}]
完整代码已上传至 GitHub:
https://github.com/kssion/SpringBootDemo.git
不断学习积累,才能提升自身!