详解spring+springmvc+mybatis整合注解
程序员文章站
2024-02-28 11:19:16
每天记录一点点,慢慢的成长,今天我们学习了ssm,这是我自己总结的笔记,大神勿喷!谢谢,主要代码!! !
spring&springmvc&mybatis整合(注解...
每天记录一点点,慢慢的成长,今天我们学习了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 }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
详解Spring 基于 Aspect 注解的增强实现
-
详解spring+springmvc+mybatis整合注解
-
Spring基于注解整合Redis完整实例
-
详解Java注解的实现与使用方法
-
spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
-
详解Spring MVC4 纯注解配置教程
-
java 中MyBatis注解映射的实例详解
-
详解如何全注解方式构建SpringMVC项目
-
详解Spring注解--@Autowired、@Resource和@Service
-
详解Spring Boot中整合Sharding-JDBC读写分离示例