spring boot 和mybatis结合实现数据库操作

一、添加数据库操作相关的包

  1. <!--mysql连接驱动包-->
  2. <dependency>
  3. <groupId>mysql</groupId>
  4. <artifactId>mysql-connector-java</artifactId>
  5. </dependency>
  6. <!--添加spring boot对jdbc的支持-->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-jdbc</artifactId>
  10. </dependency>
  11. <!--添加mybatis对spring boot的支持-->
  12. <dependency>
  13. <groupId>org.mybatis.spring.boot</groupId>
  14. <artifactId>mybatis-spring-boot-starter</artifactId>
  15. <version>1.3.0</version>
  16. </dependency>
  17. <!--mapper对spring boot的支持-->
  18. <dependency>
  19. <groupId>tk.mybatis</groupId>
  20. <artifactId>mapper-spring-boot-starter</artifactId>
  21. <version>1.1.0</version>
  22. </dependency>

如果需要修改数据库连接池,需要添加相应的数据库连接池包,如使用druid,则需要添加druid包

二、添加数据库连接以及mybatis相关配置

在项目配置application.properties中添加:

  1. #mapper的xml文件存放位置
  2. mybatis.mapper-locations=classpath*:/mapper/*Mapper.xml
  3. #mybatis的实体类包命名空间
  4. mybatis.type-aliases-package=com.shixinke.springboot.entity
  5. #数据库连接信息
  6. #驱动类
  7. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  8. #jdbc数据源地址
  9. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/letsgodb
  10. #数据库用户名
  11. spring.datasource.username=admin
  12. #数据库密码
  13. spring.datasource.password=admin

三、定义数据库实体类

  1. package com.shixinke.springboot.entity;
  2. import lombok.Data;
  3. import org.springframework.format.annotation.DateTimeFormat;
  4. import org.springframework.stereotype.Component;
  5. import java.util.Date;
  6. @Component
  7. @Data
  8. public class User {
  9. private int userId;
  10. private String account;
  11. private String password;
  12. private String salt;
  13. private String nickname;
  14. private String email;
  15. private String mobile;
  16. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  17. private Date lastLoginTime;
  18. private String lastLoginIp;
  19. private byte status;
  20. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  21. private Date createTime;
  22. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  23. private Date updateTime;
  24. }

四、定义数据库操作mapper接口

  1. package com.shixinke.springboot.mapper;
  2. import com.shixinke.springboot.entity.User;
  3. import com.shixinke.springboot.util.BaseMapper;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import org.springframework.stereotype.Component;
  6. import java.util.List;
  7. public interface UserMapper extends BaseMapper<User> {
  8. List<User> userList();
  9. }

五、定义逻辑操作服务service类

1、定义service服务接口
  1. package com.shixinke.springboot.service;
  2. import com.shixinke.springboot.entity.User;
  3. import java.util.List;
  4. public interface UserService {
  5. List<User> userList();
  6. }
2、定义service服务实现类
  1. package com.shixinke.springboot.service.impl;
  2. import com.shixinke.springboot.mapper.UserMapper;
  3. import com.shixinke.springboot.entity.User;
  4. import com.shixinke.springboot.service.UserService;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. @MapperScan("com.shixinke.springboot.mapper")
  11. public class UserServiceImpl implements UserService{
  12. @Autowired
  13. private UserMapper userMapper;
  14. public List<User> userList() {
  15. return userMapper.userList();
  16. }
  17. }

六、控制器操作

  1. package com.shixinke.springboot.controller;
  2. import com.shixinke.springboot.service.UserService;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. import java.util.List;
  9. @Controller
  10. @RequestMapping("/user")
  11. public class UserController {
  12. @Autowired
  13. private UserService userService;
  14. @GetMapping("/index")
  15. public ModelAndView index() {
  16. ModelAndView modelAndView = new ModelAndView("user/index");
  17. List userList = userService.userList();
  18. modelAndView.addObject("title", "用户列表");
  19. modelAndView.addObject("userList", userList);
  20. return modelAndView;
  21. }
  22. }

七、模板视图

注:这里使用提freemarker这种模板引擎,相关使用请参阅spring boot结合freemarker相关文章

  1. <table class="table table-bordered table-striped ">
  2. <thead>
  3. <th>用户ID</th>
  4. <th>账号</th>
  5. <th>昵称</th>
  6. <th>邮箱</th>
  7. <th>手机号</th>
  8. <th>注册时间</th>
  9. <th>状态</th>
  10. <th>操作</th>
  11. </thead>
  12. <tbody>
  13. <#list userList as user>
  14. <tr>
  15. <td>${user.getUserId()}</td>
  16. <td>${user.getAccount()}</td>
  17. <td>${user.getNickname()}</td>
  18. <td>${user.getEmail()}</td>
  19. <td>${user.getMobile()}</td>
  20. <td>${user.getCreateTime() ? string("yyyy-MM-dd HH:mm:ss")}</td>
  21. <td>
  22. <#if user.getStatus() == 1>
  23. <label class="label label-success">正常</label>
  24. <#else>
  25. <label class="label label-danger">禁用</label>
  26. </#if>
  27. </td>
  28. <td>
  29. <a href="/user/edit?id=${user.getUserId()}"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span> 编辑</a>
  30. <a href="javascript:;" class="ajax-delete-link" data-id="${user.getUserId()}" data-action="/user/delete">
  31. <span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 删除
  32. </a>
  33. </td>
  34. </tr>
  35. <#/list>
  36. </tbody>
  37. </table>