一直都说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. Notepad++替换SQL Server Select窗口列名的中括号的小技巧

    条件:“查找模式”那选中“扩展” 一.简单替换 查找目标(包括空格,各个SSMS版本可能不同): ]\r\n ,[ 替换为:, 二.替换为@ 查找目标(包括空格,各个SSMS版本可能不同): ]\r\ ...

  2. Linux运维第二课----Linux发展史、环境准备

    一.Linux的发展 1.1969年在贝尔实验室诞生Unix,是开源免费的,之后逐渐转变为收费系统. 2.1986年谭邦宁研发mini Unix,但主要用来教学. 3.斯托曼创建FSF(自由软件基金会 ...

  3. Python二级-----------程序冲刺1

    1. 仅使用 Python 基本语法,即不使用任何模块,编写 Python 程序计算下列数学表达式的结果并输出,小数点后保留3位.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪ ...

  4. Web后端 JAVAWeb面试考查知识点

    面试知识点:1:简单讲一下Java的跨平台原理答:由于非跨平台的情况下,对于不同的操作系统,那么就需要开发几套不同程序代码.为了解决这个问题,java通过不同系统,不同版本,不同位数的JVM来屏蔽不同 ...

  5. Django 提交 form 表单(使用sqlite3保存数据)

    优化 提交 form 表单,https://www.cnblogs.com/klvchen/p/10608143.html 创建数据库的字段,在 models.py 中添加 from django.d ...

  6. PostGIS计算矢量切片(二)--按值渲染

    方案背景     今年三月份写了一篇postgis计算矢量切片,参考了网上资料给出了一份很粗糙的相关方案(文章写的更粗糙).当时的方案中只能针对gis形状进行渲染,而不能用属性渲染.针对这个情况,本文 ...

  7. Android开发支付集成——支付宝集成

    微信支付传送门:https://www.cnblogs.com/dingxiansen/p/9209159.html 一.支付宝支付 1. 支付宝支付流程图 2. 集成前准备 去蚂蚁金服注册应用获取a ...

  8. deepin linux学习笔记

    目录 deepin linux学习笔记 前言 linux常用命令 ls 显示文件夹内容 cd 切换当前目录 pwd 查看当前工作目录 mkdir 新建文件夹 rm 删除文件或文件夹 mv 移动文件 c ...

  9. 【Objective-C学习笔记】变量和基本的数据类型

    OC是增强了C的特性,所以在变量和基本数据类型上基本与C一致. 在OC中变量命名有如下规则: 由字母.数字.下划线.$符号组成 必须以字母.下划线.$符号开头 大小写敏感 在OC中定义变量的时候不能使 ...

  10. 数据库微信特殊表情编码django设置

    #settings.py DATABASES = { 'default': { 'OPTIONS': { "init_command":"SET foreign_key_ ...