最终页面效果
工程目录
具体代码实现
pom.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.example
SSM
1.0-SNAPSHOT
SSM
war
UTF-8
1.8
1.8
5.8.2
org.springframework
spring-webmvc
5.2.10.RELEASE
org.springframework
spring-jdbc
5.2.10.RELEASE
org.mybatis
mybatis
3.5.6
org.mybatis
mybatis-spring
1.3.0
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.16
junit
junit
4.12
test
javax.servlet
javax.servlet-api
3.1.0
provided
com.fasterxml.jackson.core
jackson-databind
2.9.0
org.springframework
spring-test
5.2.10.RELEASE
org.apache.maven.plugins
maven-war-plugin
3.3.2
config 包
package 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;
import java.lang.management.PlatformLoggingMXBean;
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 ds=new DataSourceTransactionManager();
ds.setDataSource(dataSource);
return ds;
}
}
package 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 sqlSessionFactory(DataSource dataSource)
{
SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("domain");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer()
{
MapperScannerConfigurer msc=new MapperScannerConfigurer();
msc.setBasePackage("dao");
return msc;
}
}
package 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[]{"/"};
}
}
package 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({"service"})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@ComponentScan({"controller","config"})
@EnableWebMvc
public class SpringMvcConfig {
}
package config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
protected void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
controller包
package controller;
import domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import service.BookService;
import java.util.List;
@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 flag=bookService.update(book);
return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
boolean flag=bookService.delete(id);
return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
}
@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(code,book,msg);
}
@GetMapping
public Result getAll() {
ListbookList=bookService.getAll();
Integer code=bookList!=null?Code.GET_OK:Code.GET_ERR;
String msg=bookList!=null?"":"数据查询失败,请重试!";
return new Result(code,bookList,msg);
}
}
package controller;
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_ERR=20020;
public static final Integer UPDATE_ERR=20030;
public static final Integer GET_ERR=20040;
public static final Integer SYSTEM_ERR=50001;
public static final Integer SYSTEM_TIME_OUT=50002;
public static final Integer SYSTEM_UNKNOW=59999;
public static final Integer BUSINESS_ERR=60002;
}
package controller;
import exception.BusinessException;
import exception.SystemExecption;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemExecption.class)
public Result doException(SystemExecption ex)
{
//记录日志
//发送消息给运维
//发送消息给开发人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doException(BusinessException ex)
{
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(Exception.class)
public Result doException(Exception ex)
{
//记录日志
//发送消息给运维
//发送消息给开发人员
return new Result(Code.SYSTEM_UNKNOW,null,"系统繁忙,请稍后再试!");
}
}
package 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;
}
}
dao包
package dao;
import domain.Book;
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.util.List;
public interface BookDao {
@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
public int save(Book book);
@Update("update tbl_book set type=#{type},name=#{name},description=#{description} where id =#{id}")
public int update(Book book);
@Delete("delete from tbl_book where id=#{id}")
public int delete(Integer id);
@Select("select *from tbl_book where id=#{id}")
public Book getById(Integer id);
@Select("select *from tbl_book")
public ListgetAll();
}
domain包
package domain;
import sun.dc.pr.PRError;
public class Book {
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
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;
}
}
exception包
package exception;
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code,String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
package exception;
public class SystemExecption extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemExecption(Integer code,String message) {
super(message);
this.code = code;
}
public SystemExecption(Integer code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
service包
package service;
import domain.Book;
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 org.springframework.transaction.annotation.Transactional;
import java.util.List;
@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 getAll();
}
service.impl包
package service.impl;
import controller.Code;
import dao.BookDao;
import domain.Book;
import exception.SystemExecption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.BookService;
import java.util.List;
@SuppressWarnings("ALL")
@Service
public class BookServIceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean save(Book book) {
return bookDao.save(book)>0;
}
@Override
public boolean update(Book book) {
return bookDao.update(book)>0;
}
@Override
public boolean delete(Integer id) {
return bookDao.delete(id)>0;
}
@Override
public Book getById(Integer id) {
/*try {
int i = 1 / 0;
}catch (Exception e){
throw new SystemExecption(Code.SYSTEM_TIME_OUT,"服务器访问超时,请重试!",e);
}*/
return bookDao.getById(id);
}
@Override
public List getAll() {
return bookDao.getAll();
}
}
以上是最新学习的SSM框架的基本整合,欢迎大家批评指正。
Original: https://www.cnblogs.com/ljq20204136/p/16499881.html
Author: 山海自有归期
Title: 基于Spring+SpringMvc+MyBatis的简单图书管理系统的实现(SSM标准开发模板)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/579144/
转载文章受原作者版权保护。转载请注明原作者出处!