1.搭建开发环境:
1)导入jar包
spring的核心包+事务包+jdbc包+orm包+springmvc包
mabatis的jar包+spring与mybatis整合的包(由mybatis提供)
所需要的jar包:
数据库驱动包:mysql5.1
mybatis的jar包
mybatis和spring整合包
log4j包
dbcp数据库连接池包
spring3.2所有jar包
jstl包
2)数据库环境准备:使用
3)工程结构图 关于如何使用mybaits逆向工程生成配置文件见我的博客地址:mybatis逆向工程详解
2.mybatis的核心配置文件sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 由于 spring与mybatis的整合中使用了mapper扫描,这里就不用配置了,但是要保证mapper.xml和mapper.java要在同目录 同名 --> </configuration>3.bean-dao.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 1.配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="169500"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- mapper扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包 --> <property name="basePackage" value="cn.itcast.ssm.mapper"></property> <!-- 注入sqlSessionFactoryBeanName --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>3.逆向工程生成po类以及mapper(单表的增删改查)
将生成的文件拷贝到工程中.
4.手动定义商品查询mapper
1)包装类ItemsCustomer继承Items即可
<span style="font-family:Courier New;font-size:14px;">public class ItemsCustomer extends Items{ }</span>2)包装类ItemsQueryVO
<span style="font-family:Courier New;font-size:14px;">package cn.itcast.ssm.po; import java.util.List; public class ItemsQueryVo { private Items items; private ItemsCustomer itemsCustomer; //省略getter setter } </span>
ItemsMapperCustomer.xml 输入的是包装类型的 返回的也是包装类型的 这样便于维护
<span style="font-family:Courier New;font-size:14px;"><?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"> <mapper namespace="cn.itcast.ssm.mapper.ItemsMapperCustomer"> <sql id="query_items_where"> <if test="itemsCustomer!=null"> <if test="itemsCustomer.name!=null and itemsCustomer.name!=''"> items.name like "%${itemsCustomer.name}%" </if> </if> </sql> <select id="findItemsList" parameterType="cn.itcast.ssm.po.ItemsQueryVo" resultType="cn.itcast.ssm.po.ItemsCustomer"> select * from items <where> <include refid="query_items_where"></include> </where> </select> </mapper></span>ItemsMapperCustomer.java接口
<span style="font-family:Courier New;font-size:14px;">public interface ItemsMapperCustomer { public List<ItemsCustomer> findItemsList(ItemsQueryVo itemsQueryVo); }</span>5.整合service
ItemsService
public interface ItemsService { /** * 查询所有的商品信息 * @param itemsQueryVo * @return */ public List<ItemsCustomer> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception; /** * 根据商品id查询商品 * @param id * @return */ public ItemsCustomer findItemsById(Integer id) throws Exception; /** * 更新商品信息 * @param id * @param itemsCustomer */ public void updateItems(Integer id,ItemsCustomer itemsCustomer) throws Exception; }ItemsServiceImpl
public class ItemsServiceImpl implements ItemsService { @Autowired private ItemsMapperCustomer itemsMapperCustomer; @Autowired private ItemsMapper itemsMapper; public List<ItemsCustomer> findItemsList(ItemsQueryVo itemsQueryVo) { return itemsMapperCustomer.findItemsList(itemsQueryVo); } public ItemsCustomer findItemsById(Integer id) throws Exception { Items items = itemsMapper.selectByPrimaryKey(id); ItemsCustomer itemsCustomer=new ItemsCustomer(); //将items中的属性拷贝到itemsCustomer BeanUtils.copyProperties(items, itemsCustomer); return itemsCustomer; } public void updateItems(Integer id, ItemsCustomer itemsCustomer) { //添加业务校验,通常在service接口对关键参数进行校验 //检验id是否为空,如果为空则抛出异常 itemsCustomer.setId(id); //这个方法要求必须传入id itemsMapper.updateByPrimaryKeyWithBLOBs(itemsCustomer); } }
<bean id="itemsService" class="cn.itcast.ssm.service.impl.ItemsServiceImpl"></bean>6.事务控制bean-transation.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="REQUIRED" read-only="true"/> <tx:method name="get*" propagation="REQUIRED" read-only="true"/> <tx:method name="select*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.itcast.ssm.service.impl.*.*(..))"/> </aop:config> </beans>7.整合springmvc
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd "> <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 处理器映射器 --> <!-- 处理器适配器 --> <!-- 控制器handler --> <!-- 视图解析器 --> <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan> <mvc:annotation-driven></mvc:annotation-driven> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 配置类型转化器 --><!-- 校验器注册到处理器适配器中 --> <mvc:annotation-driven conversion-service="conversionService" validator="validator"></mvc:annotation-driven> </beans>
package cn.itcast.ssm.controller; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; import cn.itcast.ssm.po.ItemsCustomer; import cn.itcast.ssm.po.ItemsQueryVo; import cn.itcast.ssm.service.ItemsService; import cn.itcast.ssm.validation.ValidationGroup1; @Controller @RequestMapping("/items") public class ItemsController { @Autowired private ItemsService itemsService; @RequestMapping("/queryItems") public ModelAndView queryItems(ItemsQueryVo itemsQueryVo) throws Exception{ List<ItemsCustomer> itemsList = itemsService.findItemsList(itemsQueryVo); ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("itemsList", itemsList); modelAndView.setViewName("itemsList"); return modelAndView; } }
10.将mapper、service、controller加载到spring容器中。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <!-- 1.spring配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/bean-*.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>/WEB-INF/classes/spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- post乱码过虑器 --> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>11.启动服务器 访问
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u014010769/article/details/47354793