100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ssm框架连接mysql数据库的具体步骤_ssm框架搭建和整合流程

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

时间:2020-12-21 04:36:29

相关推荐

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包

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 "-////DTD Config 3.0//EN"

"/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的批量扫描。

/schema/beans/spring-beans.xsd

/schema/context

/schema/context/spring-context.xsd

/schema/aop

/schema/aop/spring-aop.xsd

/schema/tx

/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 "-////DTD Mapper 3.0//EN"

"/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)

/schema/beans/spring-beans.xsd

/schema/context

/schema/context/spring-context.xsd

/schema/aop

/schema/aop/spring-aop.xsd

/schema/tx

/schema/tx/spring-tx.xsd">

5.4 事务控制(applicationContext-transaction)

/schema/beans/spring-beans.xsd

/schema/context

/schema/context/spring-context.xsd

/schema/aop

/schema/aop/spring-aop.xsd

/schema/tx

/schema/tx/spring-tx.xsd">

6 整合springMVC

6.1 springmvc.xml

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

/schema/beans/spring-beans.xsd

/schema/context

/schema/context/spring-context.xsd

/schema/aop

/schema/aop/spring-aop.xsd

/schema/tx

/schema/tx/spring-tx.xsd

/schema/mvc

/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 根据模糊

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。