com.fasterxml.jackson.core
jackson-databind
2.9.0
org.springframework
spring-webmvc
5.2.10.RELEASE
mysql
mysql-connector-java
8.0.29
javax.servlet
javax.servlet-api
3.1.0
provided
com.alibaba
druid
1.1.16
org.springframework
spring-jdbc
5.2.10.RELEASE
org.mybatis
mybatis
3.5.6
compile
org.mybatis
mybatis-spring
1.3.0
jdbc.properties 连接数据库的信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/my_db_01?useSSL=false
jdbc.username=root
jdbc.password=100863
html
Document
添加新图书
书名
类型
作者
出版社
批量删除
确定
取消
确定
取消
修改
删除
js
public class Book {
private Integer id;
private String name;
private String type;
private String author;
private String publisher;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", type='" + type + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
'}';
}
public Book() {
}
public Book(Integer id, String name, String type, String author, String publisher) {
this.id = id;
this.name = name;
this.type = type;
this.author = author;
this.publisher = publisher;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
}
@Repository
public interface BookDao {
@Insert("insert into ev_books(name,type,author,publisher)values(#{name},#{type},#{author},#{publisher})")
void add(Book book);
@Delete("delete from ev_books where id=#{id}")
void delete(Integer id);
@Delete("")
void deleteByIds(@Param("ids") int[] ids);
@Update("update ev_books set name=#{name},type=#{type},author=#{author},publisher=#{publisher} where id=#{id}")
void update(Book book);
@Select("select * from ev_books where id=#{id}")
Book getById(Integer id);
@Select("select * from ev_books")
List getAll();
}
BookService接口:
public interface BookService {
void add(Book book);
void delete(Integer id);
void deleteByIds(int[] ids);
void update(Book book);
Book getById(Integer id);
List getAll();
}
BookService实现类:
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public void add(Book book) {
bookDao.add(book);
}
@Override
public void delete(Integer id) {
bookDao.delete(id);
}
@Override
public void deleteByIds(int[] ids) {
bookDao.deleteByIds(ids);
}
@Override
public void update(Book book) {
bookDao.update(book);
}
@Override
public Book getById(Integer id) {
return bookDao.getById(id);
}
@Override
public List getAll() {
return bookDao.getAll();
}
}
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public String save(@RequestBody Book book){
bookService.add(book);
return "{'message':'success'}";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
bookService.delete(id);
return "{'message':'success'}";
}
@DeleteMapping
public String deleteByIds(@RequestBody int[] ids){
bookService.deleteByIds(ids);
return "{'message':'success'}";
}
@PutMapping
public String update(@RequestBody Book book){
bookService.update(book);
return "{'message':'success'}";
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
@GetMapping
public List getAll(){
return bookService.getAll();
}
}
ServletContainersInitConfig.class 用来配置servlet容器
// 4.定义一个servlet容器启动的配置,在里面加载spring的配置
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
// 加载spring配置
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
// 加载springMVC配置
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
// 设置哪些请求归属springMVC处理
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
// Post请求乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter("UTF-8");
return new Filter[]{filter};
}
}
MyBatisConfig.class 用来替换xml
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
ssfb.setTypeAliasesPackage("cn.itaxu.domain");
ssfb.setDataSource(dataSource);
return ssfb;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("cn.itaxu.dao");
return msc;
}
}
SpringConfig.class 用来配置spring
@Configuration
@ComponentScan(value = "cn.itaxu",
excludeFilters = @ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
public class SpringConfig {
}
SpringMvcConfig.class 用来配置springMVC
// 3.创建springmvc的配置文件,加载controller对应的bean
@Configuration
@ComponentScan({"cn.itaxu.controller","cn.itaxu.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
SpringMvcSupport.class 用来放行一些静态资源
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
// 当访问/pages/???的时候,走/pages目录下的内容
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
// 当访问/js/???的时候,走/js目录下的内容
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
// 当访问/css/???的时候,走/css目录下的内容
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
// 当访问/plugins/???的时候,走/plugins目录下的内容
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
使用SSM框架开发更加的便捷,真正的实现了高内聚、低耦合,与传统的xml方式不同,代码更加的简洁、清晰,大大提高了开发效率。