SSM整合快速入门案例(一)(2)

简介: SSM整合快速入门案例(一)

5. spring整合springmvc

  • web容器配置类servletConfig
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
//    加载spring核心配置
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }
//    web容器
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }
//    拦截所以请求
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}
  • springmvcConfig
//1.设置该类为配置类
@Configuration
//2.设置扫描的包
@ComponentScan("org.example.controller")
//3.开启webmvc支持(rest风格)
@EnableWebMvc
public class SpringMvcConfig {
}

6. 整合效果简要说明(servletConfig)

  1. 当web容器启动的时候,会加载spring容器和springmvc容器,spring加载spring的bean,springmvc加载springmvc的bean
  2. getRootConfigClasses 是进行根目录配置
    getServletConfigClasses() 是应对web请求处理的配置
    两个配置造出的对象不一样
  3. Servlet创造出的对象能访问Root对象的内容,而Root对象不能访问Servlet对象的内容,这里涉及到父子容器的问题,要想解决这个问题,可以把spring配置和springmvc配置都写进创建ServletConfig的方法中

四、功能模块开发

1. 在domain包中根据数据库表设计对应实体类Book

public class Book {
    //此处省略getter、setter和toString方法
    private Integer id;
    private String type;
    private String name;
    private String description;
}

2. 在dao包编写数据层操作接口BookDao

public interface BookDao {
//    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    @Insert("insert into tbl_book (type,name,description) values(#{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();
}

3. 在service包中编写service接口BookService

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();
}

4. 在serice包中的impl包中编写BookService实现类BookServiceImpl

//定义bean
@Service
public class BookServiceImpl 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();
    }
}

5. 开启事务

  • 在SpringConfig中开启注解式事务驱动
...
@EnableTransactionManagement
public class SpringConfig {
}
  • 在jdbcConfig中配置事务的管理器
public class JdbcConfig {
    ...
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager ds = new DataSourceTransactionManager();
        ds.setDataSource(dataSource);
        return ds;
    }
}
  • 添加事务,配置到对应接口
@Transactional
public class BookServiceImpl implements BookService {
    ...
}

6. controller层BookController

@RestController
@RequestMapping("/books")
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();
    }
}

五、接口测试

在企业开发中业务层开发完后用junit测试,表现层开发完后用postman测试

1. 业务层接口测试示例

  • 测试类代码编写
@RunWith(SpringJUnit4ClassRunner.class)
//指定配置类
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {
    @Autowired
    private BookService bookService;
    @Test
    public void testGetById(){
        Book book = bookService.getById(1);
        System.out.println(book);
    }
    @Test
    public void testGetAll(){
        List<Book> all = bookService.getAll();
        System.out.println(all);
    }
}
  • 运行结果

2. 表现层接口测试

1. 配置tomcat服务器并运行

2. 表现层接口测试示例

`博客内容借鉴了bilibili黑马程序员SSM课程资料,如有侵权,请联系作者删除`

总结

欢迎各位留言交流以及批评指正,如果文章对您有帮助或者觉得作者写的还不错可以点一下关注,点赞,收藏支持一下。

相关文章
|
2月前
ssm使用全注解实现增删改查案例——showEmp.jsp
ssm使用全注解实现增删改查案例——showEmp.jsp
11 0
|
2月前
ssm使用全注解实现增删改查案例——showDept.jsp
ssm使用全注解实现增删改查案例——showDept.jsp
13 0
|
2月前
ssm使用全注解实现增删改查案例——web.xml
ssm使用全注解实现增删改查案例——web.xml
10 0
|
2月前
ssm使用全注解实现增删改查案例——applicationContext.xml
ssm使用全注解实现增删改查案例——applicationContext.xml
10 0
|
2月前
ssm使用全注解实现增删改查案例——EmpServiceImpl
ssm使用全注解实现增删改查案例——EmpServiceImpl
15 0
|
2月前
ssm使用全注解实现增删改查案例——DeptServiceImpl
ssm使用全注解实现增删改查案例——DeptServiceImpl
11 0
|
2月前
ssm使用全注解实现增删改查案例——IEmpService
ssm使用全注解实现增删改查案例——IEmpService
14 0
|
2月前
ssm使用全注解实现增删改查案例——IDeptService
ssm使用全注解实现增删改查案例——IDeptService
13 0
|
2月前
ssm使用全注解实现增删改查案例——Emp
ssm使用全注解实现增删改查案例——Emp
23 0
|
2月前
ssm使用全注解实现增删改查案例——Dept
ssm使用全注解实现增删改查案例——Dept
17 0