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

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包

1e146f39706f813617daa4924455746c.png

a38ab606bf8df84296e7e9364da9a2cf.png

3.3.2

spring与mybatis的整合jar包

d1e87cfcda002024ea8cc282a1f0c83e.png

3.3.3

mybatis的jar包

b4aaa0d144337f70d7e4c64202358155.png

3.3.4

数据库驱动包

8a5e93618276738713c3b137e0618b62.png

3.3.5

log4j包

0f501c35fe3f2a4f8f18131931115248.png

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数据库连接池包

cc523d5bcc7253b85a516c23a83ff8c0.png

3.3.8     jstl包

9b42ac53683bb1e5d01e4a2de79acb7a.png

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文件

生成如下图的文件:

bc3f6b765b75f04cd590a093b1d80a38.png

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

7d0ad66d75b97c4b14428c438c5a2973.png

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     数据库表结构

b8dbf806cf03c0b461bd66db05eec1cf.png

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     客户查询列表

ae7a7b4bb2784085498e246c89c38885.png

9.2     根据模糊

0960e6dc9e868d69e751e1c2d63c4ef6.png


版权声明:本文为weixin_31967777原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。