SpringBBoot整合MyBatis
一、目录展示
二、导入依赖
三、配置文件application.yml
四、Student实体类
- package com.zn.entity;
- public class Student {
- private Integer stu_id;
- private String stu_name;
- @Override
- public String toString() {
- return "Student{" +
- "stu_id=" + stu_id +
- ", stu_name='" + stu_name + '\'' +
- '}';
- }
- public Student() {
- }
- public Student(String stu_name) {
- this.stu_name = stu_name;
- }
- public Student(Integer stu_id, String stu_name) {
- this.stu_id = stu_id;
- this.stu_name = stu_name;
- }
- public Integer getStu_id() {
- return stu_id;
- }
- public void setStu_id(Integer stu_id) {
- this.stu_id = stu_id;
- }
- public String getStu_name() {
- return stu_name;
- }
- public void setStu_name(String stu_name) {
- this.stu_name = stu_name;
- }
- }
五、StudentDao层
- package com.zn.dao;
- import com.zn.entity.Student;
- import org.springframework.stereotype.Repository;
- import java.util.List;
- @Repository
- public interface StudentDao {
- //添加数据
- int insertStudent(Student student);
- //修改数据
- int updateStudent(Student student);
- //删除数据
- int deleteStudent(Integer id);
- //查询数据
- List<Student> findAll();
- }
六、resources下的mapper中的StudentDao.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.zn.dao.StudentDao" >
- <!--添加数据-->
- <insert id="insertStudent">
- insert into studentinfo(stu_name) values(#{stu_name})
- </insert>
- <!--修改数据-->
- <update id="updateStudent">
- update studentinfo set stu_name=#{stu_name} where stu_id=#{stu_id}
- </update>
- <!--删除数据-->
- <delete id="deleteStudent">
- delete from studentinfo where stu_id=#{stu_id}
- </delete>
- <!--查询数据-->
- <select id="findAll" resultType="com.zn.entity.Student">
- select * from studentinfo
- </select>
- </mapper>
七、StudentService层
- package com.zn.service;
- import com.zn.dao.StudentDao;
- import com.zn.entity.Student;
- import org.springframework.stereotype.Service;
- import javax.annotation.Resource;
- import java.util.List;
- @Service
- public class StudentService {
- @Resource
- StudentDao studentDao;
- //增加数据
- public int insertStudent(Student student) {
- return studentDao.insertStudent(student);
- }
- //修改数据
- public int updateStudent(Student student) {
- return studentDao.updateStudent(student);
- }
- //删除数据
- public int deleteStudent(Integer id) {
- return studentDao.deleteStudent(id);
- }
- //查询数据
- public List<Student> findAll(){
- return studentDao.findAll();
- }
- }
八、StudentController层
- package com.zn.controller;
- import com.zn.entity.Student;
- import com.zn.service.StudentService;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- import java.util.List;
- @RestController
- public class StudentController {
- @Resource
- StudentService studentService;
- //添加数据
- @RequestMapping("/insertStudent")
- public int insertStudent(){
- return studentService.insertStudent(new Student("刘姐"));
- }
- //修改数据
- @RequestMapping("/updateStudent")
- public int updateStudent(){
- return studentService.updateStudent(new Student(5,"小傻子"));
- }
- //删除数据
- @RequestMapping("/deleteStudent")
- public int deleteStudent(){
- return studentService.deleteStudent(4);
- }
- //查询数据
- @RequestMapping("/findAll")
- public List<Student> findAll(){
- return studentService.findAll();
- }
- }
九、测试类
十、效果展示
省略增删改效果
(1)查询数据
SpringBBoot整合MyBatis的更多相关文章
- springboot使用之二:整合mybatis(xml方式)并添加PageHelper插件
整合mybatis实在前面项目的基础上进行的,前面项目具体整合请参照springboot使用之一. 一.整合mybatis 整合mybatis的时候可以从mybatis官网下载mybatis官网整合的 ...
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- SpringMVC入门二: 1规范结构, 2简单整合MyBatis
昨天拿springMVC写的helloworld结构不好, 这次先调整一下体系结构 , 然后简单整合一下MyBatis spring的配置还是以注解为主, 不过MyBatis的映射文件什么的还是拿xm ...
- 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- 2017年2月16日 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- Spring Boot 整合 MyBatis
前言 现在业界比较流行的数据操作层框架 MyBatis,下面就讲解下 Springboot 如何整合 MyBatis,这里使用的是xml配置SQL而不是用注解.主要是 SQL 和业务代码应该隔离,方便 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...
随机推荐
- 你真的了解foreach吗?
引言 有C#基础的,当问到循环有哪些,会毫不犹豫的说出的for.do while.foreach及while这几种,但是到具体实际开发中,我们遇到一些问题,比如:到底选择哪种?为什么选择这种?哪种好像 ...
- Spring Boot中使用Jpa的findOne方法不能传入id
最近通过慕课网学习spring boot,视频中通过jpa的findOne方法以id为参数查询出对应的信息, 而当我自己做测试的时候却发现我的findOne方法的参数没有Integer类型的id,而是 ...
- JS&jQuery
1.JavaScript概述 1.什么是JavaScript JavaScript简称JS,是一种专门运行于JS解释器/引擎中的解释型脚本语言 2.JS发展史 ...
- Spring Data JPA 条件查询的关键字
Spring Data JPA 为此提供了一些表达条件查询的关键字,大致如下: And --- 等价于 SQL 中的 and 关键字,比如 findByUsernameAndPassword(Stri ...
- 小白的springboot之路(七)、事务支持
0-前言 事务管理对于企业级应用来说必不可少,用来确保数据的完整性和一致性: 1-开启事务 spring boot支持编程式事务和声明式事务,用声明式事务即可: spring boot开启事务非常简单 ...
- gulp+webpack+angular1的一点小经验(第三部分使用一些angular1的插件ui-bootstrap与highcharts)
第一个要介绍的是我们的麻烦制造器:angular-ui-bootstrap ui-bootstrap可以有很多通用的插件给大家用,比如弹窗啊(modal),翻页控件啊(pagination),为什么说 ...
- 华为云·寻找黑马程序员#海量数据的分页怎么破?【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- Java修炼——面向对象_抽象类和抽象方法
抽象类和抽象方法 什么是抽象类? 使用 abstract 修饰的类称为抽象类 public abstract class Person { } 抽象类的特征 1) 抽象类不可以创建对象 2) 抽象类可 ...
- python实现十大核心算法(桶排没实例)
# author:sevenduke # 2019-06-11 # 一.交换排序 # 排序算法的温故:冒泡排序 def dubblesort(arr): for i in range(0, len(a ...
- django上传并显示图片
环境 python 3.5 django 1.10.6 步骤 创建名为 testupload的项目 django-admin startproject testupload 在项目testupload ...