一,创建Maven的web项目
补全项目结构
二,在pom.xml中导入整合ssm相关的依赖坐标和插件
<!--SSM整合需要的依赖以及插件-->
<dependencies>
<!-- Spring 操作web服务依赖坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--Spring 操作jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--Spring 测试依赖坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<!--Spring操作mybatis依赖坐标-->
<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>
<!--mysql数据库jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--druid数据库连接池依赖坐标-->
<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>
<!--SpringMVC开发需要用到的servlet容器依赖坐标-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--json 依赖坐标-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<!--tomcat web服务器插件-->
<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>
三,SSM整合配置
SSM整合示例:
-
配置包Config
- jdbc配置类
-
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 dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } @Bean public PlatformTransactionManager transactionManager(DataSource dataSource){ DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSource); return transactionManager; } }
需要有个jdbc的配置文件
-
- mybatis配置类
-
public class MyBatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){ SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.itshicha.domain"); return factoryBean; } @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ MapperScannerConfigurer msc = new MapperScannerConfigurer(); msc.setBasePackage("com.itshicha.dao"); return msc; } }
-
- Spring配置类
-
@Configuration//设置Spring配置类 @ComponentScan({"com.itshicha.service"})//扫描bean @PropertySource("classpath:jdbc.properties")//加载jdbc配置文件 @Import({JdbcConfig.class,MyBatisConfig.class})//加载jdbc和mybatis配置类,让Spring管理这两个类 @EnableTransactionManagement public class SpringConfig { }
上面三步是Spring整合mybatis的操作
-
- Springmvc配置类
-
@Configuration @ComponentScan("com.itshicha.controller") @EnableWebMvc public class SpringMvcConfig { }
-
- Servlet配置类
-
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer { protected Class<?>[] getRootConfigClasses() { return new Class[]{SpringConfig.class }; } protected Class<?>[] getServletConfigClasses() { return new Class[]{ SpringMvcConfig.class }; } protected String[] getServletMappings() { return new String[]{"/" }; } }
-
- jdbc配置类
-
控制器Controller
- 请求数据格式类
-
public class Result { private Object data; private Integer code; private String msg; public Result() { } public Result(Integer code,Object data, String msg) { this.data = data; this.code = code; this.msg = msg; } public Result( Integer code,Object data) { this.data = data; this.code = code; } 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 class Code { public static final Integer SAVE_OK = 20011; public static final Integer DELETE_OK = 20021; public static final Integer UPDATE_OK = 20031; public static final Integer GET_OK = 20041; public static final Integer SAVE_ERR = 20010; public static final Integer DELETE_EER = 20020; public static final Integer UPDATE_ERR = 20030; public static final Integer GET_ERR = 20040; }
设置一个类,用静态变量设置业务执行成功还是失败的状态码
-
- 页面控制器
-
@RestController @RequestMapping("/books") public class BookController { @Autowired private BookService bookService; @PostMapping public Result save(@RequestBody Book book) { boolean flag = bookService.save(book); return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag); } @PutMapping public Result update(@RequestBody Book book) { boolean update = bookService.update(book); return new Result(update ? Code.UPDATE_OK : Code.UPDATE_ERR, update); } @DeleteMapping("/{id}") public Result delete(@PathVariable Integer id) { boolean delete = bookService.delete(id); return new Result(delete ? Code.DELETE_OK : Code.DELETE_EER, delete); } @GetMapping("/{id}") public Result getById(@PathVariable Integer id) { Book byId = bookService.getById(id); Integer integer = byId != null ? Code.GET_OK : Code.GET_ERR; String msg = byId != null ? "" : "数据查询失败,请重试"; return new Result(integer, byId,msg); } @GetMapping public Result getAll() { List<Book> all = bookService.getAll(); Integer integer1 = all != null ? Code.GET_OK : Code.GET_ERR; String msg = all != null ? "" : "数据查询失败,请重试"; return new Result(integer1,all,msg); } }
业务的请求方法中返回一个数据格式类,将业务执行的状态码,数据,回执信息,封装成一个对象给前端,前端接收到这个对象,转成json,再根据状态码以及回执信息做出要不要提取数据的决定
-
- 请求数据格式类
-
数据访问层dao
-
public interface BookDao { @Insert("insert into tbl_book value (null,#{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(); }
-
-
数据封装层domain
-
public class Book { private Integer id; private String type; private String name; private String description; public Integer getId() { return id; } public void setId(Integer 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; } @Override public String toString() { return "Book{" + "id=" + id + ", type='" + type + '\'' + ", name='" + name + '\'' + ", description='" + description + '\'' + '}'; } }
-
-
业务层service
-
@Transactional public interface BookService { /** * 保存 * @param book * @return */ 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(); }
业务接口
-
@Service public class BookServletImpl 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(); } }
业务接口的实现类
-