ssm(spring+springmvc+mybatis)xml版的整合
来源:互联网 发布:淘宝怎么买游戏账号 编辑:程序博客网 时间:2024/05/16 14:32
1.导包:
2搭建包:
3.mybatis.cfg.xml:
<configuration><!-- 交给spring配置数据源--> <!-- <environments default="oracle"> <environment id="oracle"> <transactionManager type="jdbc"></transactionManager> <dataSource type="pooled"> <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/> <property name="username" value="zhubin"/> <property name="password" value="123"/> </dataSource> </environment> </environments> --> <!--配置映射具体文件 --> <mappers> <mapper resource="entity/empMap.xml"/> </mappers></configuration>
4.spring.xml
<!-- 配置数据源 --> <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="oracle.jdbc.driver.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/> <property name="user" value="zhubin"/> <property name="password" value="123"/> </bean> <!-- 配置 工厂类,及加载mybatis配置文件,替代工厂的作用--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis.cfg.xml"/> <property name="dataSource" ref="comboPooledDataSourceID"/> </bean> <!--配置事务管理器 --> <bean id="dataSourceTransactionManagerID" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- mybatis事务有jdbc管理,配置事务管理器应该由数据源管理,而不是像hibernate交由它的工厂管理--> <property name="dataSource" ref="comboPooledDataSourceID"/> </bean> <!-- 配置事务通知 指定哪些方法需要事务--> <tx:advice id="tx" transaction-manager="dataSourceTransactionManagerID"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务切面 指定哪些方法(一般是service中的方法) --> <aop:config> <aop:pointcut expression="execution(* Dao.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="tx" pointcut-ref="pointcut"/> </aop:config> <bean id="empDaoID" class="Dao.EmpDao"> <property name="sqlsesionfactory" ref="sqlSessionFactoryBean"/> </bean> <bean id="empService" class="service.EmpService"> <property name="empDao" ref="empDaoID"/> </bean> <!-- 扫描识别注解 --> <context:component-scan base-package="action"></context:component-scan> <!-- 识别注解在empaction中的引入的参数 @Resource(name="empService") --> <context:annotation-config/> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean></beans>
5.dao层(mapper):
private SqlSessionFactory sqlsesionfactory; public void setSqlsesionfactory(SqlSessionFactory sqlsesionfactory) { this.sqlsesionfactory = sqlsesionfactory; } public void add(Emp emp){ SqlSession sqlSession = sqlsesionfactory.openSession(); sqlSession.insert("empnamespace.addemp", emp); //int i=1/0; sqlSession.close(); }
6.control:
@Controller@RequestMapping(value="/emp")public class EmpAction { private EmpService empService; @Resource(name="empService") public void setEmpservice(EmpService empService) { this.empService = empService; } @RequestMapping(value="/register") public String empRegister(Emp emp,Model model)throws Exception{ empService.registerMethod(emp); model.addAttribute("message", "messsage"); return "success"; }}
7.总结:比较复杂的spring.xml配置,理清楚 各层之间的bean引用。
阅读全文
0 0
- ssm(spring+springmvc+mybatis)xml版的整合
- SSM(SpringMVC+Spring+Mybatis)整合
- springmvc+mybatis+spring 整合SSM
- 【SSM】Spring+SpringMVC+Mybatis整合
- SSM整合(Spring+SpringMVC+Mybatis)
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- 基于Maven的SSM框架整合(Spring+SpringMVC+MyBatis)
- SSM框架的整合详解(SpringMVC + Spring + MyBatis)
- ssm框架的整合(spring ,springMvc,MyBatis)
- Spring+SpringMVC+MyBatis三大框架整合的SSM
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- SSM(Spring+SpringMVC+MyBatis)整合
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- SSM(springmvc+spring+mybatis)整合过程
- spring、springmvc、mybatis整合(SSM)
- SSM(Spring,SpringMVC,MyBatis)整合开发流程
- SSM(spring+mybatis+springmvc)整合 配置文件
- SSM整合配置文件(Springmvc+Spring+Mybatis)
- 《Learning Sentiment-Specific Word Embedding for Twitter Sentiment Classificaton》阅读笔记
- 链路聚合技术
- ERROR ITMS-90022,90023,问题已解决
- 25 仿函数与函数对象(学自Boolean)
- caffe源代码阅读
- ssm(spring+springmvc+mybatis)xml版的整合
- forward 和redirect的区别
- sql:left join、right join、inner join联表查询
- 剑指Offer---和为s的连续正数序列
- spring-boot 知识积累
- dede后台菜单错位问题,所有文档列表下掉的解决方法
- ShaderWeaver使用教程-美化精灵动画
- 2017/12/13
- Leetcode算法题——Network Delay Time