• springboot整合mybatis实现增删改查


    立志存高远,笃行践初心

    三更灯火五更鸡,正是男儿读书时。 黑发不知勤学早,白首方悔读书迟。

    立志,标定人生方向;奋斗,创造人生价值,二者相辅相成,互相促进。

    大部分程序员的**「 目标 」都是成为一名优秀的工程师,一名可以统览全局的「 架构师 」**。

    千里之行始于足下

    对于大部分普通人而言,成为一名优秀的架构师还是有一定难度的,「 千里之行始于足下,一步一个脚印,慢慢来 」

    很多小伙伴私下问我,没有实际的开发经验,自学成才、或者是培训班出来的,简历上的项目经验怎么写?我觉得可以简简单单的写一个SSM整合项目。

    目录

    立志存高远,笃行践初心?

    千里之行始于足下?

    亿级流量Java高并发与网络编程实战?

    一、Spring思维导图

    二、Spring

    1、基本概念

    2、Spring的流程图

    3、spring的优点

    4、spring的缺点

    三、SpringMVC

    1、基本概念

    2、SpringMVC的优点

    3、SpringMVC的缺点

    四、mybatis

    1、基本概念

    2、mybatis的优点

    五、前置知识总结

    六、Java程序员简历上的第一个项目

    Spring、SpringMVC、MyBatis整合

    1、大体框架

    ?2、引入jar包

    ?3、配置

    4、编写代码

    5、运行


    一、Spring思维导图

    二、Spring

    1、基本概念

    spring是一个开源开发框架,是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

    spring主要用来开发java应用,构建J2EE平台的web应用。其核心就是提供一种新的机制管理业务对象及其依赖关系。

    2、Spring的流程图

    解析:上面是在Struts结构图的基础上加入了spring流程图,在web.xml配置文件中加入了spring的监听器,在struts.xml配置文件中添加

    
    
    • 1

    是告知Struts2运行时使用spring来管理对象,spring在其中主要做的就是注入实例,所有需要类的实例都由spring管理。

    3、spring的优点

    容器:spring是一个容器,包含并管理对象的生命周期和配置。可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。
    支持AOP:spring提供对AOP的支持,它允许将一些通用任务,如安全、事物、日志等进行集中式处理,从而提高了程序的复用性。
    轻量级框架:spring是轻量级框架,其基本的版本大约2M。
    控制反转:spring通过控制反转实现松耦合。对象们给他们依赖,而不是对象本身,方便解耦,简化开发。
    方便程序测试:spring提供了Junit4的支持,可以通过注解方便的测试spring程序。
    降低java EE API的使用难度:spring对java EE开发中非常难用的一些API(比如JDBC),都提供了封装,使这些API应用难度大大降低。
    方便集成各种优秀框架:spring内部提供了对各种优秀框架(如Struts、mybatis)的直接支持。
    支持声明式事务处理:只需要通过配置就可以完成对事务的管理,而无须手动编程。

    4、spring的缺点

    • 依赖反射,反射影响进程。
    • 太过于依赖设计模式。
    • 控制器过于灵活。
    • 不支持分布式应用。

    三、SpringMVC

    1、基本概念

    属于spring框架的一部分,用来简化MVC架构的web应用程序开发。

    2、SpringMVC的优点

    1. 拥有强大的灵活性,非侵入性和可配置性
    2. 提供了一个前端控制器dispatcherServlet,开发者无需额外开发控制器对象
    3. 分工明确,包括控制器、验证器、命令对象、模型对象、处理程序映射视图解析器,每一个功能实现由一个专门的对象负责完成
    4. 可以自动绑定用户输入,并正确的转换数据类型
    5. 可重用的业务代码:可以使用现有的业务对象作为命令或表单对象,而不需要去扩展某个特定框架的基类。

    3、SpringMVC的缺点

    1. servlet API耦合难以脱离容器独立运行
    2. 太过于细分,开发效率低

    四、mybatis

    1、基本概念

    mybatis是一个简化和实现了java数据持久层的开源框架,它抽象了大量的JDBC冗余代码,并提供了一个简单易用的API和数据库交互。

    2、mybatis的优点

    1. 与JDBC相比,减少了50%以上的代码量。
    2. mybatis是最简单的持久化框架,小巧并且简单易学。
    3. mybatis灵活,不会对应用程序或者数据库的限售设计强加任何影响,SQL写在XML里,从程序代码中彻底分离,降低耦合度,便于统一管理和优化,可重用。
    4. 提供XML标签,支持编写动态SQL语句(XML中使用if,else)。
    5. 提供映射标签,支持对象与数据库的ORM字段关系映射(在XML中配置映射关系,也可以使用注解)

    3、mybatis的缺点

    1. SQL语句的编写工作量较大,对开发人员的SQL语句编写有一定的水平要求。
    2. SQL语句过于依赖数据库,不能随意更换数据库。
    3. 拼接复杂SQL语句时不灵活。

    五、前置知识总结

    Java框架总结

    Spring AOP基础知识总结

    Spring常用注解(绝对经典)

    SpringMVC中put和post如何选择

    @RequestParam、@ModelAttribute、@RequestBody的区别

    mybatis常用注解(绝对经典)

    【MyBatis?基础知识总结1】SQL注入

    【MyBatis?基础知识总结2】MyBatis-Plus

    【MyBatis?基础知识总结3】MyBatis一级缓存和二级缓存

    【MyBatis 基础知识总结 4】动态sql

    【MyBatis 基础知识总结 5】SqlSessionFactory和SqlSession

    【MyBatis?基础知识总结6】Statement、PreparedStatement和CallableStatement

    六、Java程序员简历上的第一个项目

    Spring、SpringMVC、MyBatis整合

    1、大体框架

    2、引入jar包

    3、配置

    与spring整合时,mybatis的配置文件conf.xml(数据源+mapper.xml)可省,将其配置在applicationContext.xml中。

    
    
     
    	
    		
    			
    				classpath:db.properties
    			
    		
    	
     
    	
    	
    		
    		
    		
    		
    	 
    	
    	
    	
    		
    		
    	 
     
    	
    		
    	
    	
    	
    	
    		  
    		
    		  
    	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    数据库配置

    driver=oracle.jdbc.OracleDriver
    url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
    username=orcl
    password=orcl
    
    • 1
    • 2
    • 3
    • 4

    4、编写代码

    (1)entity

    package com.guor.entity;
     
    public class Student {
    	private int id;
    	private String name;
    	private int age;
     
    	...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)mapper

    package com.guor.mapper;
     
    import com.guor.entity.Student;
     
    public interface StudentMapper {
    	public void addStudent(Student student);
     
    	public Student queryStudentByStuNo(int id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    StudentMapper.xml

    
    
    
    	
    	
    	
    		insert into student(id,name,age) values (#{id},#{name},#{age})
    	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    (3)StudentService

    package com.guor.service;
     
    import com.guor.entity.Student;
     
    public interface IStudentService {
    	public void addStudent(Student student);
    	public Student queryStudentByStuNo(int id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    StudentServiceImpl

    package com.guor.service.impl;
     
    import com.guor.entity.Student;
    import com.guor.mapper.StudentMapper;
    import com.guor.service.IStudentService;
     
    public class StudentServiceImpl implements IStudentService {
    	private StudentMapper studentMapper;
    	
    	public void setStudentMapper(StudentMapper studentMapper) {
    		this.studentMapper = studentMapper;
    	}
     
    	@Override
    	public void addStudent(Student student) {
    		studentMapper.addStudent(student);
    	}
     
    	@Override
    	public Student queryStudentByStuNo(int id) {
    		return studentMapper.queryStudentByStuNo(id);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (4)controller

    package com.guor.controller;
     
    import java.util.Map;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.guor.entity.Student;
    import com.guor.service.IStudentService;
     
    @Controller
    @RequestMapping("studentController")
    public class StudentController {
    	@Autowired
    	@Qualifier("studentService")
    	private IStudentService studentService;
     
    	public void setStudentService(IStudentService studentService) {
    		this.studentService = studentService;
    	}
     
    	@RequestMapping("queryStudentByStuNo/{id}")//映射
    	public String queryStudentByStuNo(@PathVariable("id") Integer id,Map map) {
    		Student student = studentService.queryStudentByStuNo(id);
    		map.put("student", student);
    		return "result";
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    (5)配置文件

    
    
    	
    	
    	
    	
    	
    	
    		
    		
    	
    	
    	
    	
    	
    	
    		
    			
    				
    			
    		
    	
    	
    		
    			
    				text/html;charset=UTF-8
    			
    		
    	
    	
    	
    		
    		
    			
    				
    				
    					error
    				
    				
    					error
    				
    			
    		
    	
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    3、index.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    
    
    
    
    Insert title here
    
    
    	queryStudentByStuNo
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    5、运行

    ?? Java学习路线思维导图:Java学习路线思维导图

    ?? Java学习路线配套文章:搬砖工逆袭Java架构师

    ?? Java经典面试题大全:10万字208道Java经典面试题总结(附答案)

    ?? 简介:Java领域优质创作者??、CSDN哪吒公众号作者 、Java架构师奋斗者??

    ?? 扫描主页左侧二维码,加入群聊,一起学习、一起进步

    ?? 欢迎点赞 ?? 收藏 留言 ??

    添加微信,备注1024,赠送Java学习路线思维导图

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    React-Router源码分析-History库
    【元胞自动机】模拟电波在整个心脏中的传导和传播的时空动力学研究(Matlab代码实现)
    k8s 1.24 配置cri-dockerd 使用docker
    Android13将Settings移植到AndroidStudio中
    更多龙蜥自研特性,生产可用的 Anolis OS 8.6 正式发布
    怎么解决Spring的循环依赖
    永久删除怎么恢复?小白适用
    qnx sh: rm: Arg list too long
    vivo全球商城:库存系统架构设计与实践
    Cadence OrCAD Capture 全局修改TitleBlock信息
  • 原文地址:https://blog.csdn.net/m0_67402564/article/details/126114899