SSM整合 ssm整合原理 ssm框架是指Spring(业务层)、SpringMVC(web层)和Mybatis(持久层)将系统分成表现层、controller层、service层、dao层四层从而达到加速开发、简化开发的目的
添加依赖 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > org.example</groupId > <artifactId > SpringMVC_Case</artifactId > <version > 1.0-SNAPSHOT</version > <packaging > war</packaging > <properties > <maven.compiler.source > 8</maven.compiler.source > <maven.compiler.target > 8</maven.compiler.target > </properties > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > 5.2.10.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-jdbc</artifactId > <version > 5.2.10.RELEASE</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-test</artifactId > <version > 5.2.10.RELEASE</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > 3.5.6</version > </dependency > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis-spring</artifactId > <version > 1.3.0</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.47</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid</artifactId > <version > 1.1.16</version > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > 4.12</version > <scope > test</scope > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > javax.servlet-api</artifactId > <version > 3.1.0</version > <scope > provided</scope > </dependency > <dependency > <groupId > com.fasterxml.jackson.core</groupId > <artifactId > jackson-databind</artifactId > <version > 2.9.0</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.apache.tomcat.maven</groupId > <artifactId > tomcat7-maven-plugin</artifactId > <version > 2.1</version > <configuration > <port > 80</port > <path > /</path > </configuration > </plugin > </plugins > </build > </project >
架构及文件分布
配置类 JdbcConfig.java:传递关于数据库的信息,包括地址名字密码等,另外还需要配置事务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.itheima.config;import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;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 public DataSource dataSource () { DruidDataSource dds=new DruidDataSource (); dds.setDriverClassName(driver); dds.setUrl(url); dds.setUsername(username); dds.setPassword(password); return dds; } @Bean public PlatformTransactionManager transactionManager (DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager=new DataSourceTransactionManager (); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } }
Mybatis.java:用来放置Mybatis配置,包括类型包扫描、dao层包扫描
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.itheima.config;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.mapper.MapperScannerConfigurer;import org.springframework.context.annotation.Bean;import javax.sql.DataSource;public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactoryBean (DataSource dataSource) { SqlSessionFactoryBean sfb=new SqlSessionFactoryBean (); sfb.setDataSource(dataSource); sfb.setTypeAliasesPackage("com.itheima.domain" ); return sfb; } @Bean public MapperScannerConfigurer mapperScannerConfigurer () { MapperScannerConfigurer msc=new MapperScannerConfigurer (); msc.setBasePackage("com.itheima.dao" ); return msc; } }
ServletConfig.java:servlet的配置类,用来设置由上往下的加载Spring配置,加载SpringMvc和设置那些请求需要经过SpringMvc处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package com.itheima.config;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class []{SpringConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class []{SpringMvcConfig.class}; } @Override protected String[] getServletMappings() { return new String []{"/" }; } }
SpringMvcConfig.java
1 2 3 4 5 6 7 8 9 10 11 package com.itheima.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration @ComponentScan({"com.itheima.controller","com.itheima.config"}) @EnableWebMvc public class SpringMvcConfig {}
SpringConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.itheima.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.PropertySource;import org.springframework.transaction.annotation.EnableTransactionManagement;@Configuration @ComponentScan({"com.itheima"}) @PropertySource("classpath:jdbc.properties") @Import({JdbcConfig.class,MybatisConfig.class}) @EnableTransactionManagement public class SpringConfig {}
创建pojo文件 Book.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 package com.learn.domain;public class book { private long id; private String type; private String name; private String description; public long getId () { return id; } public void setId (long id) { this .id = id; } public String getType () { return type; } public void setType (String type) { this .type = type; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getDescription () { return description; } public void setDescription (String description) { this .description = description; } }
Dao层(连接数据库) 创建接口BookDao:
其中查询全部返回结果需要一个大的列表所以返回值设为List<pojo类>
通过id号查找只需要返回一个记录,所以返回值设为一个pojo类 即可
新增、删除、修改都不需要返回东西,只需要一个展示已经执行成功的信息这件事交给service来办就可以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 package com.learn.dao;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.apache.ibatis.annotations.Update;import java.awt.print.Book;public interface BookDao { @Select("select * from tbl_book") Boolean select (Book book) ; @Select("select * from tbl_book where id=#{id}") Book getById (Integer id) ; @Insert("insert into tbl_book (type,name,description) values (#{type},#{name},#{description})") void save (Book book) ; @Delete("delete from tbl_book where id=#{id}") void delete (Integer id) ; @Update("update tbl_book set type=#{type},name=#{name},description=#{description} where id=#{id}") void update (Book book) ; }
service层 BookService接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.learn.service;import com.learn.domain.book;import com.sun.org.apache.xpath.internal.operations.Bool;import org.springframework.transaction.annotation.Transactional;import java.util.List;@Transactional public interface BookService { List<Book> select () ; Book getById (Integer id) ; Boolean save (Book Book) ; Boolean delete (Integer id) ; Boolean update (Book Book) ; }
BookService实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package com.learn.service.impl;import com.learn.dao.BookDao;import com.learn.domain.book;import com.learn.service.BookService;import com.sun.org.apache.xpath.internal.operations.Bool;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Service public class BookServiceImpl implements BookService { @Autowired public BookDao bookDao; @Override public List<Book> select () { return bookDao.select(); } @Override public Book getById (Integer id) { return bookDao.getById(id); } @Override public Boolean save (Book Book) { bookDao.save(Book); return true ; } @Override public Boolean delete (Integer id) { bookDao.delete(id); return true ; } @Override public Boolean update (Book Book) { bookDao.update(Book); return true ; } }
前端接收设置 在前后端交换数据的过程中,前后端缺乏约定性的格式
对于这个前后端有个约定:
格式为:类似这种
1 2 3 4 5 6 7 8 9 10 11 12 { "data" : [ { "id" : 1 , "type" : "计算机理论" , "name" : "Spring实战 第5版" , "description" : "Spring入门经典教程,深入理解Spring原理技术内幕" } ] , "code" : 20011 , "msg" : "" }
我们后端需要做的就是:
定义Code,用于设置返回值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.learn.controller;public class Code { public static final Integer GET_OK=20011 ; public static final Integer SAVE_OK=20021 ; public static final Integer DELETE_OK=20031 ; public static final Integer UPDATE_OK=20041 ; public static final Integer GET_ERR=20010 ; public static final Integer SAVE_ERR=20020 ; public static final Integer DELETE_ERR=20030 ; public static final Integer UPDATE_ERR=20040 ; }
定义Result,关于data、code、msg的pojo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 package com.learn.controller;public class result { private Object data; private Integer code; private String msg; public Object getData () { return data; } public void setData (Object data) { this .data = data; } public Integer getCode () { return code; } public void setCode (Integer code) { this .code = code; } public String getMsg () { return msg; } public void setMsg (String msg) { this .msg = msg; } public result (Object data, Integer code, String msg) { this .data = data; this .code = code; this .msg = msg; } public result (Object data, Integer code) { this .data = data; this .code = code; } public result () { } }
BookController,类似于servlet类用来前后端交互
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.learn.controller;import com.learn.domain.book;import com.learn.service.BookService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.util.List;@RestController @RequestMapping("/books") public class BookController { @Autowired private BookService bookService; @GetMapping public result select () { List<Book> list=bookService.select(); Integer code=list!=null ? Code.GET_OK:Code.GET_ERR; String msg= list!=null ? "" :"啥也没查到" ; return new result (list,code,msg); } @GetMapping("/{id}") public result getById (@PathVariable Integer id) { Book book=bookService.getById(id); Integer code=book!=null ? Code.GET_OK:Code.GET_ERR; String msg= book!=null ? "" :"啥也没查到" ; return new result (book,code,msg); } @PostMapping public result save (@RequestBody Book book) { boolean flag= bookService.save(book); return new result (flag,flag ? Code.SAVE_OK:Code.SAVE_ERR); } @DeleteMapping("/{id}") public result delete (@PathVariable Integer id) { boolean flag= bookService.delete(id); return new result (flag,flag?Code.DELETE_OK:Code.DELETE_ERR); } @PutMapping public result update (@RequestBody Book book) { boolean flag= bookService.update(book); return new result (flag,flag?Code.UPDATE_OK:Code.UPDATE_ERR); } }
三层架构的调用基本上是这样的
以删除操作为例:
常见出错
使用maven架构,必须带webapp切记
别忘记在JdbcConfig中设置事务和在service中添加事务
jdbc资源包属性和统一