详解spring+springmvc+mybatis整合注解

发布时间 - 2026-01-11 00:51:20    点击率:

每天记录一点点,慢慢的成长,今天我们学习了ssm,这是我自己总结的笔记,大神勿喷!谢谢,主要代码!! !

spring&springmvc&mybatis整合(注解)

1.jar包

2.引入web.xml文件

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

3.创建实体类

4.引入一个(类名)dao.xml

<update id="update" parameterType="accounting" >
    update accounting set money=#{money} where name=#{name}
  </update>
  <select id="findMoneyByName" parameterType="string" resultType="accounting">
    select * from accounting where name=#{name}
</select>

5.创建一个(类名)dao

public void update(Accounting a);
public Accounting findMoneyByName(String name);

6.写service

public void remit(String from,String to,double money);

7.写serviceimpl

@Service
public class AccountServiceImpl implements AccountService {
  @Autowired
  private AccountDao ad;
  @Override
  public void remit(String from, String to, double money) {
    Accounting fromAccount=ad.findMoneyByName(from);
    fromAccount.setMoney(fromAccount.getMoney()-money);
    ad.update(fromAccount);
    Accounting toAccount=ad.findMoneyByName(to);
    toAccount.setMoney(toAccount.getMoney()+money);
    ad.update(toAccount);
  }

}

8.引入applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

  <!-- 加载db.properties文件中的内容,db.properties文件中key命名要有一定的特殊规则 -->
  <context:property-placeholder location="classpath:db.properties" />
  <!-- 配置数据源 ,dbcp -->

  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${jdbc.driver}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxActive" value="30" />
    <property name="maxIdle" value="5" />
  </bean>
  <!-- sqlSessionFactory -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 数据库连接池 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 加载mybatis的全局配置文件 -->
    <property name="configLocation" value="classpath:sqlMapConfig.xml" />
  </bean>


  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
  </bean>
  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>
  <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* service..*.*(..))"/>
  </aop:config>


  <!-- mapper扫描器 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 扫描包路径,如果需要扫描多个包,中间使用半角逗号隔开 -->
    <property name="basePackage" value="dao"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
  </bean>

</beans>

9.引入db.properties文件和log4j.properties文件

10.引入springmvc.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
  <mvc:annotation-driven></mvc:annotation-driven>
  <context:component-scan base-package="action"></context:component-scan>
  <context:component-scan base-package="service"></context:component-scan>
</beans>

11.jsp页面编写

//index.jsp:
 <form action="account_execute.action" method="post">
  汇款人:<input type="text" name="from"/>
  收款人:<input type="text" name="to"/>
  钱数:<input type="text" name="money"/>
  <input type="submit"/>
 </form>
//message.jsp
${message }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# springmvcmybatis注解  # spring  # mybatis  # 注解  # spring4  # Spring整合Mybatis 扫描注解创建Bean报错的解决方案  # 解决SpringBoot整合Mybatis扫描不到Mapper的问题  # 使用Spring扫描Mybatis的mapper接口的三种配置  # 基于Spring整合mybatis注解扫描是否成功的问题  # 加载  # 半角  # 多个  # 要有  # 这是我  # 大神  # 创建一个  # 配置文件  # 大家多多  # 连接池  # 汇款人  # 实体类  # public  # http  # java  # void  # service  # Accounting  # String  # www 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 如何基于云服务器快速搭建网站及云盘系统?  Python数据仓库与ETL构建实战_Airflow调度流程详解  Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理  网站建设整体流程解析,建站其实很容易!  潮流网站制作头像软件下载,适合母子的网名有哪些?  教你用AI将一段旋律扩展成一首完整的曲子  php嵌入式断网后怎么恢复_php检测网络重连并恢复硬件控制【操作】  JS中使用new Date(str)创建时间对象不兼容firefox和ie的解决方法(两种)  如何在腾讯云免费申请建站?  高端建站三要素:定制模板、企业官网与响应式设计优化  高端云建站费用究竟需要多少预算?  Laravel如何实现邮件验证激活账户_Laravel内置MustVerifyEmail接口配置【步骤】  如何挑选高效建站主机与优质域名?  如何快速搭建安全的FTP站点?  EditPlus中的正则表达式 实战(2)  如何在万网主机上快速搭建网站?  Laravel如何实现本地化和多语言支持?(i18n教程)  iOS发送验证码倒计时应用  如何在Tomcat中配置并部署网站项目?  5种Android数据存储方式汇总  详解Android图表 MPAndroidChart折线图  C语言设计一个闪闪的圣诞树  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  实现点击下箭头变上箭头来回切换的两种方法【推荐】  Android使用GridView实现日历的简单功能  Laravel如何发送邮件_Laravel Mailables构建与发送邮件的简明教程  开心动漫网站制作软件下载,十分开心动画为何停播?  Python结构化数据采集_字段抽取解析【教程】  Laravel怎么实现前端Toast弹窗提示_Laravel Session闪存数据Flash传递给前端【方法】  如何快速搭建高效简练网站?  html5的keygen标签为什么废弃_替代方案说明【解答】  nodejs redis 发布订阅机制封装实现方法及实例代码  Laravel用户认证怎么做_Laravel Breeze脚手架快速实现登录注册功能  Laravel怎么在Blade中安全地输出原始HTML内容  如何用狗爹虚拟主机快速搭建网站?  详解ASP.NET 生成二维码实例(采用ThoughtWorks.QRCode和QrCode.Net两种方式)  Python图片处理进阶教程_Pillow滤镜与图像增强  绝密ChatGPT指令:手把手教你生成HR无法拒绝的求职信  黑客如何通过漏洞一步步攻陷网站服务器?  如何用好域名打造高点击率的自主建站?  矢量图网站制作软件,用千图网的一张矢量图做公司app首页,该网站并未说明版权等问题,这样做算不算侵权?应该如何解决?  Laravel如何创建自定义中间件?(Middleware代码示例)  如何在阿里云部署织梦网站?  Firefox Developer Edition开发者版本入口  Laravel怎么进行数据库事务处理_Laravel DB Facade事务操作确保数据一致性  Laravel怎么实现一对多关联查询_Laravel Eloquent模型关系定义与预加载【实战】  使用C语言编写圣诞表白程序  详解jQuery中基本的动画方法  如何在阿里云通过域名搭建网站?  Laravel如何集成第三方登录_Laravel Socialite实现微信QQ微博登录