当前位置:网站首页>SSM整合流程
SSM整合流程
2022-08-03 06:34:00 【bobo洁厕灵】
三层结构的理解
- 表示层:负责直接跟用户进行交互,一般也就是指系统的界面,用于数据录入,数据显示等。意味着只做与外观显示相关的工作,不属于他的工作不用做。
- 业务逻辑层:用于做一些有效的验证工作,以更好地保证程序运行的健壮性。如完成数据添加、修改和查询等;不允许指定的文本框中输入空字符串,数据格式是否正确及数据类型验证;用户的权限合法性判断等。通过以上诸多判断以决定是否将操作继续向后传递,尽量保证程序的正常运行。
- 数据访问层:顾名思义,就是专门跟数据库进行交互,执行数据的添加、删除、修改和显示等。需要强调的是,所有的数据对象只在这一层被引用,除数据层之外的任何地方都不应该出现这样的引用。
在SSM的组合:
·SpringMVC(表示层)+Spring(业务层)+Mybatis(持久层)
业务层也是承接层,用来处理业务逻辑,通常使用数据库中的事务来进行,按照web层的要求来对数据层进行增删改查。
service是业务层,是使用一个或多个模型执行操作的方法:
·封装通用的业务逻辑、操作。如一些数据的校验,可以通用处理;
·与数据层交互;
·其他请求:如远程服务获取数据,如第三方api等。
持久层数据大概可以分为三个部分:DAO、文件系统和其他应用数据。
DAO由一下几个部分组成:
·DatabaseConnection:专门负责数据库的打开与关闭操作的类;
·VO:主要由属性、setter、getter、方法组成,VO类中的属性与表中的字段相对应,每一个VO类的对象都表示表中的每一条记录;
·DAO:主要定义操作的接口,定义一系列数据库的原子性操作,如增加、修改、删除和查询;
·impl:DAO接口的真实实现类,完成具体的数据库操作,但是不负责数据的打开和关闭;
·Proxy:代理实现类,主要完成数据库的打开和关闭操作,并且调用真实实现对象的操作;
·Factory:工厂类,通过工厂类取得一个DAO的实例化对象
1.创建工程
创建一个maven的web工程
项目的结构如下
pom.xml导入依赖
2.SSM整合
总的SSM整合所需要的结构
config目录存放的是相关的配置类
controller编写的是Controller类
dao存放的是Dao接口,因为使用的是Mapper接口代理方式,所以没有实现类包
service存的是Service接口,impl存放的是Service实现类
resources:存入的是配置文件,如Jdbc.properties webapp:目录可以存放静态资源
test/java:存放的是测试类
1.Spring
1.SpringConfig配置类的书写
@Configuration //设定当前类为配置类
@ComponentScan({"com.itheima.service"}) //扫描bean对象路径
@PropertySource("classpath:jdbc.properties") //属性文件路径
@Import({JdbcConfig.class,MyBatisConfig.class}) //导入配置文件
@EnableTransactionManagement //
public class SpringConfig {
}
2.MyBatis
1.jdbc.properties文件的书写
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=xhsxcsjcnh
2.JdbcConfig配置类的书写
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean //将方法的返回值作为定义为bean对象
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource
dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager();
ds.setDataSource(dataSource);
return ds;
}
}
3.MybatisConfig配置类的书写
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.itheima.domain");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
3.SpringMVC
1.SpringMvcConfig配置类的书写
@Configuration
@ComponentScan("com.itheima.controller")
@EnableWebMvc
public class SpringMvcConfig {
}
2.ServletConfig(web项目入口)配置类
public class ServletConfig extends
AbstractAnnotationConfigDispatcherServletInitializer {
//加载Spring配置类
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
//加载SpringMVC配置类
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
//设置SpringMVC请求地址拦截规则
protected String[] getServletMappings() {
return new String[]{"/"};
}
//设置post请求中文乱码过滤器
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("utf-8");
return new Filter[]{filter};
}
}
至此,SSM环境搭建完毕,接下来是功能模块的实现
三.功能模块
1.数据库与实现类
建表
domain路径下编写Book模型类
public class Book {
private Integer id;
private String type;
private String name;
//get,set,toString方法省略
}
2.dao(接口加自动代理)
BookDao接口
public interface BookDao {
// @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
@Insert("insert into tbl_book (type,name,description) values(#{type},# {name},#{description})")
public void save(Book book);
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
public void update(Book book);
@Delete("delete from tbl_book where id = #{id}")
public void delete(Integer id);
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
@Select("select * from tbl_book")
public List<Book> getAll();
}
3.service(接口加实现类)
BookService接口
@Transactional
public interface BookService {
/**
* 保存
* @param book
*/
public boolean save(Book book);
/**
* 修改
* @param book
* @return
*/
public boolean update(Book book);
/**
* 按id删除
* @param id
* @return
*/
public boolean delete(Integer id);
/**
* 按id查询
* @param id
* @return
*/
public Book getById(Integer id);
/**
* 查询全部
* @return
*/
public List<Book> getAll();
}
BookService实现类
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public boolean save(Book book) {
bookDao.save(book);
return true;
}
public boolean update(Book book) {
bookDao.update(book);
return true;
}
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
public Book getById(Integer id) {
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
这里出现error,是因为BookDao作为接口,不能创造对象 ,这里使用代理模式创造对象
但是代理对象在IOC里面管理,IOC容器在web服务器启动时才会创建。
所以IDEA在检测依赖关系时,没有找到合适的类注入,提示错误。
但是程序运行是无影响的
解决报错或者无视此报错皆可
4.controller
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public boolean update(@RequestBody Book book) {
return bookService.update(book);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping
public List<Book> getAll() {
return bookService.getAll();
}
}
四.测试
1.单元测试
业务层接口测试,整合JUnit
新建一个测试类,测试service
注入service,编写测试方法
运行测试方法
2.表现层测试(PostMan测试)
边栏推荐
- Cesium loads offline maps and offline terrain
- volatile
- Detailed explanation of cause and effect diagram of test case design method
- Basic syntax of MySQL DDL and DML and DQL
- 【多线程进阶】--- 常见锁策略,CAS,synchronized底层工作原理,JUC,线程安全的集合类,死锁
- 模型训练前后显卡占用对比、多卡训练GPU占用分析【一文读懂】
- Laravel 中使用子查询
- 信息学奥赛一本通T1449:魔板
- 七夕和程序员有毛关系?
- 【Shell】3万字图文讲解带你快速掌握shell脚本编程
猜你喜欢
HCIP笔记整理 2022/7/18
多线程打印ABC(继承+进阶)
word之图表目录中点号位置提升3磅
spark中的cache和checkpoint
MySQL性能优化(硬件,系统配置,表结构,SQL语句)
C语言版本和GCC版本
el-table gets the data attribute of a row in the read data table
Detailed explanation of cause and effect diagram of test case design method
Charles capture shows
solution 帆软11版本参数联动为null查询全部
随机推荐
戳Web3的神话?戳到铁板。
用代码构建UI界面
关于利用canvas画带箭头的直线旋转
Shell脚本之一键安装mysql
Detailed explanation of cause and effect diagram of test case design method
word之图表目录中点号位置提升3磅
在线开启gtid偶发hang住的问题解决
第一章:ARM公司Cortex-M 系列处理器介绍,第二章:嵌入式软件开发介绍和第三章:Cortex-M3和Cortex-M4处理器的一般介绍
924. 尽量减少恶意软件的传播 前缀和
6.nodejs--promise、async-await
【playwright】pytest-playwright增加代理服务选项
神经网络原理及代码实现
Laravel 中使用子查询
【C语言】函数栈帧的创建和销毁详解
(十四)51单片机——LCD1602实现滚动效果
PMP每日一练 | 考试不迷路-8.2(包含敏捷+多选)
我国有关信息方面的法律法规
多线程打印ABC(继承+进阶)
安全狗云原生安全能力全面亮相全球数字经济大会暨ISC互联网安全大会
[机缘参悟-59]:《素书》-6-安于礼仪[安礼章第六]