Spring + SpringMVC + Mybatis整合流程

1      需求

1.1     客户列表查询

1.2     根据客户姓名模糊查询

2      整合思路

第一步:整合dao层

Mybatis和spring整合,通过spring管理mapper接口,使用mapper扫描器自动扫描mapper接口,并在spring中进行注册。

第二步:整合service层

通过spring管理service层,service调用mapper接口。使用配置方式将service接口配置在spring配置文件中,并且进行事务控制。

第三步:整合springMVC

由于springMVC是spring的模块,不需要整合。

3      准备环境

3.1     数据库版本

mysql5.7

3.2

编译器

eclipse

3.3

Jar 包

3.3.1

spring的jar包

3.3.2

spring与mybatis的整合jar包

3.3.3

mybatis的jar包

3.3.4

数据库驱动包

3.3.5

log4j包

3.3.6

log4j配置文件

### direct log messages to stdout ###

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.Target=System.err

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###

log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=c:\mylog.log

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=debug, stdout

3.3.7     dbcp数据库连接池包

3.3.8     jstl包

4      整合dao

4.1      sqlMapconfig.xml

mybatis的配置文件:

/p>

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

4.2      db.properties数据库配置文件

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/haohan1?characterEncoding=utf8&useSSL=false

jdbc.username=root

jdbc.password=123456

4.3     applicationContext-dao.xml

spring在这个xml文件中配置dbcp连接池,sqlSessionFactory,mapper的批量扫描。

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

4.4     逆向工程生成po类和mapper接口和mapper.xml文件

生成如下图的文件:

4.5     自定义mapper接口和xml文件,以及po的包装类

4.5.1     CustomMapper.java

public interfaceCustomMapper {public List findAllCustom(HhCustomVo hhCustomVo)throwsException;

}

4.5.1     CustomMapper.xml

/p>

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

name like '%${hhCustom.name}%'

SELECT

* FROM hh_custom

4.5.2     HhCustomVo

//客户的包装类

public classHhCustomVo {//客户信息

privateHhCustom hhCustom;publicHhCustom getHhCustom() {returnhhCustom;

}public voidsetHhCustom(HhCustom hhCustom) {this.hhCustom =hhCustom;

}

}

4.6     数据库表结构

5      整合service

5.1     定义service接口

public interfaceCustomService {public HhCustom findCustomById(Integer id)throwsException;public List findAllCustom(HhCustomVo hhCustomVo)throwsException;

}

5.2     service接口实现

public class CustomServiceImpl implementsCustomService{

@Autowired

HhCustomMapper hhCustomMapper;

@Autowired

CustomMapper customMapper;

@Overridepublic HhCustom findCustomById(Integer id) throwsException {//TODO Auto-generated method stub

returnhhCustomMapper.selectByPrimaryKey(id);

}

@Overridepublic List findAllCustom(HhCustomVo hhCustomVo) throwsException {//TODO Auto-generated method stub

returncustomMapper.findAllCustom(hhCustomVo);

}

}

5.3     在spring容器配置service(applicationContext-service)

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

5.4   事务控制(applicationContext-transaction)

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

6      整合springMVC

6.1      springmvc.xml

在springmvc.xml中配置适配器映射器、适配器处理器、视图解析器

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd">

6.2     配置前端控制器(web.xml)

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

springmvc

*.action

6.3     编写controller

@Controllerpublic classCustomController {

@Autowired

CustomService customService;//模糊查询客户

@RequestMapping("/findAllCustom")public ModelAndView findAllCustom(HhCustomVo hhCustomVo) throwsException {

List customlist =customService.findAllCustom(hhCustomVo);

ModelAndView modelAndView= newModelAndView();

modelAndView.addObject("customlist", customlist);

modelAndView.setViewName("customlist");returnmodelAndView;

}//根据客户id查询

public ModelAndView findCustomByid(Integer id) throwsException {

HhCustom hhCustom=customService.findCustomById(id);

ModelAndView modelAndView= newModelAndView();

modelAndView.addObject("hhCustom", hhCustom);

modelAndView.setViewName("customlist");returnmodelAndView;

}

}

6.4     编写jsp

客戶列表

查询条件:

客戶名称:

客戶类型:

${customType.value}

--%>

查询

客戶列表:

选择客戶名称客戶邮箱客戶电话客户类型${custom.name }${custom.mail }${custom.phoneNumber }${custom.category }修改 --%>

7      加载spring容器(web.xml)

contextConfigLocation

classpath:spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

8      Post方法中文乱码(web.xml)

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

CharacterEncodingFilter

/*

9      结果

9.1     客户查询列表

9.2     根据模糊

ssm框架连接mysql数据库的具体步骤_ssm框架搭建和整合流程相关推荐

  1. 使用Java语言开发工具idea连接MySQL数据库的基本步骤及操作实例

    Java连接MySQL数据库并进行一些基本操作以及导入jar包的两种方式 其实,任何开发工具连接数据库无非就是三步:1.安装驱动.2.加载驱动,创建连接对象.3.创建对象操作游标.4.游标调用函数完成 ...

  2. pycharm中django框架连接mysql数据库

    1.首先下载安装pymysql模块. pip install pymysql 如果出现 timeout 超时可以使用其他的资源下载: pip install 模块名 -i https://pypi.d ...

  3. ABP框架连接Mysql数据库

    开始想用Abp框架来搭建公司的新项目,虽然一切还没有定数,但是兵马未动,粮草先行,我先尝试一下整个过程,才能够更好的去争取机会. 此次技术选型:Abp(Asp.Net core mvc)+mysql( ...

  4. 十八、Express框架连接MySQL数据库操作

    在上一篇中已经在Node.js中引入使用了mysql模块进行数据库的基本操作,在本篇当中在Express框架中来连接数据库以及操作数据库: Express 项目环境 这里是通过全局安装Express框 ...

  5. Django框架连接MySQL数据库

    pymysql安装 pymysql就是作为python3环境下mysqldb的替代物,进入命令行,使用pip安装pymysql pip install pymysql 在主项目的文件中设置连接 在项目 ...

  6. 使用MyBatis框架连接MySQL数据库查询记录,全部步骤

    系统:Windows 10 ×64 使用软件:eclipse IDE.Navicat Premiun 12.MySQL 5.7 1.在Eclipse IDE中创建Java project 2.配置JA ...

  7. JDBC如何连接mysql数据库附详细步骤

    JDBC连接数据库在学习中是很重要的一个环节,今天给大家详细说明JDBC连接数据库需要的步骤 1.加载驱动 驱动包的下载地址 https://dev.mysql.com/downloads/conne ...

  8. Flask框架连接mysql数据库

    1.安装mysql 下载安装包进行安装,可以参考  安装mysql 安装完成后修改密码 2.登录数据库 进入到安装目录bin下,打开cmd窗口 在cmd出口中输入命令 mysql -u root -p ...

  9. zend framework mysql_Zend Framework连接Mysql数据库实例分析

    这篇文章主要介绍了Zend Framework连接Mysql数据库的方法,以完整实例形式分析了Zend Framework连接MySQL数据库的具体步骤与相关实现技巧,需要的朋友可以参考下 本文实例讲 ...

最新文章

  1. Datawhale组队学习周报(第041周)
  2. 热加载和热部署,没听过?看看 Tomcat 是怎么实现的
  3. 入门级Mat (java版)
  4. 消息积压在消息队列里怎么办
  5. java反序列化 exp_java反序列化-ysoserial-调试分析总结篇(4)
  6. node.js中对 mysql 进行增删改查等操作和async,await处理
  7. kali linux fuzz工具集简述
  8. CF741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths 树启 + 状压
  9. centos 如何登陆mysql_CentOS 配置MySQL允许远程登录
  10. NAR | 陈加余/陈亮合作建立R-loop全基因组分布与调控的专家数据库
  11. 国家机构测评主流电视品牌语音识别 长虹Q5K综合评价最佳
  12. 链家网页爬虫_R爬虫小白入门:Rvest爬链家网+分析(一)
  13. poj 2231 Moo Volume 暴力一定超时啊
  14. windows 7 help 帮助文件无法打开的解决
  15. AZPR3.0的脱壳教程.
  16. 详细介绍如何在华为云调用SDK的Python代码(以文字识别OCR技术实现身份证识别为例)
  17. java将pdf转excel,excel转pdf,itextpdf转换excel
  18. C# Socket通信服务器编写
  19. python 正则re模块 group() groups()
  20. vue中views新建文件夹的代码规范

热门文章

  1. Tomcat的目录结构详细介绍-解压版
  2. 广联达服务器维护,标准图集维护如何操作?
  3. JZOJ6893. 【2020.11.25提高组模拟】小 T 与灵石(stone)题解
  4. 王者荣耀服务器微信和qq,王者荣耀:微信区和qq区的对比,微信区的大部分是坑货?...
  5. 卸载Mac版杀毒软件 airo?
  6. js toFixed
  7. 网易mumu模拟器顶部布局
  8. 当CAD图形中无法进行复制、粘贴时,请将此DWG另存一下或许就可以了
  9. 【openpyxl】设置样式(字体样式、行列宽高、对齐方式、边框、填充和渐变)
  10. spring定时任务 cron的含义