在写代码之前我们先了解一下这三个框架分别是干什么的?
SSM框架整合配置
IDE: Eclipse
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包
spring与mybatis的整合jar包
mybatis的jar包
数据库驱动包
log4j包
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
数据库连接池包
jstl包
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文件
生成如下图的文件:
自定义mapper接口和xml文件,以及po的包装类
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; }}
数据库表结构
定义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.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的资料与面试题,私信我发给你,另外如果你在技术上面想提升自己的话,可以关注我后面更新的内容,有时间记得帮我点下转发让更多的人看到哦。