使用spring如何实现springmvc与mybatis进行整合

发布时间:2020-11-18 15:21:12 作者:Leah
来源:亿速云 阅读:203

使用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 }

关于使用spring如何实现springmvc与mybatis进行整合问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Spring4+SpringMVC+MyBatis整合思路
  2. SpringMVC + MyBatis整合 【转】

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring springmvc mybatis

上一篇:利用java如何实现遍历二叉树

下一篇:http协议中如何实现HttpCore与Transfer-Encoding

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》