当前位置:   article > 正文

springBoot整合myBatis完整前后端项目实例_springboot+sqlserver+mybatis

springboot+sqlserver+mybatis

前期已经介绍:

eclipse创建springBoot项目详细教程

springBoot整合thymeleaf

本文主要介绍springBoot三层架构(controller+service/serviceImpl+Dao)+myBatis+thymeleaf开发模式完整的项目整合。

以获取用户信息为例,先看整体结构图:

1. 数据表信息:

2. pom文件引入数据库驱动包(示例数据库为SQLServer) 

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.microsoft.sqlserver</groupId>
  8. <artifactId>mssql-jdbc</artifactId>
  9. </dependency>

3. properties或yml文件添加对应配置

  1. ##设置端口
  2. server.port=6789
  3. ##设置数据源信息
  4. spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  5. spring.datasource.url=jdbc:sqlserver://192.168.0.8:1433;DatabaseName=myDB;useUnicode=true;characterEncoding=utf8;characterSetResults=utf8;allowMultiQueries=true
  6. spring.datasource.username=sa
  7. spring.datasource.password=DB!pwd
  8. #设置thymeleaf
  9. spring.thymeleaf.prefix=classpath:/templates/
  10. spring.thymeleaf.encoding=UTF-8
  11. spring.thymeleaf.cache=false
  12. spring.thymeleaf.suffix=.html
  13. spring.thymeleaf.servlet.content-type=text/html

4. User实体类:

  1. package com.example.demo.entity;
  2. public class User {
  3. private int id;
  4. private String userName;
  5. private String userSex;
  6. private String userBirth;
  7. public int getId() {
  8. return id;
  9. }
  10. public void setId(int id) {
  11. this.id = id;
  12. }
  13. public String getUserName() {
  14. return userName;
  15. }
  16. public void setUserName(String userName) {
  17. this.userName = userName;
  18. }
  19. public String getUserSex() {
  20. return userSex;
  21. }
  22. public void setUserSex(String userSex) {
  23. this.userSex = userSex;
  24. }
  25. public String getUserBirth() {
  26. return userBirth;
  27. }
  28. public void setUserBirth(String userBirth) {
  29. this.userBirth = userBirth;
  30. }
  31. }

5.  数据访问层DAO

  1. package com.example.demo.dao;
  2. import java.util.List;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import com.example.demo.entity.User;
  5. @Mapper
  6. public interface UserMapper {
  7. List<User> getInfoList();
  8. }

PS:此处需要添加@Mapper注解,如果觉得每一个文件都添加比较麻烦,可以在启动类添加@MapperScan扫描注入,并且可以添加扫描多个包:

  1. package com.example.demo;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. //可以添加多个,还可以用通配符进行配置
  7. @MapperScan(basePackages="com.example.demo.dao")
  8. public class DemoApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. }

6. service接口:

  1. package com.example.demo.service;
  2. import java.util.List;
  3. import com.example.demo.entity.User;
  4. public interface UserService {
  5. List<User> getInfoList();
  6. }

7. service接口实现类:

  1. package com.example.demo.service.impl;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import com.example.demo.dao.UserMapper;
  6. import com.example.demo.entity.User;
  7. import com.example.demo.service.UserService;
  8. //此处的命名对于类似项目结构一个接口对应多个实现类的情况必不可少
  9. @Service("userService")
  10. public class UserServiceImpl implements UserService{
  11. @Autowired
  12. private UserMapper demoDao;
  13. @Override
  14. public List<User> getInfoList() {
  15. return demoDao.getInfoList();
  16. }
  17. }

8. SQL Mapper文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="com.example.demo.dao.UserMapper">
  5. <select id="getInfoList" parameterType="String" resultType="com.example.demo.entity.User">
  6. select
  7. userName,
  8. userSex,
  9. userBirth
  10. from user_info
  11. </select>
  12. </mapper>

PS:此处需要注意,mapper文件要和数据访问层DAO文件在同一目录下,不然会出现“Invalid bound statement”即无效绑定语句的异常。如果想结构清晰并对mybatis文件进行统一管理,可以在demo\src\main\resources目录下创建mybatis目录,把所有mybatis文件放在此目录下,并在properties或yml文件添加mybatis配置:mybatis.mapper-locations=classpath:/myBatis/*.xml

9. 控制器Controller

  1. package com.example.demo.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import com.example.demo.entity.User;
  8. import com.example.demo.service.UserService;
  9. @Controller
  10. public class UserController {
  11. @Autowired
  12. UserService userService;
  13. @RequestMapping("/getUser")
  14. public String getUser(Model model){
  15. List<User> userList=userService.getInfoList();
  16. model.addAttribute("userList",userList);
  17. return "user/userHtm";
  18. }
  19. }

PS:如果一个接口对应多个实现类(即:UserService 对应多个实现类),可以通过@Resource(name="userService")或【@Autowired+@Qualifier("userService")】注入需要的实现类,前提是实现类中已命名。

10. thymeleaf html数据展示:

  1. <!DOCTYPE html>
  2. <html xmlns : th = "http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Demo HTML</title>
  6. <style type="text/css">
  7. .user-table{
  8. border:1px solid grey;
  9. border-spacing:0;
  10. }
  11. .user-table th,.user-table td{
  12. border:1px solid grey;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <table class="user-table">
  18. <tr>
  19. <th>序号</th>
  20. <th>姓名</th>
  21. <th>性别</th>
  22. <th>出生年份</th>
  23. </tr>
  24. <tr th:each="user,userStat:${userList}">
  25. <td th:text="${userStat.index}+1"></th>
  26. <td th:text="${user.userName}"></td>
  27. <td th:text="${user.userSex}"></td>
  28. <td th:text="${user.userBirth}"></td>
  29. </tr>
  30. </table>
  31. </body>
  32. </html>

11. 请求测试:

 以上,完整的项目案例已结束。

PS:整个项目设计下来,肯定有人会觉得太繁琐,每一个service接口还需要再设计一个serviceImpl实现类,又要对应一个访问层DAO,代码虽不复杂,但就是感觉繁琐,目前其实很多同行们都是直接自动生成这些文件的。本可以只用service,至于为什么要采用service+serviceImpl这样的结构模式,权威些的解释是:spring鼓励应用程序的各个层以接口的形式暴露功能,在service层,可以使用service接口+serviceImpl实现类,也可以只使用service类,但考虑到“接口是实现松耦合的关键”,所以更加推荐使用service接口+serviceImpl实现类。

虽然繁琐些,但对于初学者能够清晰了解后端设计思想和结构,还是很有帮助的~

后续再介绍一种逻辑简单、结构清晰并且很易于上手的项目案例。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号