Spring+SpringMVC+Mybatis 整合入门
发布日期:2021-05-04 20:40:32 浏览次数:25 分类:精选文章

本文共 8325 字,大约阅读时间需要 27 分钟。

文章目录

整体结构

在这里插入图片描述

结构图

在这里插入图片描述

Spring 整合SpringMVC

整合SpringMVC我们需要将Spring的配置文件加载到服务器中(配置Spring ioc容器)。

我们可以使用Spring的ContextLoaderListener 监听器去监听ServletContext的生命周期。

org.springframework.web.context.ContextLoaderListener

在启动服务器时监听ServletContext的创建,在这时载入Spring的配置文件,这样就整合成功了。但是有个细节。ContextLoaderListener 默认加载只加载WEB-INF目录下的的applicationContext.xml文件,所以我们Spring的配置文件的名称要设置为applicationContext.xml,但是在一般情况下我们的配置文件都会在resouces下面也就是服务器中WEB-INF/classes下面。所以我们需要配置文件路径:

contextConfigLocation
classpath:applicationContext.xml

web.xml

org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
characterEncodingFilter
/*

SpringMvc.xml

Spring整合Mybatis

我们将Mybatis的配置集成的Spring的配置文件里。这种方式是最方便的!

applicationContext.xml

代理接口的配置accountDaoimpl.xml

insert into account (name,money) values(#{name},#{money});

其余代码

表现层

package com.controller;import com.domain.Account;import com.servcie.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import javax.annotation.Resource;import java.util.List;/** * 账户表现层 */@Controller@RequestMapping("/account")public class AccountController {       @Resource(name = "accountService") // @Autowired    private AccountService accountService;    @RequestMapping("/findAll")    public String findAll(Model model) {           System.out.println("表现层:查询所有账户");        final List
accounts = accountService.findAll();// System.out.println(accounts); model.addAttribute("accounts", accounts); return "list"; } @RequestMapping("saveAccount") public String saveAccount(Account account, Model model) { accountService.saveAccount(account); final List
accounts = accountService.findAll(); model.addAttribute("accounts", accounts); return "list"; }}

业务层

package com.servcie.impl;import com.dao.AccountDao;import com.domain.Account;import com.servcie.AccountService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Service("accountService")public class AccountServiceimpl implements AccountService {       @Autowired    private AccountDao accountDao;    @Override    public List
findAll() { System.out.println("业务层:查询所有账户信息"); final List
all = accountDao.findAll(); return all; } @Override public void saveAccount(Account account) { System.out.println("业务层:保存账户"); accountDao.saveAccount(account); }}

持久层

package com.dao;import com.domain.Account;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;import java.util.List;/** * 账户的dao接口 */@Repositorypublic interface AccountDao {       //查询所有账户//    @Select("select *from Account")   List
findAll(); //保存账户// @Insert("insert into account (name,money) values(#{name},#{money})") void saveAccount(Account account);}

domain

package com.domain;import java.io.Serializable;public class Account implements Serializable {       private Integer id;    private String name;    private Double money;    @Override    public String toString() {           return "Account{" +                "id=" + id +                ", name='" + name + '\'' +                ", money=" + money +                '}';    }    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 Double getMoney() {           return money;    }    public void setMoney(Double money) {           this.money = money;    }}

查询效果展示

在这里插入图片描述

附上spring约束

spring约束

SpringMVC约束

Maven 依赖

org.springframework
spring-core
5.2.5.RELEASE
org.springframework
spring-context
5.2.5.RELEASE
org.springframework
spring-web
5.2.5.RELEASE
org.springframework
spring-oxm
5.2.5.RELEASE
org.springframework
spring-tx
5.2.5.RELEASE
org.springframework
spring-jdbc
5.2.5.RELEASE
org.springframework
spring-webmvc
5.2.5.RELEASE
org.springframework
spring-aop
5.2.5.RELEASE
org.springframework
spring-test
5.2.5.RELEASE
org.aspectj
aspectjweaver
1.9.5
javax.servlet.jsp
javax.servlet.jsp-api
2.3.3
javax.servlet
javax.servlet-api
4.0.1
junit
junit
4.10
org.mybatis
mybatis
3.5.4
org.mybatis
mybatis-spring
2.0.4
mysql
mysql-connector-java
5.1.6
log4j
log4j
1.2.12
c3p0
c3p0
0.9.1.2
上一篇:SpringBoot实现国际化
下一篇:从前序与中序遍历序列构造二叉树

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月18日 03时31分29秒