STS: Spring Boot and MyBatis
2016-03-01 22:31
615 查看
这篇文章在上一篇 STS: Spring Boot and Spring MVC 的基础上,说说使用 MyBatis 访问数据库的事儿。
工具:Spring Tool Suite Version: 3.7.3.RELEASE
数据库:MySQL Example Database: world
在接下来的代码中,将遵照 Spring Boot 简化编码的思想,使用最简单的方法实现代码。这包括三个方面:
1. 使用 mybatis-spring-boot-starter 集成 MyBatis,参阅:Spring Boot 集成 MyBatis
2. 使用 @Select 注解实现 SQL 映射,参阅:Spring Boot MyBatis 连接数据库
3. 使用 @Service、@Controller 注解实现 Bean 定义,参阅:注解实现 Bean 定义 使用 Java 配置进行 Spring bean 管理 装配bean,spring XML格式配置
添加完成后,Dependencies 显示如下图所示:
最后点击工具栏的 Save 按钮,将更改保存到 pom.xml 文件,并自动下载相关 .jar 文件。
展开 src/main/java 节点,在 com.example 节点上点击鼠标右键,在弹出菜单上选择 New -> Folder 项,弹出 New Folder 对话框,Folder name 输入 domain,然后点击〖Finish〗按钮创建文件夹。
重复上面的步骤,依次创建 mapper、service 和 web 文件夹。完成后的项目结构如下图所示:
添加 @MapperScan 注解后的 MybatisExampleApplication.java 内容如下所示:
新创建的 Country.java 文件会自动打开,编辑文件内容如下:
新创建的 CountryMapper.java 文件会自动打开,编辑文件内容如下:
新创建的 CountryService.java 文件会自动打开,编辑文件内容如下:
新创建的 CountryController.java 文件会自动打开,编辑文件内容如下:
程序启动后,打开浏览器,在地址栏中输入 http://localhost:8080 即可查看运行结果。
运行结果如下图:
参考:mybatis-spring-boot-sample-annotation
工具:Spring Tool Suite Version: 3.7.3.RELEASE
数据库:MySQL Example Database: world
在接下来的代码中,将遵照 Spring Boot 简化编码的思想,使用最简单的方法实现代码。这包括三个方面:
1. 使用 mybatis-spring-boot-starter 集成 MyBatis,参阅:Spring Boot 集成 MyBatis
2. 使用 @Select 注解实现 SQL 映射,参阅:Spring Boot MyBatis 连接数据库
3. 使用 @Service、@Controller 注解实现 Bean 定义,参阅:注解实现 Bean 定义 使用 Java 配置进行 Spring bean 管理 装配bean,spring XML格式配置
新建 Spring Starter Project
点击菜单 File -> New -> Spring Starter Project,打开 New Spring Starter Project 对话框,将 Name 改为 mybatis-example,然后点击〖Next >〗按钮,进入下一个对话框。展开 SQL 节点,选中 MySQL,再展开 Template Engines,选中 Thymeleaf。然后点击〖Finish〗按钮,创建 mybatis-example 项目。添加 mybatis-spring-boot-starter Dependency
打开 pom.xml 文件,切换到 dependencies 选项卡,点击〖Add…〗按钮,弹出 Select Dependency 对话框,Group Id 输入 org.mybatis.spring.boot,Artifact Id 输入 mybatis-spring-boot-starter,Version 输入 1.0.0,然后点击〖 OK 〗按钮完成添加。如下图所示:添加完成后,Dependencies 显示如下图所示:
最后点击工具栏的 Save 按钮,将更改保存到 pom.xml 文件,并自动下载相关 .jar 文件。
设置 DataSource 信息
展开 src/main/resources 节点,打开 application.properties 文件,输入如下内容:spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/world spring.datasource.username=root spring.datasource.password=xxxxx
创建项目文件夹
参考 Spring 的典型项目结构创建 domain、mapper、service 和 web 四个文件夹。展开 src/main/java 节点,在 com.example 节点上点击鼠标右键,在弹出菜单上选择 New -> Folder 项,弹出 New Folder 对话框,Folder name 输入 domain,然后点击〖Finish〗按钮创建文件夹。
重复上面的步骤,依次创建 mapper、service 和 web 文件夹。完成后的项目结构如下图所示:
给启动类添加 @MapperScan 注解
展开 com.example 节点,打开 MybatisExampleApplication.java 文件,在 MybatisExampleApplication 类上添加如下注解:@MapperScan("com.example.mapper")
添加 @MapperScan 注解后的 MybatisExampleApplication.java 内容如下所示:
package com.example; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.mapper") public class MybatisExampleApplication { public static void main(String[] args) { SpringApplication.run(MybatisExampleApplication.class, args); } }
创建数据模型
在 com.example.domain 节点上点击鼠标右键,在弹出菜单上选择 New -> Class 项,弹出 New Java Class 对话框,在 Name 栏输入 Country,然后点击〖Finish〗按钮创建 Country 类。新创建的 Country.java 文件会自动打开,编辑文件内容如下:
package com.example.domain; public class Country { private String code; private String code2; private String name; private String continent; private int population; private float surfaceArea; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getCode2() { return code2; } public void setCode2(String code2) { this.code2 = code2; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContinent() { return continent; } public void setContinent(String continent) { this.continent = continent; } public int getPopulation() { return population; } public void setPopulation(int population) { this.population = population; } public float getSurfaceArea() { return surfaceArea; } public void setSurfaceArea(float surfaceArea) { this.surfaceArea = surfaceArea; } @Override public String toString() { return this.name; } }
创建映射接口
在 com.example.mapper 节点上点击鼠标右键,在弹出菜单上选择 New -> Interface 项,弹出 New Java Interface 对话框,在 Name 栏输入 CountryMapper,然后点击〖Finish〗按钮创建 CountryMapper 接口。新创建的 CountryMapper.java 文件会自动打开,编辑文件内容如下:
package com.example.mapper; import java.util.List; import org.apache.ibatis.annotations.Select; import com.example.domain.Country; public interface CountryMapper { @Select("SELECT code, code2, name, continent, population, surfaceArea FROM country ORDER BY name") List<Country> findAll(); }
创建 Service
在 com.example.service 节点上点击鼠标右键,在弹出菜单上选择 New -> Class 项,弹出 New Java Class 对话框,在 Name 栏输入 CountryService,然后点击〖Finish〗按钮创建 CountryService 类。新创建的 CountryService.java 文件会自动打开,编辑文件内容如下:
package com.example.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.domain.Country; import com.example.mapper.CountryMapper; @Service public class CountryService { @Autowired private CountryMapper countryMapper; public List<Country> findAll() { return countryMapper.findAll(); } }
创建 Controller
在 com.example.web 节点上点击鼠标右键,在弹出菜单上选择 New -> Class 项,弹出 New Java Class 对话框,在 Name 栏输入 CountryController,然后点击〖Finish〗按钮创建 CountryController 类。新创建的 CountryController.java 文件会自动打开,编辑文件内容如下:
package com.example.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.example.service.CountryService; @Controller public class CountryController { @Autowired private CountryService countryService; @RequestMapping("/") public String countryList(Model model) { model.addAttribute("countries", countryService.findAll() ); return "countryList"; } }
创建模板
在 src/main/resources 下面的 templates 文件夹内创建 CountryList.html 文件,编辑文件内容如下:<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>国家</title> </head> <body> <table> <thead> <tr> <th>代码</th> <th>名称</th> <th>面积</th> <th>人口</th> <th>洲</th> </tr> </thead> <tbody> <tr th:each="country : ${countries}"> <td th:text="${country.code2}"></td> <td th:text="${country.name}"></td> <td th:text="${#numbers.formatDecimal(country.surfaceArea,1,0)}"></td> <td th:text="${country.population}"></td> <td th:text="${country.continent}"></td> </tr> </tbody> </table> </body> </html>
完成后的项目结构
经过上面这些步骤,我们已经完成了这个项目。最终的项目结构如下图所示:运行程序
在项目名称 mybatis-example 上点击鼠标右键,在弹出菜单上选择 Run As -> Spring Boot App。程序启动后,打开浏览器,在地址栏中输入 http://localhost:8080 即可查看运行结果。
运行结果如下图:
参考:mybatis-spring-boot-sample-annotation
相关文章推荐
- 深入浅析mybatis oracle BLOB类型字段保存与读取
- MyBatis MapperProvider MessageFormat拼接批量SQL语句执行报错的原因分析及解决办法
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- MyBatis学习笔记(二)之关联关系
- 浅析Mybatis 在CS程序中的应用
- Java Mybatis框架入门基础教程
- Java简单实现SpringMVC+MyBatis分页插件
- MyBatis批量添加、修改和删除
- MyBatis与Hibernate的比较
- 浅析mybatis和spring整合的实现过程
- mybatis的动态sql详解(精)
- Mybatis实现增删改查及分页查询的方法
- MyBatis入门学习教程(一)-MyBatis快速入门
- Spring与Mybatis的整合方法有哪些
- 详解MyBatis直接执行SQL查询及数据批量插入
- 解决springmvc+mybatis+mysql中文乱码问题
- Spring3.1.1+MyBatis3.1.1的增、删、查、改以及分页和事务管理
- Spring+Mybatis+Mysql搭建分布式数据库访问框架的方法
- Mybatis实战教程之入门到精通(经典)
- Mybatis与Hibernate的区别