代码已经完善,文章内容会在后续补充
代码地址(https://javazhang.lanzn.com/ig8tf1wd2aba
密码:1234)
1.1 .SpringConfig 代码
@Configuration
@ComponentScan({"com.itheima.service" })
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}
第一个注解: 设置当前类为配置类
第二个:包扫描识别Spring组件类
第三个:加载外部配置文件
第四个:导配置包(导入service 是为了自动装配能够找到它 , 在后续代码中有用到service类的代码 , 需要用到自动装配)
第五个:开启事务
1.2JdbcConfig
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 ds = new DruidDataSource();
ds.setDriverClassName(driver);
ds.setUrl(url);
ds.setUsername(username);
ds.setPassword(password);
return ds;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager();
//这里的dataSourse不能用方法调 因为用发放调就会脱离Spring管理 就不是一个东西
ds.setDataSource(dataSource);
return ds;
}
第二个方法是用来配合事务管理的
1.3 MyBatisConfig
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 ssfb = new SqlSessionFactoryBean();
ssfb.setDataSource(dataSource);
//类型别名
ssfb.setTypeAliasesPackage("com.itheima.domain");
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
//指定映射从哪加载
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
这个类是为了在Spring类中集成MyBatis的
第一个方法 创建与数据交互用到的sqlsession
为类型起别名 简化sql语句类型引用
第二个方法 为了绑定mapper的类 ,mapper中做到了进行数据库操作等内容
1.4 jdbc配置类
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=1234
2.1 ServletConfig
package com.itheima.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* 外部容器配置类
*/
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
//记载spring配置类
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
//web容器加载SpringMvcconfig
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
//拦截请求归谁管
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
第一个方法指定SpringConfig 的配置类
第二个指定SpringMVC配置类
第三个指定Spring mvc响应哪些请求
SpringMvc:为构建web项目提供了一个完整的流程
2.2SpringMvcConfig
SpringMvc 配置类
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 {
}
第一个注解: 表明当前类为配置类
第二个注解:扫描包下的组件
第三个注解:功能很强大,开启mvc功能 , 暂时用到解析JSON数据
SpringMvcSupport
package com.itheima.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 {
@Override
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类:
1.BookController
package com.itheima.controller;
import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 控制器类
*/
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
// 这里面的参数从Json数据中来 所以从@RequestBody 请求体中获得
@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) ;
}
// 这种是路径参数 所以从@PathVariable(英文翻译是路径参数) 中获得
@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() {
List<Book> book = bookService.getAll();
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" :"数据查询失败,请重试";
return new Result(code,book,msg);
}
}
使用了Rest风格简化开发 统一规范
Rest风格个人理解
2.Code 编写与前端商量好的协议号
package com.itheima.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_TIMEOUT_ERR = 50002;
public static final Integer BUSINESS_ERR = 50002;
public static final Integer SYSTEM_UNKNOW_ERR = 59999;
}
3.异常处理类
package com.itheima.controller;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//声明类是用来做异常处理的
@RestControllerAdvice
public class ProjectExceptionAdvice {
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//记录日志
//发送消息给运维
//发送邮件给开发
return new Result(ex.getCode() , null ,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null ,ex.getMessage());
}
//告诉他处理什么异常
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
System.out.println("Bug ");
return new Result(Code.SYSTEM_UNKNOW_ERR,"null" , "系统繁忙,请稍后再试");
}
}
分为三部分 系统异常 , 运行异常 ,其他异常 根据返回一个异常类 来表明出现什么问题
3.异常类Result
package com.itheima.controller;
public class Result {
private Object data;
private Integer code;
private String msg;
public Result() {
}
public Result(Integer code,Object data ) {
this.data = data;
this.code = code;
}
public Result(Integer code,Object data, String msg) {
this.data = data;
this.code = code;
this.msg = 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;
}
}
dao层
package com.itheima.dao;
import com.itheima.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 {
// 这里中的type指的是book中属性
//使用这个语句可以少写一个数据库操作类
@Insert("INSERT INTO tbl_book values (null , #{type}, #{name},#{description}) ")
//第二种写法 为了不写id 因为id是默认给的
// 下面的type是表中字段名 这里的是Book中的属性
//@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 List<Book> getAll();
}
执行对数据库操作的内容
domain
package com.itheima.domain;
public class Book {
private Integer id;
private String type;
private String name;
private String description;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
", description='" + description + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int 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;
}
}
用于映射数据库 在MyBatis配置类中会用到这个类
exception类
两个异常处理相关方法
package com.itheima.exception;
public class BusinessException extends RuntimeException{
//添加异常编号
//添加异常编号
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(String message, Integer code) {
super(message);
this.code = code;
}
public BusinessException(String message, Throwable cause, Integer code) {
super(message, cause);
this.code = code;
}
public BusinessException(Throwable cause, Integer code) {
super(cause);
this.code = code;
}
}
package com.itheima.exception;
public class SystemException extends RuntimeException{
//添加异常编号
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(String message, Integer code) {
super(message);
this.code = code;
}
public SystemException(String message, Throwable cause, Integer code) {
super(message, cause);
this.code = code;
}
public SystemException(Throwable cause, Integer code) {
super(cause);
this.code = code;
}
}
service 包
使用了事务
package com.itheima.service;
import com.itheima.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
*/
boolean save(Book book);
/**
* 修改
* @param book
* @return
*/
boolean update(Book book);
/**
* 按id删除
* @param id
* @return
*/
boolean delete(Integer id);
/**
* 按id查询
* @param id
* @return
*/
Book getById(Integer id);
/**
* 查询全部
* @return
*/
List<Book> getAll();
}
在package com.itheima.service.impl;
import com.itheima.controller.Code;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import com.itheima.exception.BusinessException;
import com.itheima.exception.SystemException;
import com.itheima.service.BookService;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookServiceImpl implements BookService {
/**
* 进行添加正确失败的判断是根据BookDao传回来的受影响行数决定 >0表示操作成功
*/
@Autowired
//这里通常会报错 不用理会 可以执行
private BookDao bookDao;
public boolean save(Book book) {
;
return bookDao.save(book) > 0;
}
public boolean update(Book book) {
return bookDao.update(book) > 0;
}
public boolean delete(Integer id) {
return bookDao.delete(id) >0;
}
public Book getById(Integer id) {
//
// if (id == 1){
// throw new BusinessException("错误", Code.BUSINESS_ERR);
// }
//
// //将可能出现的异常进行包装,转换为自定义异常
// try {
// // int i = 1/0;
// } catch (Exception e) {
// throw new SystemException("访问超时请重试", e, Code.SYSTEM_TIMEOUT_ERR);
// }
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
插入代码片
剩下的部分是前端相关在资源中有介绍