Spring Boot 与 Mybatis、Mysql整合使用的例子
第一步: 创建一个SpringBoot的工程,在其中的Maven依赖配置中添加对JDBC、MyBatis、Mysql Driver的依赖具体如下:
<!-- JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
第二步:在SprintBoot的配置文件application-dev.yml中添加mybatis、数据源的配置:
mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.cs.pojo
mapper-locations:扫描指定路径下的*.xml的映射文件
type-aliases-package:完全限定名来指定这些POJO的引用
datasource: username: 数据库登录用户名 password: 数据库登录密码 url: jdbc:mysql://数据库ip地址:端口/数据库名称?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver
第三步:使用 Navicat在数据库上创建一个用于测试的数据库和数据表
数据库名称为:cs_orchard
表名和结构:login_user{
id int
user_name varchar
password varchar
description varchar
}
写入几条记录:
第四步:代码工程结构创建
- 四个包以com.cs.pojo、com.cs.mapper、com.cs.service、com.cs.controller
- 在src/main/resources下创建mapping文件夹用于存放mybatis的映射xml
第五步:编写代码
- 在pojo报下创建一个实体类如:LoginUser
@Component public class LoginUser { private int id; private String user_name; private String password; private String description; public LoginUser(){} public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2. 创建Mybatis的接口
@Mapper public interface LoginUserMapper { //获取用户名单 public List<LoginUser> getLoginUser() throws Exception; //根据id删除用户 public void deleteLoginUser(int id)throws Exception; //新增用户 public void addLoginUser(LoginUser user)throws Exception; }
3. 编写LoginUser的映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cs.mapper.LoginUserMapper"> <select id="getLoginUser" resultType="com.cs.pojo.LoginUser"> select * from login_user </select> <delete id="deleteLoginUser" parameterType="Integer"> delete from login_user where id =#{id} </delete> <insert id="addLoginUser" parameterType="com.cs.pojo.LoginUser"> insert into login_user(user_name, password, description) values (#{user_name}, #{password}, #{description}) </insert> </mapper>
其中注意每个操作与mapper接口类的对应,resultType和parameterType的类型。
4. 业务层的接口与实现类:LoginUserService.java LoginUserServiceImp.java
public interface LoginUserService { public List<LoginUser> getLoginUser() throws Exception; public void deleteUser(int id) throws Exception; public void addLoginUser(LoginUser user) throws Exception;
@Service public class LoginUserServiceImp implements LoginUserService { @Autowired private LoginUserMapper loginUserMapper; public List<LoginUser> getLoginUser(){ try { return loginUserMapper.getLoginUser(); } catch (Exception e) { e.printStackTrace(); } return null; } public void deleteUser(int id) { try { loginUserMapper.deleteLoginUser(id); } catch (Exception e) { e.printStackTrace(); } } public void addLoginUser(LoginUser user) { try { loginUserMapper.addLoginUser(user); } catch (Exception e) { e.printStackTrace(); } } }
5. 前端控制器类:LoginUserController
@RestController @RequestMapping("/loginuser") public class LoginUserController { @Autowired private LoginUserService loginUserService; @Autowired private LoginUser user; //显示用户 @RequestMapping("/list") public List<LoginUser> index() throws Exception { return loginUserService.getLoginUser(); } //删除用户 @RequestMapping("/delete/{id}") public String delete(@PathVariable int id) throws Exception { loginUserService.deleteUser(id); return "你已经删掉了id为"+id+"的用户"; } //增加用户 @RequestMapping("/addLoginUser") public String addLoginUser() throws Exception { user.setUser_name("阿花2"); user.setPassword("ddccs那还"); user.setDescription("这是我阿花的行配置"); loginUserService.addLoginUser(user); return "增加用户"; } }
调试运行,在浏览器中输入地址访问测试。
附上UML图:图用的是visio不太好表示