ssm框架连接mysql数据库的具体步骤_ssm框架搭建和整合流
程mysql下载jar包
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.
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' ###
3.3.7 dbcp数据库连接池包
3.3.8 jstl包
4 整合dao
4.1 l
mybatis的配置⽂件:
/p>
PUBLIC "-////DTD Config 3.0//EN"
"/dtd/mybatis-3-config.dtd">
4.2 db.properties数据库配置⽂件
jdbc.sql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/haohan1?characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123456
4.3 l
spring在这个xml⽂件中配置dbcp连接池,sqlSessionFactory,mapper的批量扫描。
4.4 逆向⼯程⽣成po类和mapper接⼝和l⽂件
⽣成如下图的⽂件:
4.5 ⾃定义mapper接⼝和xml⽂件,以及po的包装类
4.5.1 CustomMapper.java
public interfaceCustomMapper {public List findAllCustom(HhCustomVo hhCustomVo)throwsException; }
4.5.1 l
/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)
5.4 事务控制(applicationContext-transaction)
6 整合springMVC
6.1 l
在l中配置适配器映射器、适配器处理器、视图解析器
6.2 配置前端控制器(l)
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:l
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容器(l)
contextConfigLocation
classpath:spring/applicationContext-*.xml
org.t.ContextLoaderListener
8 Post⽅法中⽂乱码(l)
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论