【建议收藏】手把手带你搭建SSM项目
1.简介SSM
SSM(Spring+SpringMVC+MyBatis) 框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
Spring Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。也可以称之为项目中的粘合剂。 Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地new
一个对象,而是让Spring框架帮你来完成这一切。
SpringMVC SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。SpringMVC相当于SSH框架中struts。
mybatis mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
2.搭建项目
配置环境–>导入mybatis–>编写代码–>测试
搭建ssm项目需要
- mybatis
- mysql
- juit
2.1 File-New-Project
2.2 使用spring initializr 搭建项目
Spring initializr 是Spring 官方提供的一个用来初始化一个Spring boot 项目的工具。
2.3.设置Project 名称
2.4.选择项目中所需要的组件
2.5.保存项目
2.6.检查下maven地址[File-Setting-Maven]
2.7.构建项目格式
- common 放一些公用组件
- controller是前端访问使用
- dao (Mapper 用于和Mybatis进行交货)
- pojo(就是VO)
- service(由Controller调用Service,ServiceImpl,Mapper进行执行数据)
- mapper(mybatis语句注意存放的是Xml文件sql语句)
2.8.配置application.yml
`spring:profiles:server:port: 8100 复制代码`
Tomcat started on port(s): 8100 则启动成功
2.9.配置数据源
这里我们写一个通用的扫描配置数据库
`@Configuration public class DataSourceConfig {@Bean(name = "octMybatisDataSource")@Qualifier("octMybatisDataSource")@ConfigurationProperties(prefix = "spring.datasource")public DataSource octMybatisDataSource() {DruidDataSource dataSource = new DruidDataSource();return dataSource;} } 复制代码`
2.10.配置mybatis扫描路径
<pre>`/*** Mybatis配置,只读*/
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.groot.springbootmybatis.dao.mapper","com.groot.springbootmybatis.dao.mapper"})
public class MiddleMybatisConfig {@Bean(name = "middleMybatisSqlSessionFactory")public SqlSessionFactory testSqlSessionFactory(@Qualifier("octMybatisDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "/mapper/*.xml";bean.setMapperLocations(pathMatchingResourcePatternResolver.getResources(packageSearchPath));return bean.getObject();}@Bean(name = "middleMybatisSqlSessionTemplate")public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("middleMybatisSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
} 复制代码`</pre>
2.11.在application文件中增加数据库配置
<pre>`spring:datasource:url: jdbc:mysql://127.0.0.1:3306/oct?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8username: rootpassword: 1q2w3edriver-class-name: com.mysql.cj.jdbc.DriverinitialSize: 10minIdle: 10maxActive: 30timeBetweenEvictionRunsMillis: 60000validationQuery: SELECT 1testWhileIdle: truetestOnBorrow: falsetestOnReturn: false 复制代码`</pre>
2.12.创建VO
<pre>`public class ExampleDemoVO {@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8")private Date bTime;@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8")private Date eTime;@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd ", timezone = "GMT+8")private Date createTime;@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd ", timezone = "GMT+8")private Date updateTime;private Integer createUser;private Integer updateUser;private Integer valid;private Date rVersion;private String deliveryNo;private Integer bookingId;private Integer taskId;/**
- 页面收货方编码
* */private List<String> receiveNos;public List<String> getReceiveNos() {return receiveNos;}public void setReceiveNos(List<String> receiveNos) {this.receiveNos = receiveNos;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public Date getUpdateTime() {return updateTime;}public void setUpdateTime(Date updateTime) {this.updateTime = updateTime;}public Integer getCreateUser() {return createUser;}public void setCreateUser(Integer createUser) {this.createUser = createUser;}public Integer getUpdateUser() {return updateUser;}public void setUpdateUser(Integer updateUser) {this.updateUser = updateUser;}public Integer getValid() {return valid;}public void setValid(Integer valid) {this.valid = valid;}public Date getrVersion() {return rVersion;}public void setrVersion(Date rVersion) {this.rVersion = rVersion;}public String getDeliveryNo() {return deliveryNo;}public void setDeliveryNo(String deliveryNo) {this.deliveryNo = deliveryNo;}public Integer getBookingId() {return bookingId;}public void setBookingId(Integer bookingId) {this.bookingId = bookingId;}public Integer getTaskId() {return taskId;}public void setTaskId(Integer taskId) {this.taskId = taskId;}
} 复制代码`</pre>
2.13.创建service
`public interface ExampleDemoService {List getExampleList(); } 复制代码`
2.14.创建serviceImpl
在接口的实现类中需要增加注解@Service
`@Service public class ExampleDemoServiceImpl implements ExampleDemoService {@AutowiredExampleMapper exampleMapper;@Overridepublic List getExampleList() {List list= exampleMapper.findExampleList();return list;} } 复制代码`
2.15.创建Mapper
`@Mapper public interface ExampleMapper {List findExampleList(); } 复制代码`
2.16.创建controler
`@RestController @RequestMapping(value = {"/springboot"}) public class MybatisController {@AutowiredExampleDemoService service;@GetMapping("/mybatis")public void hello() {List list= service.getExampleList();System.out.println("获取到的数据为:"+list.size());} } 复制代码`
2.17.访问地址如下
访问地址
2.18.将代码打包
由于集成了maven所以可以直接使用maven打成jar包
2.19.运行Jar包
上一步已经可以看到文件路径
再该路径执行cmd 然后 **java -jar spring-boot-mybatis-0.0.1-SNAPSHOT.jar **
2.20.将项目上传到gitee
20.1 修改.gitignore文件
`/target/ !.mvn/wrapper/maven-wrapper.jar### STS.apt_generated .classpath .factorypath .project .settings .springBeans .sts4-cache### IntelliJ IDEA.idea .idea/ /.idea/ .mvn *.iws *.iml *.ipr mvnw.cmd mvnw /.mvn### NetBeans复制代码``/nbproject/private/ /build/ /nbbuild/ /dist/ /nbdist/ /.nb-gradle/ /.idea /.git`
20.2 初始化文件
`$ git add . #将当前目录所有文件添加到git暂存区 $ git commit -m "my first commit" #提交并备注提交信息 $ git remote add origin https://gitee.com/用户个性地址/HelloGitee.git $ git push origin master #将本地提交推送到远程仓库 复制代码`
3.问题总结
1.The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary
`Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. 复制代码`
问题原因: 升级后的mysql驱动类,Driver位置由com.mysql.jdbc.Driver 变为com.mysql.cj.jdbc.Driver 解决方案: 将数据配置文件里spring.datasource.driver-class-name=com.mysql.jdbc.Driver修改为如下 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2.Invalid bound statement (not found): com.groot.springbootmybatis.dao.mapper.ExampleMapper.findExampleList
2.1 找不到mapper的路径,检查mapper的路径和是否有问题
`复制代码`
3.解决idea新建maven项目时一直loading问题
打开:Setting---->Build Tools → Maven → Importing,
set VM options for importer to -Xmx1024m 中数字改成1024
【建议收藏】手把手带你搭建SSM项目相关推荐
- 从 0 开始手把手带你搭建一套规范的 Vue3.x 项目工程环境
Vue3 跟 Vite 正式版发布有很长一段时间了,生态圈也渐渐丰富起来,作者已在多个项目中使用,总结一下:就是快!也不用担心稳定性问题,开发体验真不是一般好!还没尝试的同学可以从本文开始学习,从 0 ...
- 手把手教你搭建SSM框架(Eclipse版)
作者: C you again,从事软件开发 努力在IT搬砖路上的技术小白 公众号: [C you again],分享计算机类毕业设计源码.IT技术文章.游戏源码.网页模板.程序人生等等.公众号回复 ...
- 手把手带大家搭建一个java个人网站(腾讯云为例)
大家好,我是鸟哥.一个半路出家的程序员. 这次真是学妹要的!前几天鸟哥以腾讯云为例给大家分享了一篇如何搭建服务器的文章--手把手带大家搭建一台服务器(腾讯云为例),文章结尾表示过几天带大家搭建一个网站 ...
- 快速搭建SSM项目【最全教程】~令狐小哥版
快速搭建SSM项目[最全教程]~令狐小哥版 文章目录 快速搭建SSM项目[最全教程]~令狐小哥版 一.创建项目 二.集成spring依赖 三.创建applicationContext.xml文件 四. ...
- 手把手带你搭建个人博客系统(一)
⭐️前言⭐️ 该web开发系统涉及到的知识: Java基础 MySQL数据库 JDBC技术 前端三件套(HTML+CSS+JavaScript) Servlet 使用到的开发工具: idea vsco ...
- 手把手教你搭建SpringCloud项目(十六)集成Stream消息驱动
Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...
- 手把手教你搭建SpringCloud项目(十)集成Hystrix之服务降级
Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...
- 手把手教你搭建SpringCloud项目(九)集成OpenFeign服务接口调用
Spring Cloud全集文章目录: 零.什么是微服务?一看就会系列! 一.手把手教你搭建SpringCloud项目(一)图文详解,傻瓜式操作 二.手把手教你搭建SpringCloud项目(二)生产 ...
- 5min搭建SSM项目
5min搭建SSM项目 1. 环境准备 Java 1.8_281 Mysql 5.7.27 IDEA 2020.1.3 maven 3.6.3 Tomcat 8.5.56 2. 创建项目 1. 使用I ...
最新文章
- cdialog创建后马上隐藏_隐藏你的小秘密,这款神器就是玩的这么6!
- R语言使用str_locate函数和str_locate_all函数来定位特定字符串或者字符串模式在字符串中的位置:str_locate函数第一个位置、str_locate_all函数定位所有位置
- 用脑科学支持人工智能
- Windows 编程[12] - 菜单与菜单资源(一)
- JavaScript获取当前日期时间
- matlab的guide怎么添加函数,整理:matlab如何添加m_map工具箱
- python子进程关闭fd_gpg –passphrase-fd无法使用python 3子进程
- 禁售苹果手机_苹果、华为供应商工厂突发火灾!浓烟冲天
- 从代码规范学到的细节
- spl_autoload_register函数
- HDU1290 献给杭电五十周年校庆的礼物【水题】
- 高效的JavaScript
- 传智播客java课程表,先睹为快
- EEE802.11协议基础知识
- java单击按钮实现窗口隐藏
- 易思ESPCMS企业建站管理系统 P8.21120101 稳定版
- defcon quals 2016 feedme writeup
- 早安心语优美的心情语录
- 微信跳一跳作弊python脚本,非常简单
- SnackBar介绍