目录

一.建四个配置文件在resource根目录

二.applicationContext-mybatis.xml核心配置

三.database.properties配置

四.mybatis-config.xml配置

五.springmvc-servlet.xml配置

项目示例


一.建四个配置文件在resource根目录

二.applicationContext-mybatis.xml核心配置

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--读取注解-->
<context:component-scan base-package="service,mapper"></context:component-scan><!--读取数据库配置文件--><context:property-placeholder location="classpath:database.properties"></context:property-placeholder><!-- JNDI获取数据源(使用dbcp连接池)  value值和database.properties名字一致--><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton"><property name="driverClassName" value="${driver}" /><property name="url" value="${url}" /><property name="username" value="${user}" /><property name="password" value="${password}" /><property name="initialSize" value="${initialSize}"/><property name="maxActive" value="${maxActive}"/><property name="maxIdle" value="${maxIdle}"/><property name="minIdle" value="${minIdle}"/><property name="maxWait" value="${maxWait}"/><property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/><property name="removeAbandoned" value="${removeAbandoned}"/><!-- sql 心跳 --><!--开启Evict的定时校验,循环校验  --><property name= "testWhileIdle" value="true"/><!-- 在进行borrowObject处理时,会对拿到的 连接进行校验-false--><property name= "testOnBorrow" value="false"/><!-- 在进行ruturnObject处理时,会对返回的连接进行校验-false --><property name= "testOnReturn" value="false"/><!-- 校验使用的sql语句,validatetionQuery,复杂的校验sql会影响性能 --><property name= "validationQuery" value="select 1"/><!-- 定义Evict的时间间隔,单位:毫秒 --><property name= "timeBetweenEvictionRunsMillis" value="60000"/><!-- 配置每次校验连接的数量,一般等于maxActive --><property name= "numTestsPerEvictionRun" value="${maxActive}"/></bean><!--事务管理--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!--配置mybatis  SqlSessionFactoryBean--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource"></property><property name="configLocation" value="classpath:mybatis-config.xml"></property></bean><!--接口扫描仪 mapper层 优先扫描接口中的注解如果找不到就会找同名xml--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="mapper"></property></bean>
</beans>

三.database.properties配置

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/surveillance?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
user=root
password=root
minIdle=45
maxIdle=50
initialSize=5
maxActive=100
maxWait=100
removeAbandonedTimeout=180
removeAbandoned=true

四.mybatis-config.xml配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!--环境池--><!--三个等级:全等级匹配,不映射,普通映射--><settings><setting name="cacheEnabled" value="true"/><!--全映射--><setting name="autoMappingBehavior" value="FULL"/><!--打印Sql日志到控制台上 方便调试找错--><setting name="logImpl" value="STDOUT_LOGGING"/></settings><!--实体类取别名--><typeAliases><package name="entity"/></typeAliases></configuration>

五.springmvc-servlet.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!--suppress ALL -->
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--<bean name="/hello" class="controller.FristController"></bean>--><!--支持MVC注解--><mvc:annotation-driven></mvc:annotation-driven><!--扫描指定包的注解--><context:component-scan base-package="controller"></context:component-scan><mvc:default-servlet-handler></mvc:default-servlet-handler><!--解决文件上传多部分打包问题,并且设置上传条件--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxInMemorySize" value="5000000"></property><property name="defaultEncoding" value="utf-8"></property></bean>
</beans>

项目示例

①搭建项目结构

②entity创建实体类

package entity;public class Banana {private Integer bid;private String bname;private String bcolor;public Integer getBid() {return bid;}public void setBid(Integer bid) {this.bid = bid;}public String getBname() {return bname;}public void setBname(String bname) {this.bname = bname;}public String getBcolor() {return bcolor;}public void setBcolor(String bcolor) {this.bcolor = bcolor;}
}

③mapper层接口写入方法

package mapper;import entity.Banana;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;import java.util.List;
//专门给mapper加注解   纳入ioc
@Repository
public interface BananaMapper {@Select("select * from banana")public List<Banana> getAll();@Delete("delete from banana where bid=#{bid}")public int delete(Integer bid);
}

④service实现接口

@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean
package service.impl;import entity.Banana;
import mapper.BananaMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.BananaService;import java.util.List;
//component升级版
@Service
public class BananaImpl implements BananaService {//@Autowired 是一个注释,它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。//@Autowired 默认是按照类去匹配,配合 @Qualifier 指定按照名称去装配 bean@AutowiredBananaMapper bm;@Overridepublic List<Banana> getAll() {return bm.getAll();}@Overridepublic int delete(Integer bid) {return bm.delete(bid);}
}

⑤controller控制层

package controller;import entity.Banana;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.BananaService;import java.util.List;@Controller
public class BananaController {@AutowiredBananaService bs;@RequestMapping("/getAll")public String getAll(Model m){List<Banana> list=bs.getAll();m.addAttribute("list",list);return "index.jsp";}@RequestMapping("/delete")public String delete(Integer bid){int num=bs.delete(bid);return "forward:getAll";}
}

⑥运行页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head><title>$Title$</title>
</head>
<script src="js/jquery-1.12.4.js"></script>
<body>
<c:forEach items="${list}" var="l"><p>${l.bid}~~${l.bname}~~${l.bcolor}<a href="delete?bid=${l.bid}">点我删除</a></p>
</c:forEach>

⑦结果

SSM框架整合---详细案例相关推荐

  1. SSM框架整合详细案例

    目录描述 一.创建web项目(使用idea maven) 二.使用maven添加依赖 1.在pom.xml中添加项目所需的所有依赖 2.添加tomcat运行驱动 三.建立整体项目架构 四.搭建myba ...

  2. java ssm小案例_简易的SSM框架整合小案例

    简易的SSM框架整合小案例 一.创建一个web工程的maven项目 1.项目名随便起 2.选择好你的maven路径,然后finish 二.配置pom.xml文件 org.springframework ...

  3. SSM框架整合+简单案例实现

    SSM框架整合+简单案例实现 文章目录 前言 一.Spring+SpringMVC+Mybatis框架整合 1.建立一个新的web项目 2.所需jar包 3.建立数据库表与实体类之间的映射 4.web ...

  4. SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

    SSM框架整合-详细整合教程(Spring+SpringMVC+MyBatis) ✨博主介绍 MyBatis和Spring整合 1.整合原因 2.整合条件 3.整合入门 4.整合MyBatis现有配置 ...

  5. SSM框架整合完整案例

    SSM框架整合 一.整合思路 二.案例实战 1. 项目前期准备 2. 整合dao层 ① mybatis全局配置文件(SqlConfig.xml) ② 配置spring.xml ③ 编写POJO类(ja ...

  6. SSM框架整合入门案例

    文章目录 SSM整合案例 1,SSM整合 1.1 流程分析 1.2 整合配置 步骤1:创建Maven的web项目 步骤2:添加依赖 步骤3:创建项目包结构 步骤4:创建SpringConfig配置类 ...

  7. SSM框架开发-基础案例

    SSM框架整合基础案例详解 1.数据库环境 创建一个存放书籍数据的数据库表 CREATE DATABASE `ssmbuild`;USE `ssmbuild`;DROP TABLE IF EXISTS ...

  8. IDEA优雅整合Maven+SSM框架(详细思路+附带源码)

    前言: 网上很多整合SSM博客文章并不能让初探ssm的同学思路完全的清晰,可以试着关掉整合教程,摇两下头骨,哈一大口气,就在万事具备的时候,开整,这个时候你可能思路全无 ~中招了咩~ ,还有一些同学依 ...

  9. ssm框架使用resultful_SSM框架整合完整案例

    SSM框架整合 一.整合思路 二.案例实战 1. 项目前期准备 2. 整合dao层 ① mybatis全局配置文件(SqlConfig.xml) ② 配置spring.xml ③ 编写POJO类(ja ...

最新文章

  1. 20145307《信息安全系统设计基础》期中总结
  2. redis伪集群安装linux,redis伪集群搭建(亲测无坑)
  3. 终于有篇看的懂的 B 树文章了!
  4. hadoop 2.9.2 yarn配置公平调度器
  5. leetcode 667. Beautiful Arrangement II | 667. 优美的排列 II(Java)
  6. HDLBits答案(7)_Verilog多路选择器
  7. 还需要更多东西吗?这里有一些要做的事情
  8. 15 设置系统分词器
  9. 推荐几款好用的电脑定时执行软件 - 定时执行专家
  10. 注册测绘师 案例分析
  11. U盘加密软件测试简历,U盘加密软件哪个好用?2020U盘加密软件推荐
  12. typescript获取时间戳
  13. 所罗门王的宝藏(高斯消元)
  14. cad2012打开后闪退_打开CAD2012出现闪退问题的解决方法
  15. 【GreenDao学习笔记】SQLite数据库保存float/double小数类型精度丢失
  16. 解除操作系统宽带限制
  17. 呆萝卜与每日优鲜竞品分析
  18. 志愿人生——带给你每一次温暖
  19. C++将类写在头文件中
  20. 短语、直接短语、句柄、素短语

热门文章

  1. 锂电池保护板中的MOS管作用与必备功能介绍
  2. asp毕业设计——基于asp+access的网上图书销售系统设计与实现设计与实现(毕业论文+程序源码)——网上图书销售系统
  3. stm32经典笔试题_经典面试题及解析
  4. 初步对SSM框架中Dao层,Mapper层,service等层的理解
  5. 除了 BAT之外,国内有哪些实力雄厚的物联网平台企业?
  6. 最详细的Python安装教程
  7. Ubuntu20.04服务器重装系统后从头进行环境配置
  8. python成语接龙到为所欲为_ParisGabriel:Python全栈工程师(0基础到精通)教程 第十八课(递归函数、闭包)...
  9. 云呐|IT运维管理主要是做什么?it企业运维管理服务
  10. 上海火车票订票点及代售点地址电话