整合springmvc+mybatis+veloctiy<二>
来源:互联网 发布:wifi网络延时怎么解决 编辑:程序博客网 时间:2024/06/11 18:25
本章讲解结合springmvc+ mybatis
包的结构如图:
1 添加mybatis的依赖包:
<!-- 自动生成包的时候用 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency>
2 添加对应的配置: mybatis-config.xml
<configuration> <settings> <!-- 支持zip_code字段映射成po.zipCode --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings></configuration>
3 添加spring注解的配置: spring-datasource.xml
<context:component-scan base-package="cn.chen" /> <!-- 启用Transactional注解 --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/chendb" /> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- Spring的声明式事务管理 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 声明切入点 --> <aop:config proxy-target-class="true"> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.chen.service..*.*(..))" /> </aop:config> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:cn/chen/mappers/*.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.chen.manager" /> </bean>
-----------------------
此处插一句:数据源采用jndi的方式注入:
配置方法有很多种,此处不一一详述,此处采用的配置,配置步骤为;
1 tomcat中配置;
在context.xml中添加:
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="10" maxIdle="5" maxWait="10" name="chendb" password="root" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/zctest" username="root"/>
2 web.xml 指向:
<resource-ref> <description>connectDB</description> <res-ref-name>chendb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <welcome-file-list>
3 spring中注入:
见spring-datasource.xml
注意:红色标注部分需要保持一致。否则会出问题。【此处欲哭无泪啊,就因为我没配置一致,找了好长时间。】
--------------
4 采用mybatis自动生成的代码:
这两个文件即为自动生成的。
【不多说,去百度也好,去官网也好,都有讲解。】
最后再结合上velocity后才才能进行演示
写在最下面的话:以后所有的文章均发表在勾勾手
0 0
- 整合springmvc+mybatis+veloctiy<二>
- 整合springmvc+mybatis+veloctiy<三>
- Springmvc 与 MyBatis的整合(二)
- springmvc+mybatis整合springmvc
- SpringMVC入门二: 1规范结构, 2简单整合MyBatis
- Java知识总结---整合SpringMVC+Mybatis+Spring(二)
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试
- spring mvc高级篇(二):Spring+SpringMVC+Mybatis整合
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(二)牛刀小试
- springmvc+mybatis整合
- Spring-SpringMVC-Mybatis整合
- springmvc Mybatis 整合
- SpringMVC+Mybatis整合注解
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- SpringMVC+mybatis+maven整合
- 31天学习重构(java)
- 【Android】短信应用——短信截获
- Linux ALSA声卡驱动之四:Control设备的创建
- 如何创建MySQL索引
- 数据结构之红黑树
- 整合springmvc+mybatis+veloctiy<二>
- Linux中sed和awk命令比较实例
- HDU 3046 Pleasant sheep and big big wolf 最小割
- net下多个应用之间的web.config冲突的解决办法
- 组图:中国人互联网的20年
- Spring配置中transactionAttributes的使用方法和作用
- 第7周作业1——背包问题
- adb connect unable to connect to处理方法
- 谷歌招聘新员工五大标准:文凭一边玩去