springmvc+mybatis整合
来源:互联网 发布:java删除项目根目录 编辑:程序博客网 时间:2024/06/05 08:24
环境搭建
创建项目动态网络工程
引入jar包
- 全部jar包,包含MySql和Oracle驱动包
db.properties配置数据库连接文件
oracle_driver=oracle.jdbc.driver.OracleDriveroracle_url=jdbc:oracle:thin:@localhost:1521:XEoracle_username=systemoracle_password=dhee
- mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration SYSTEM "com/dhee/dtd/mybatis-3-config.dtd" ><configuration> <settings> <!-- 开启驼峰命名规则,如果表中的列名为:employee_id,则mybatis可以自动对应到类中名为employeeId的成员变量 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings></configuration>
- springmvc.xml配置视图解析器,处理器映射器,处理器适配器
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置自动扫描的包 --> <context:component-scan base-package="com.dhee.ssm"></context:component-scan> <!-- 配置视图解析器 如何把handler 方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler></mvc:default-servlet-handler></beans>
- applicationContext.xml加载数据源,在Spring添加mybatis,使用Spring的事务管理,
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 加载db.properties --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${oracle_driver}" /> <property name="jdbcUrl" value="${oracle_url}" /> <property name="user" value="${oracle_username}" /> <property name="password" value="${oracle_password}" /> </bean> <!-- sqlSessionFactory(在Spring中添加mybatis) --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 需要修改mybatis配置文件路径 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" /> </bean> <!-- mapper扫描器(配置mybatis生成的接口与xxxMapper.xml文件的位置) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 需要修改位置,可能需要创建包 --> <property name="basePackage" value="com.dhee.ssm.mapper"></property> </bean> <!-- 使用spring的事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 以下3种方法出现异常时需要rollback --> <tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /> <!-- 以下4种方法不需要进行事务管理 --> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> <tx:method name="list*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- aop,需要修改位置 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.dhee.springmvc.service.*.*(..))" /> </aop:config> <!-- 自动扫描Service,需要修改位置 --> <!-- <context:component-scan base-package="com.dhee.springmvc.service"></context:component-scan> --></beans>
- web.xml配置Spring,配置前端控制器,配置Spring自带的解决乱码的过滤器
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>testsystem</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- SpringMVC自带用于解决乱码的过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping></web-app>
- 用例驱动
- 向items表中插入输入的数据,成功跳转到success页面,页面显示success
- 页面定义
- index.jsp
<body> <!-- name的命名必须和定义的实体类一致, 其中实体类属性的命名要和数据库的保持一致 --> <form action="items/insertItems" method="post"> <input type="text" name="itemsName"/> <input type="text" name="itemsPrice"/> <input type="text" name="itemsDetails"/> <input type="submit" value="插入"/> </form>
- 实体类创建
- Items.java
public class Items { private Short itemsId; private String itemsName; private BigDecimal itemsPrice; private String itemsDetails;
- mapper定义
- ItemsCustomMapper.java
public interface ItemsCustomMapper { public void insert(Items items) throws Exception;}
- mapper对应的xml文件定义
- ItemsCustomMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" ><!-- namespace:指向对应的mapper接口 --><mapper namespace="com.dhee.ssm.mapper.ItemsCustomMapper" > <!-- id要和mapper接口定义的方法名保持一致 parameterType:接收的数据类型 --> <insert id = "insert" parameterType="com.dhee.ssm.po.Items"> insert into items (items_id,items_name,items_price,items_details) values (items_id_seq.nextval,#{itemsName},#{itemsPrice},#{itemsDetails}) </insert></mapper>
- service和controller的定义
@Servicepublic class ItemsInsertService { @Autowired private ItemsCustomMapper mapper; public void insert(Items items) throws Exception{ mapper.insert(items); }}
public class ItemsController { @Autowired private ItemsInsertService service; @RequestMapping(value="/insert") public String insert(Items record){ try { service.insert(record); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return "success"; }}
- END
阅读全文
0 0
- springmvc+mybatis整合springmvc
- springmvc+mybatis整合
- Spring-SpringMVC-Mybatis整合
- springmvc Mybatis 整合
- SpringMVC+Mybatis整合注解
- springmvc+spring+mybatis整合
- springmvc+spring+mybatis整合
- Spring+SpringMVC+Mybatis整合
- springmvc+mybatis+spring整合
- SpringMVC+mybatis+maven整合
- SpringMvc 3.0 + MyBatis 整合
- SpringMVC+Mybatis整合
- Maven+SpringMVC+MyBatis整合
- springmvc+spring+mybatis 整合
- springMVC+mybatis整合(新手)
- mybatis+springmvc+spring 整合
- SpringMVC+Spring+Mybatis整合
- springmvc+spring+mybatis 整合
- SDUT 2040 数据结构上机实验之顺序查找
- Weblogic java生成wlfullclient.jar
- 礼物分配 (差分约束)
- linux 内存管理、进程的映射、mmap()函数 、mallco()函数的缓冲机制
- codeforces 868C Qualification Rounds
- springmvc+mybatis整合
- angular应用图标的自动生成
- BZOJ 4443 浅谈二分+二分图即四分图性质利用
- weblogic10.0客户端,必须要生成wlfullclient.jar
- poj2411 Mondriaan's Dream(状压dp)
- 一天搞懂深度学习—学习笔记1
- MySQL烂笔头记忆3
- html5网页基本制作和php入门代码
- T-SQL 存储过程与变量