mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程

最近有粉丝给我留言SSM框架三件套,很重要!自己必须要会,但是不知道该怎么做,所以今天小编给大家整理一个SSM框架的搭建与整合教程案列

在写代码之前我们先了解一下这三个框架分别是干什么的?

  1. SpringMVC:它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求。举个例子,用户在地址栏输入http://网站域名/login,那么springmvc就会拦截到这个请求,并且调用controller层中相应的方法,(中间可能包含验证用户名和密码的业务逻辑,以及查询数据库操作,但这些都不是springmvc的职责),最终把结果返回给用户,并且返回相应的页面(当然也可以只返回json/xml等格式数据)。springmvc就是做前面和后面过程的活,与用户打交道!
  2. Spring:太强大了,以至于我无法用一个词或一句话来概括它。但与我们平时开发接触最多的估计就是IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用这个类的时候为它初始化,很少看到关键字new。另外spring的aop,事务管理等等都是我们经常用到的。
  3. MyBatis:如果你问我它跟鼎鼎大名的Hibernate有什么区别?我只想说,他更符合我的需求。第一,它能自由控制sql,这会让有数据库经验的人编写的代码时能提升数据库访问的效率。第二,它可以使用xml的方式来组织管理我们的sql,因为一般程序出错很多情况下是sql出错,别人接手代码后能快速找到出错地方,甚至可以优化原来写的sql。

SSM框架整合配置

开发环境

IDE: Eclipse

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第1张图片

Jdk: 1.7

数据库: MySQL

注:本例演示采用的开发工具是Eclipse,不要让开发工具限制了你的学习,按照自己的需要来创建就好,用什么工具就按照什么步骤来创建。

项目需求

客户列表查询
根据客户姓名模糊查询

整合思路

第一步:整合dao层

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

第二步:整合service层

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

第三步:整合springMVC

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

我用的是mysql5.7版本,开发工具是ecplise

加入配置文件

mybatis——mybatis配置

spring——spring+springmvc+spring和mybatis配置

jdbc.properties——数据库配置文件

log4j.properties——log日志等

spring的jar包

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第2张图片
4b57239a8750bcab0156738b1de06c42.png

spring与mybatis的整合jar包

315226cdad67af7531fdc86a112b485c.png

mybatis的jar包

c4990da20be4988b181476489949127c.png

数据库驱动包

7cc9e04a9c0a06a57774791527d18662.png

log4j包

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第3张图片

log4j配置文件

### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.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

数据库连接池包

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第4张图片

jstl包

ac60e9894faeee97d94669b2a81838ca.png

整合dao

sqlMapconfig.xml

mybatis的配置文件:

-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">

db.properties数据库配置文件

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/haohan1?characterEncoding=utf8&useSSL=falsejdbc.username=rootjdbc.password=123456

applicationContext-dao.xml

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

生成po类和mapper接口和mapper.xml文件

生成如下图的文件:

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第5张图片

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

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第6张图片

CustomMapper.java

public interface CustomMapper {   public List findAllCustom(HhCustomVo hhCustomVo)throws Exception;}

CustomMapper.xml

-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">          name like '%${hhCustom.name}%'             SELECT      * FROM hh_custom       

HhCustomVo

//客户的包装类public class HhCustomVo {   //客户信息   private HhCustom hhCustom;   public HhCustom getHhCustom() {      return hhCustom;   }   public void setHhCustom(HhCustom hhCustom) {      this.hhCustom = hhCustom;   }}

数据库表结构

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第7张图片

整合service

定义service接口

public interface CustomService {   public HhCustom findCustomById(Integer id)throws Exception;   public List findAllCustom(HhCustomVo hhCustomVo)throws Exception;}

service接口实现

public class CustomServiceImpl implements CustomService{   @Autowired   HhCustomMapper hhCustomMapper;   @Autowired   CustomMapper customMapper;   @Override   public HhCustom findCustomById(Integer id) throws Exception {      // TODO Auto-generated method stub      return hhCustomMapper.selectByPrimaryKey(id);   }   @Override   public List findAllCustom(HhCustomVo hhCustomVo) throws Exception {      // TODO Auto-generated method stub      return customMapper.findAllCustom(hhCustomVo);   }}

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

事务控制(applicationContext-transaction)

整合springMVC

springmvc.xml

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

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

 springmvcorg.springframework.web.servlet.DispatcherServletcontextConfigLocationclasspath:spring/springmvc.xmlspringmvc*.action

编写controller

@Controllerpublic class CustomController {   @Autowired   CustomService customService;   //模糊查询客户   @RequestMapping("/findAllCustom")   public ModelAndView findAllCustom(HhCustomVo hhCustomVo) throws Exception {      List customlist = customService.findAllCustom(hhCustomVo);      ModelAndView modelAndView = new ModelAndView();      modelAndView.addObject("customlist", customlist);      modelAndView.setViewName("customlist");      return modelAndView;   }   //根据客户id查询      public ModelAndView findCustomByid(Integer id) throws Exception {         HhCustom hhCustom = customService.findCustomById(id);         ModelAndView modelAndView = new ModelAndView();         modelAndView.addObject("hhCustom", hhCustom);         modelAndView.setViewName("customlist");         return modelAndView;      }}

编写jsp

客戶列表      查询条件:      
客戶名称: 客戶类型: ${customType.value} --%> 查询 客戶列表: 选择 客戶名称 客戶邮箱 客戶电话 客户类型 ${custom.name } ${custom.mail } ${custom.phoneNumber } ${custom.category } 修改 --%>

加载spring容器(web.xml)

  contextConfigLocationclasspath:spring/applicationContext-*.xmlorg.springframework.web.context.ContextLoaderListener

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

  CharacterEncodingFilterorg.springframework.web.filter.CharacterEncodingFilterencodingUTF-8CharacterEncodingFilter/*

最后,开发这么多年我也总结了一套学习Java的资料与面试题,私信我发给你,另外如果你在技术上面想提升自己的话,可以关注我后面更新的内容,有时间记得帮我点下转发让更多的人看到哦。

mybatis 模糊查询_SSM(Spring + SpringMVC + Mybatis)的搭建与整合流程_第8张图片

你可能感兴趣的:(mybatis,模糊查询,mybatis模糊查询,spring,boot,mybatis,整合,spring,mybatis整合)