一直都说SpringBoot是零配置,当然,真正实现零配置是不可能的,但是在配置mybatis这里真的是太简单了,哈哈,下面我们一起看一下。

1.先导入基于SpringBoot的mybatis依赖包

        <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
       <version>1.3.0</version>
</dependency>

2.引入mybatis的基本配置文件mybatis.cfg.xml,这里就不过多做其他的配置了,一个最简单的配置文件如下

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<!-- 进行Mybatis的相应的环境的属性定义 -->
<settings> <!-- 在本项目中开启二级缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
</configuration>

3.在application.yml下配置mybatis相关。此配置主要是针对mybatis配置文件和mapper配置文件以及bean的路径。

mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径
type-aliases-package: cn.mldn.microboot.vo #定义所有操作类的别名所在包
mapper-locations: #所有的mapper映射文件
- classpath:mybatis/mapper/**/*.xml

4.我们做一个Student对象的映射,实现基本的增删改查功能,Student表和bean已经提前建好。

<?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="cn.mldn.microboot.dao.IStudentDAO">
<select id="findAll" resultType="Student">
SELECT * FROM student;
</select>
<insert id="doCreate" parameterType="Student">
INSERT INTO student(name,age,sex,birthday) VALUES(#{name},#{age},#{sex},#{birthday})
</insert>
<update id="doUpdate" parameterType="Student">
UPDATE student set name = #{name} where id=#{id}
</update>
<delete id="doDelete" parameterType="int">
Delete from student Where id=#{id}
</delete>
</mapper>

这里的namespace属性值对应我们Dao层,id则为方法名。先看一下Dao层的代码。

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import cn.mldn.microboot.vo.Student;
@Mapper
public interface IStudentDAO {
public List<Student> findAll();
public boolean doCreate(Student vo);
public boolean doUpdate(Student vo);
public boolean doDelete(Integer id);
}

看到了,就是这么简单,一个Mapper注解,解决了所有问题。

5.接下来我们写一下Servic层和调用的测试类。

Service层接口定义如下:

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import cn.mldn.microboot.vo.Student; public interface IStudentService {
@Transactional(readOnly = true)
public List<Student> list();
@Transactional(propagation=Propagation.REQUIRED)
public boolean add(Student vo);
@Transactional(propagation=Propagation.REQUIRED)
public boolean update(Student vo);
@Transactional(propagation=Propagation.REQUIRED)
public boolean delete(Integer id);
}
Transactional注解是对事物的控制。
下面是Service的实现
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.mldn.microboot.dao.IStudentDAO;
import cn.mldn.microboot.service.IStudentService;
import cn.mldn.microboot.vo.Student;
@Service
public class StudentServiceImpl implements IStudentService {
@Resource
private IStudentDAO studentDao;
@Override
public List<Student> list() {
return this.studentDao.findAll();
}
@Override
public boolean add(Student vo) {
return this.studentDao.doCreate(vo);
}
@Override
public boolean update(Student vo) {
return this.studentDao.doUpdate(vo);
}
@Override
public boolean delete(Integer id) {
return this.studentDao.doDelete(id);
}
}

最后我们写下测试类

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import cn.mldn.microboot.StartSpringBootMain;
import cn.mldn.microboot.service.IStudentService;
import cn.mldn.microboot.vo.Student; @SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestStudentService {
@Resource
private IStudentService studentService;
@Test
public void testList() throws Exception{
System.out.println(this.studentService.list());
}
@Test
public void testAdd() throws Exception{
Student student = new Student();
student.setAge(26);
student.setName("来来来");
student.setBirthday("2018/03/01");
student.setSex(true);
System.out.println(this.studentService.add(student));
}
}

这里只写了查询和添加的测试类,测试

测试后,成功添加与查询。

SpringBoot配置mybatis的更多相关文章

  1. springboot 配置mybatis 配置mapper.xml

    # 插件 进行配置 也可以用yml # 1. 配置 Tomcat 修改端口号 server.port=8848 server.context-path=/zxf #2.配置数据源 spring.dat ...

  2. 【记录】spring/springboot 配置mybatis打印sql

    ======================springboot mybatis 打印sql========================================== 方式 一: ##### ...

  3. springboot配置mybatis的mapper路径

    1.在src/main/resources/目录下新建mybatis文件夹,将xxx.xml文件放入该文件夹内 2.在application.yml文件中配置: mybatis: configurat ...

  4. springboot 配置mybatis打印sql

    方式 一: ###########################################################配置打印sql############################ ...

  5. springboot 配置mybatis

  6. IDEA springboot配置

    基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...

  7. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  8. SpringBoot集成mybatis配置

    一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis:之所以出现这个问题感觉与对应的业务有关,比方说,互联网的业务更加的复杂,更加需要进行灵活性的处理,所以myba ...

  9. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

随机推荐

  1. Windows Server 2012 R2安装SqlServer 2016

    1.系统安装 微软操作系统 Windows Server 2012 R2 官方原版镜像 Windows Server 2012 R2 是由微软公司(Microsoft)设计开发的新一代的服务器专属操作 ...

  2. .NET redis cluster

    一.下载Windows版本Redis 下载链接:https://github.com/MSOpenTech/redis/releases(根据系统选择对应版本) 二.修改默认的配置文件 如上图两个配置 ...

  3. 回顾曾经的自己,献给java初学者的建议

    要不惜代价投资自己,任何对自己的投资都是值得的 要多学习数据结构, 习惯看源码! 一份知识经过n个人的传递早已经不成样子了 遇到问题不要直接百度,百度上那些花里胡哨的东西有用的很少,对症下药才是王道, ...

  4. java爬虫系列第四讲-采集"极客时间"专栏文章、视频专辑

    1.概述 极客时间(https://time.geekbang.org/),想必大家都知道的,上面有很多值得大家学习的课程,如下图: 本文主要内容 使用webmagic采集极客时间中某个专栏课程生成h ...

  5. Ext JS中的typeOf

    Ext JS中的typeOf:以字符串格式,返回给定变量的类型 其中对字符串对象.元素节点.文本节点.空白文本节点判断并不准确 测试代码如下: <!DOCTYPE HTML PUBLIC &qu ...

  6. JavaScript基础-3

    3 运算符 按照个数分类可分为:一元运算符.二元运算符.三元运算符: 按照功能分类可分为:算数运算符.自增运算符.比较运算符.逻辑运算符.赋值运算符: 3.1 算数运算符 算术运算符包含了加减乘除,符 ...

  7. Ajax的面试题

    一.什么事Ajax?为什么要用Ajax?(谈谈对Ajax的认识) 什么是Ajax: Ajax是“Asynchronous JavaScript and XML”的缩写.他是指一种创建交互式网页应用的网 ...

  8. 【Oracle RAC】Linux系统Oracle12c RAC安装配置详细记录过程V2.0(图文并茂)

    [Oracle RAC]Linux系统Oracle12c RAC安装配置详细过程V2.0(图文并茂) 2 Oracle12c RAC数据库安装准备工作2.1 安装环境介绍2.2 数据库安装软件下载3 ...

  9. go的生产者-消费者模式

    package main import ( "fmt" "math/rand" "time" ) // 数据生产者 func produce ...

  10. Oracle 安装步骤、安装中错误处理、完整卸载

    /*************************************************以下ORACLE服务端安装************************************* ...