之前搭传统的ssm框架,配置文件很多,看了几天文档才把那些xml的逻辑关系搞得七七八八,搭起来也是很麻烦,那时我完全按网上那个demo的版本要求(jdk和tomcat),所以最后是各种问题没成功跑起来。    今天尝试用springboot来整合,不敢相信才失败几次就成功了!!

现在来记录一下过程:

  首先springboot基本的建立就不讲了,之前的博客里面有写。

添加POM依赖

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>

关于dao层的依赖就是后面两个mysql的connector和mybatis的starter(这个可是好东西)

开始写代码

这里我们有个简单是数据表:

我们的目的是  用get发一个带有 id值的请求,服务器根据id值返回这个图书管理员的全部信息并用json的方式直接显示在页面上,

Controller:

@RestController
public class LibrarianController { @Autowired
private LibrarianService librarianService; @GetMapping("/getLibrarian")
public Librarian getALibrarianInfo(int id) {
//System.out.println("test :id: "+id);
return librarianService.selectLibrarian(id);
}
}

RestController是responsebody+Controller两个注解的合体,一般就拿来直接传json数据。  为什么可以直接传个对象过去呢?这是因为springboot内置了jackson模块,可以在maven的依赖下看到这方面的jar包(之前我写是按网上的教程用gson来处理的,比起来这个简直无敌)

然后看到我们注入的sevice,下面是service

Service

public interface LibrarianService {
Librarian selectLibrarian(int id);
}

就是个接口

ServiceImpl

@Service
public class LibrarianServiceImpl implements LibrarianService{ @Autowired
private LibrarianMapper librarianMapper; @Override
public Librarian selectLibrarian(int id) {
// TODO Auto-generated method stub
return librarianMapper.selectLibrarian(id);
}
}

这里记得要加@Service备注,才会被spring生成bean然后注入到controller那里去。

然后看到这里注入了个mapper

Dao:

package com.example.dao;

import org.apache.ibatis.annotations.Mapper;

import com.example.entity.Librarian;

@Mapper
public interface LibrarianMapper {
Librarian selectLibrarian(int id);
}

这里加的@Mapper是 MyBatis的备注,目的是为了让spring能够根据xml和这个接口动态生成这个接口的实现。如果是加@Repository,就是spring生成一个bean,自动注入service的相关引用中。

然后是Mapper的xml文件(我的MyBatis的相关sql用的是xml)

mapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.LibrarianMapper"> <!-- 可根据自己的需求,是否要使用 -->
<resultMap type="Librarian" id="LibrarianMap"> <id column="id" property="id" jdbcType="INTEGER" />
<result column="userName" property="useName" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="position" property="position" jdbcType="VARCHAR" /> </resultMap> <select id="selectLibrarian" parameterType="INTEGER" resultMap="LibrarianMap">
select *
from t_librarian
where 1=1
and id = #{id,jdbcType=INTEGER}
</select> </mapper>

第三行的namespace很重要噢,它是指定这个xml所对应的是哪个dao(mapper)接口

resultMap是做个pojo和数据库表的对应(这里只写个Librarian不用写全路径是因为做了设置,下面会说)

select标签的id对应的就是mapper接口中的方法名,parameterType就是传进来的参数类型

简单的配置与设置

好现在讲讲设置,这里会想到,那些Controller啊,@Service啊还有MyBatis的注解@Mapper什么的spring怎么知道在哪呢?不用像mvc那样搞个扫描设置吗?

是的要的,这些我们在启动类做了简单的设置:

package com.example.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 指定所扫的包,会自动扫描指定包下的全部标有@Component的类,并注册成bean,
* 当然包括@Component下的子注解@Service,@Repository,@Controller。
* @author 85060
*
*/
@SpringBootApplication(scanBasePackages={"com.example.*"})
@MapperScan("com.example.dao")
public class SpringBootDemoApplication { public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}

关于这个SpringBootApplication,它其实是好几个注解的合体(之前的博客里有讲),所以可以直接在这写扫包的设置,然后那个@MapperScan是MyBatis提供的设置注解。

然后还有点配置文件:

spring.thymeleaf.cache=false
spring.devtools.restart.enabled=true
spring.devtools.restart.additional-paths=src/main/java spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_library
spring.datasource.username=root
spring.datasource.password=4008 mybatis.type-aliases-package=com.example.entity
mybatis.mapperLocations=classpath:mappers/*.xml

主要有关的是第四行开始的,最后两行一个是设置基本包(包别名)也就是为什么在mapper.xml中可以只写一个Librarian的原因。

最后一行的很重要,是告诉系统在哪里去找mapper.xml文件。

上一个最后的效果图:

再放一个项目结构图:

放两个springboot和mybatis整合讲得比较好的博客供参考:

https://blog.csdn.net/gebitan505/article/details/54929287(要是着看到这个就省好多功夫了!)

https://blog.csdn.net/blackwoodcliff/article/details/50776155

Springboot与MyBatis简单整合的更多相关文章

  1. IntelliJ IDEA 2017版 spring-boot与Mybatis简单整合

    一.编译器建立项目 参考:http://www.cnblogs.com/liuyangfirst/p/8372291.html 二.代码编辑 1.建立数据库 /* Navicat MySQL Data ...

  2. SpringBoot+SpringMVC+MyBatis快速整合搭建

    作为开发人员,大家都知道,SpringBoot是基于Spring4.0设计的,不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程.另外Spr ...

  3. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  4. springboot+springmvc+mybatis项目整合

    介绍: 上篇给大家介绍了ssm多模块项目的搭建,在搭建过程中spring整合springmvc和mybatis时会有很多的东西需要我们进行配置,这样不仅浪费了时间,也比较容易出错,由于这样问题的产生, ...

  5. SpringBoot和Mybatis的整合

    这里介绍两种整合SpringBoot和Mybatis的模式,分别是“全注解版” 和 “注解xml合并版”. 前期准备开发环境 开发工具:IDEAJDK:1.8技术:SpringBoot.Maven.M ...

  6. springboot对mybatis的整合

    1. 导入依赖 首先新建一个springboot项目,勾选组件时勾选Spring Web.JDBC API.MySQL Driver pom.xml配置文件导入依赖 <!--mybatis-sp ...

  7. SpringBoot系列——MyBatis整合

    前言 MyBatis官网:http://www.mybatis.org/mybatis-3/zh/index.html 本文记录springboot与mybatis的整合实例:1.以注解方式:2.手写 ...

  8. 30分钟带你了解Springboot与Mybatis整合最佳实践

    前言:Springboot怎么使用想必也无需我多言,Mybitas作为实用性极强的ORM框架也深受广大开发人员喜爱,有关如何整合它们的文章在网络上随处可见.但是今天我会从实战的角度出发,谈谈我对二者结 ...

  9. SpringBoot与SpringDateJPA和Mybatis的整合

    一.SpringBoot与SpringDateJPA的整合 1-1.需求 查询数据库---->得到数据------>展示到页面上 1-2.整合步骤 1-2-1.创建SpringBoot工程 ...

随机推荐

  1. 认识HttpContext.User

    HttpContext.User,即IPrincipal .net源代码 namespace System.Security.Principal { /// <summary>Define ...

  2. Action Results in MVC

  3. javascript总结3:javaScript的 Math 对象

    Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). Math 常用的方法 var n1=1234; v ...

  4. JavaScript必备:Google发布的JS代码规范(转)

    [翻译]关于Google发布的JS代码规范,你需要了解什么? 翻译 | WhiteYin 译文 | https://github.com/WhiteYin/translation/issues/10 ...

  5. UVa 1625 Color Length (DP)

    题意:给定两个序列,让你组成一个新的序列,让两个相同字符的位置最大差之和最小.组成方式只能从一个序列前部拿出一个字符放到新序列中. 析:这个题状态表示和转移很容易想到,主要是在处理上面,dp[i][j ...

  6. mybatis使用count返回int的方法

    <select id="countByExample" resultType="java.lang.Integer" > select count( ...

  7. DataSet取值并保存在List集合中

    DBHelper db = new DBHelper(); //实例化DB类 string sql = "select * from student"; //虚构的sql语句 Da ...

  8. 「BZOJ 3218」 a + b Problem

    题目链接 戳我 \(Solution\) 题目为什么是\(a\ +\ b\ Problem\)啊?这和题面半毛钱关系都没有. 现在来讲一下这题的解法吧,我们首先看看没有奇怪的方格这一个条件吧. 其实没 ...

  9. Python的特殊属性和魔法函数

    python中有很多以下划线开头和结尾的特殊属性和魔法函数,它们有着很重要的作用. 1.__doc__:说明性文档和信息,python自建,不需要我们定义. # -*- coding:utf- -*- ...

  10. 【Es】jest操作elasticsearch

    https://blog.csdn.net/niuchenliang524/article/details/82869319 操作es的客房端有多个,在此例出三种(具体区别自行百度),本文讲的是jes ...