SpringBoot配置mybatis
一直都说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的更多相关文章
- springboot 配置mybatis 配置mapper.xml
# 插件 进行配置 也可以用yml # 1. 配置 Tomcat 修改端口号 server.port=8848 server.context-path=/zxf #2.配置数据源 spring.dat ...
- 【记录】spring/springboot 配置mybatis打印sql
======================springboot mybatis 打印sql========================================== 方式 一: ##### ...
- springboot配置mybatis的mapper路径
1.在src/main/resources/目录下新建mybatis文件夹,将xxx.xml文件放入该文件夹内 2.在application.yml文件中配置: mybatis: configurat ...
- springboot 配置mybatis打印sql
方式 一: ###########################################################配置打印sql############################ ...
- springboot 配置mybatis
- IDEA springboot配置
基于springboot2.1.7 springboot项目创建 springboot热部署 springboot配置swagger2 springboot配置mybatis springboot配置 ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
- SpringBoot集成mybatis配置
一个有趣的现象:传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis:之所以出现这个问题感觉与对应的业务有关,比方说,互联网的业务更加的复杂,更加需要进行灵活性的处理,所以myba ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
随机推荐
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- shared_ptr和动态数组
std::shared_ptr智能指针是c++11一个相当重要的特性,可以极大地将开发者从资源申请/释放的繁重劳动中解放出来. 然而直到c++17前std::shared_ptr都有一个严重的限制,那 ...
- 头部banner根据网址高亮
$(function(){ var urlstr = location.href; $(".nav li a").each(function () { if ((urlstr + ...
- 在react中实现打印功能
最近项目中,前端采用react+antd+dva的组合,已经完成了后头管理类系统产品的更新迭代工作. 今天有一个新需求,需要在后台管理系统中实现点击打印完成指定页面的打印功能. 之前也没接触过,只知道 ...
- 【English】十三、英语中的连词有哪些,都有什么作用
一.什么是连词 参考:https://m.hujiang.com/en_cixing/yylc/ 连词是一种虚词,用于连接单词.短语.从句或句子,在句子中不单独用作句子成分. 连词按其性质可分为并列连 ...
- 【English】十二、英语句子种类,陈述句、疑问句、祈使句、感叹句
一.英语句子按照用途可以分为4类 种类.用途.例句 陈述句 用于说明事实或说话人的看法(事实不就是别人发起并被同意的看法) My name is Jennt Green. I like him bec ...
- DVWA 黑客攻防演练(十)反射型 XSS 攻击 Reflected Cross Site Scripting
XSS (Cross-site scripting) 攻击,为和 CSS 有所区分,所以叫 XSS.又是一种防不胜防的攻击,应该算是一种 "HTML注入攻击",原本开发者想的是显示 ...
- SpringMVC归纳-1(model数据模型与重定向传参技术)
要点: model是一个Map结构的数据模型,能重定向时传递数据(拼接URL),但不安全,主要用于渲染前端页面,配合Thymeleaf填充html里面里设置好的参数. @RequestParam用来获 ...
- 测者的测试技术手册:Java中的null类型是测试不可超越的鸿沟
null是一个非常非常特殊的类型,对于每一个测试人员都要十分小心null的存在的可能性.同时null也让很多RD头疼,甚至连Java的设计者都成人null是一个设计失误.这篇文章,测者想聊聊这个让很多 ...
- 记一次使用SimpleDateFormat 格式化时间时遇到的问题
网上的使用方法一大堆,我就不再介绍了,就写一下自己遇到的问题. 先来实现一下获取当前时间: SimpleDateFormat simpleDateFormat =new SimpleDateForma ...
