详解spring+springmvc+mybatis整合注解

 更新时间:2017年04月27日 08:19:14   作者:White_Black007  
本篇文章主要介绍了详解spring+springmvc+mybatis整合注解,详细的介绍了ssm框架的使用,具有一定的参考价值,有兴趣的可以了解一下

每天记录一点点,慢慢的成长,今天我们学习了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 }

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

相关文章

  • java中排序报:Comparison method violates its general contract异常的解决

    java中排序报:Comparison method violates its general contract异常的解

    这篇文章主要给大家介绍了关于java中排序报:Comparison method violates its general contract异常的解决方法,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-06-06
  • Spring Boot详解各类请求和响应的处理方法

    Spring Boot详解各类请求和响应的处理方法

    平时只是在用SpringBoot框架,但并没有详细研究过请求和响应执行的一个具体过程,所以本文主要来梳理一下SpringBoot请求和响应的处理过程
    2022-07-07
  • Java关于IO流的全面介绍

    Java关于IO流的全面介绍

    下面小编就为大家带来一篇Java关于IO流的全面介绍。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • 详解java中String值为空字符串与null的判断方法

    详解java中String值为空字符串与null的判断方法

    这篇文章主要介绍了详解java中String值为空字符串与null的判断方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • JAVA内存溢出解决方案图解

    JAVA内存溢出解决方案图解

    这篇文章主要介绍了JAVA内存溢出解决方案图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • Hadoop Combiner使用方法详解

    Hadoop Combiner使用方法详解

    这篇文章主要介绍了 Hadoop Combiner使用方法详解的相关资料,希望通过本文能帮助到大家让大家理解掌握这部分内容,需要的朋友可以参考下
    2017-10-10
  • java中stream去重的几种方式举例

    java中stream去重的几种方式举例

    Stream流是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列,这篇文章主要给大家介绍了关于java中stream去重的几种方式,需要的朋友可以参考下
    2023-07-07
  • Java中的深拷贝(深复制)和浅拷贝(浅复制)介绍

    Java中的深拷贝(深复制)和浅拷贝(浅复制)介绍

    这篇文章主要介绍了Java中的深拷贝(深复制)和浅拷贝(浅复制)介绍,需要的朋友可以参考下
    2015-03-03
  • Springboot Thymeleaf实现HTML属性设置

    Springboot Thymeleaf实现HTML属性设置

    这篇文章主要介绍了Springboot Thymeleaf实现HTML属性设置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2007-11-11
  • springboot枚举类型传递的步骤

    springboot枚举类型传递的步骤

    这篇文章主要介绍了springboot枚举类型传递的步骤,帮助大家更好的理解和学习使用springboot,感兴趣的朋友可以了解下
    2021-04-04

最新评论