注明所有文档和图片完整对照,辟免笔记出错,不能复习

 

package com.ithm.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean; 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 dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/xsytest");
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}

jdbcConfig数据源

第一步配置数据库

第二步 这步配置mybates数据要扫的包和数据类型印射,详细代码可复制于代码

mybatiesConfig

第三步  servlet配置请求解析器

package com.ithm.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpingConfig.class};
} @Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringmvcConfig.class};
} @Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}

package com.ithm.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; @Configuration
@ComponentScan("com.ithm.service")
@PropertySource("classpath:Jdbc.properties")
@Import({jdbcConfig.class,MybatiesConfig.class})
public class SpingConfig {
}

package com.ithm.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration
@ComponentScan("com.ithm.controller")
@EnableWebMvc
public class SpringmvcConfig { }

最后差一人propetie配置文件

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/xsytest
jdbc.username = root
jdbc.password = 142857

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++配置结束,以下是工程文件+++

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import java.util.List;

@RestController
@RequestMapping("/book") public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public boolean update(@RequestBody Book book) {
return bookService.update(book);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getByid(id);
}
@GetMapping
public List<Book> getAll() {
return bookService.getAll();
}
}

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.stereotype.Service; import java.util.List; public interface BookDao {
//@Insert("insert into tbl_book values(null,#{type},#{name})")
@Insert("insert into tbl_book (type, name) values(null,#{type},#{name})")
public void save(Book book);
@Update("update tbl_book set type=#{type},name=#{name} 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(); }

package com.ithm.domain;

public class Book {
private Integer id;
private String type;
private String name;
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; }
@Override
public String toString() {
return "Book{" +
"id=" + id +
", type='" + type + '\'' +
", name='" + name + '\'' +
'}';
}
}

package com.ithm.service.impl;

import com.ithm.dao.BookDao;
import com.ithm.domain.Book;
import com.ithm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List;
@Service
public class BookServiceimpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public boolean save(Book book) {
bookDao.save(book);
return true;
}
@Override
public boolean update(Book book) {
bookDao.update(book);
return true;
}
@Override
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
@Override
public Book getByid(Integer id) {
return bookDao.getByid(id);
}
@Override
public List<Book> getAll() {
return bookDao.getAll();
}
}

package com.ithm.service;

import com.ithm.domain.Book;

import java.util.List;

public interface BookService {
public boolean save(Book book);
public boolean update(Book book);
public boolean delete(Integer id);
public Book getByid(Integer id);
public List<Book> getAll();
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++最后编写测试类和postman工具测试+++++++++++++++++++++++++++

package com.ithm.text;

import com.ithm.config.SpingConfig;
import com.ithm.domain.Book;
import com.ithm.service.BookService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpingConfig.class)
public class servie {
@Autowired
private BookService bookService;
@Test
public void testgetbyid(){
Book book = bookService.getByid(1);
System.out.println("ok");
System.out.println(book); }
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++以上是junit测试++++++++++++++++++++++++++

如果是postMan接口测试需要:

如果是sava接口需要{“a”:"b","ee":"bb"}  pust传这样格式数据上去  选择jsons格式

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++=

开启事务:

需要在springConfig

1  新增注解  @EnableTransactionManagement

2 在jdbcConfig 中新增代码,事务管理器

@Bean
public PlatformTransactionManager platformTransactionManager(DataSource dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager();
ds.setDataSource(dataSource);
return ds;
}



3,最后在servlet接口层上加事务 @Transactional

spirmmvc框架整合手抄版示例,供基础搭建代码对照的更多相关文章

  1. 手写Mybatis和Spring整合简单版示例窥探Spring的强大扩展能力

    Spring 扩展点 **本人博客网站 **IT小神 www.itxiaoshen.com 官网地址****:https://spring.io/projects/spring-framework T ...

  2. s2sh三大框架整合过程(仅供参考)

    三大框架顾名思义就是非常有名的Struts2 ,Hibernate,Spring, 框架整合的方法很多,现在我写一个非常简单的整合过程,相信大家一看就会! 这里使用的struts-2.2.1.1.hi ...

  3. 学习《TensorFlow实战Google深度学习框架 (第2版) 》中文PDF和代码

    TensorFlow是谷歌2015年开源的主流深度学习框架,目前已得到广泛应用.<TensorFlow:实战Google深度学习框架(第2版)>为TensorFlow入门参考书,帮助快速. ...

  4. SSH(Spring Struts2 Hibernate)框架整合(注解版)

    案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 框架:Spring Struts2  Hibernate 案例架构: 1.依赖jar包 pom.xml < ...

  5. SSH(Spring Struts2 Hibernate)框架整合(xml版)

    案例描述:使用SSH整合框架实现部门的添加功能 工程: Maven 数据库:Oracle 案例架构: 1.依赖jar包pom.xml <project xmlns="http://ma ...

  6. 第二个基础框架 — spring — xml版,没用注解 — 更新完毕

    1.什么是spring? 老规矩:百度百科一手 这上面说得太多了,我来提炼一下: spring就是一个轻量级的控制反转( IOC ) 和 面向切面编程( AOP ) 的容量框架.总的来说:本质就是对j ...

  7. [Java] SSH框架笔记_框架整合示例(一)

    本文描述的是框架SSH集成的示例,由于在这个过程中有一些小的细节容易被遗忘,特别撰写了一篇小的博文来记录这个过程,希望对自己以及后来者能够起到积极意义. 本文中使用的框架和版本号为: struts-2 ...

  8. Struts2框架学习第三章——Struts2基础

    本章要点 —  Struts 1框架的基本知识 — 使用Struts 1框架开发Web应用 —  WebWork框架的基本知识 — 使用WebWork框架开发Web应用 — 在Eclipse中整合To ...

  9. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  10. SSM框架整合,以CRM为例子

          Mybatis.SpringMVC练习   CRM系统         回顾 Springmvc  高级参数绑定  数组  List <input type  name=ids /& ...

随机推荐

  1. LyScript 从文本中读写ShellCode

    LyScript 插件通过配合内存读写,可实现对特定位置的ShellCode代码的导出,或者将一段存储在文本中的ShellCode代码插入到程序堆中,此功能可用于快速将自己编写的ShellCode注入 ...

  2. 《ASP.ENT Core 与 RESTful API 开发实战》(第3章)-- 读书笔记(下)

    第 3 章 ASP.NET Core 核心特性 3.5 配置 要访问配置,需要使用 ConfigurationBinder 类,它实现了 IConfigurationBuilder 接口,该接口包括两 ...

  3. MySQL创建函数报错:1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled

    创建函数时报错: 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaratio ...

  4. Net5 WorkService 继承 Quarzt 以及 Net5处理文件上传

    Net5 版本以Core为底层非framework框架的windowservice 服务. 在VS里叫WorkService 可以以CMD方式运行也可以以Windowservice方式运行,部署简单. ...

  5. MFC-ODBC API动态连接配置数据库

    一.ODBC管理器介绍 在Window中,ODBC数据远管理器有6个标签:用户DSN.系统DSN.文件DSN.驱动程序.跟踪.连接池,通常情况下,使用用户DSN或者系统DSN,这里主要了解用户DSN和 ...

  6. ARM中PC和LR寄存器的关系

    我们常常听说的PC,LR到底是什么关系,我这次终于弄明白了.我们都知道,LR是指向PC下一次要执行的地址,但是ARM不同的工作模式,他们有不同的关系.ARM有如下几种工作模式:用户模式,FIQ模式,I ...

  7. CSS实现渐隐渐现效果

    CSS实现渐隐渐现效果 实现渐隐渐现效果是比较常见的一种交互方式,通常的做法是控制display属性值在none和其它值之间切换,虽说功能可以实现,但是效果略显生硬,所以会有这样的需求--希望元素消失 ...

  8. Java集合框架学习(四) LinkedHashSet详解

    LinkedHashSet介绍 前面我们介绍了HashSet和TreeSet. LinkedHashSet也是Set接口的一个实现类,同时还继承了HashSet public class Linked ...

  9. win32- 窗口模板

    主要用于日常的win32窗口的测试 #include <Windows.h> #include <stdio.h> #include <iostream> usin ...

  10. Redis原理再学习03:数据结构-链表 list

    链表list介绍 1. 链表list简介 链表(linked list)是一种基础数据结构,是一种线性表,但是不会按照线性表的顺序存储数据,而是在每一个节点里存到下一个节点的指针. 链表插入节点时是 ...