springmvc(5) springmvc和mybatis整合

目录

整合思路

准备环境

所需要的jar包:

工程结构

整合dao

mybaits自己的配置文件-SqlMapConfig.xml

持久层spring配置文件-springApplication-dao.xml

数据库配置文件-db.properties

日志配置文件-log4j.properties  

 逆向工程生成po类及mapper(单表增删改查)

手动定义商品查询mapper

ItemsCustormer

ItemsMapperCustom.xml

    ItemsMapperCustom.java

整合service

定义service接口以及实现类

在spring容器配置service(config/springApplication-service.xml)

事务控制(config/springApplication-transaction.xml)

整合springmvc

springmvc.xml

配置前段控制器web.xml并加载spring容器

编写handler(就是controller)

编写jsp


需求

使用springmvc和mybatis完成商品列表查询。

整合思路

springmvc+mybaits的系统架构:

准备环境

数据库

所需要的jar包

springmvc版本:spring4.2

 

所需要的jar包:

数据库驱动包:mysql5.1

mybatis的jar包

mybatis和spring整合包

log4j包

dbcp数据库连接池包

spring3.2所有jar包

jstl包

spring包未列完   

工程结构

整合dao

mybaits自己的配置文件-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>

<!-- 全局变量的配置 根据需要再加 比如开启二级缓存等  -->

<!-- 配置别名  -->
	<typeAliases>
	<!-- 批量扫描的别名   -->
	<package name="com.pro.ssm.entity"/>	
	</typeAliases>
	
	
 <!-- mapper在spring中使用了扫描包 不需要再配置mapper 
 必须准备mapper.xml和mapper.java文件同名并且在同一目录
 
 -->
</configuration>

持久层spring配置文件-springApplication-dao.xml

配置:

数据源

SqlSessionFactory

mapper扫描器

<?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: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.xsd 
								   http://www.springframework.org/schema/mvc 
								   http://www.springframework.org/schema/mvc/spring-mvc.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 ">
 
 
		<!-- bean definitions here -->
		<!-- 数据库配置文件  db.properties中的key需要有特殊的命名规则 -->
		<context:property-placeholder location="classpath:config/db.properties"/>
		
		<!-- 定义数据源    -->
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxActive" value="10"></property>
		<property name="maxIdle" value="5"></property>
		</bean>
 
	
	<!-- sqlSessinFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 加载mybatis的配置文件 -->
		<property name="configLocation" value="classpath:config/SqlMapConfig.xml" />
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>
	
<!-- 	<bean id="userdao" class="cn.pro.dao.impl.UserDaoImpl">
	<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean> -->
	
	<!-- 配置mapper的扫描器  如果需要扫描多个包 中间使用 半角逗号隔开 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	<!-- 扫描包路径  -->
	<property name="basePackage" value="com.pro.ssm.dao"></property>
	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
 
</beans>

数据库配置文件-db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.user=root
jdbc.password=123456

日志配置文件-log4j.properties  

src下 

# Global logging configuration
#在开发环境下,日志的级别要设置成DEBUG   
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 逆向工程生成po类及mapper(单表增删改查)

将生成的文件拷贝至工程 中。

手动定义商品查询mapper

针对综合查询mapper,一般情况会有关联查询,建议自定义mapper

定义商品的包装对象

ItemsQueryVo

package com.pro.ssm.entity;


//商品的包装对象 
public class ItemsQueryVo {
	//商品信息 
	private Items items;
	
	//为了系统的可扩展性 对原始生成的po类进行扩展 
	
	private ItemsCustormer itemsCustormer;

	public Items getItems() {
		return items;
	}

	public void setItems(Items items) {
		this.items = items;
	}

	public ItemsCustormer getItemsCustormer() {
		return itemsCustormer;
	}

	public void setItemsCustormer(ItemsCustormer itemsCustormer) {
		this.itemsCustormer = itemsCustormer;
	}
	
	
	
	
	
}

原始的pojo类是通过逆向工程生成的一般不在原始的pojo类做扩展 ,使用继承的类

ItemsCustormer

package com.pro.ssm.entity;

public class ItemsCustormer extends Items{
	//添加商品信息的扩展属性 
		
}

ItemsMapperCustom.xml

sql语句:

         SELECT * FROM items  WHERE items.name LIKE '%笔记本%'

<?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="com.pro.ssm.dao.ItemsMapperCustormer" >

<!-- 定义商品查询的sql片段  就是商品的查询条件 -->
<sql id="query_items_where">
<!-- 使用动态sql 通过if判断 满足条件 进行sql的拼接 -->
	<!-- 商品的查询条件  通过ItemsQueryVo 包装对象中itemsCustormer属性进行传递 -->
	<if test="itemsCustormer!=null">
		<if test="itemsCustormer.name!=null and itemsCustormer.name !=''">
		      items.name LIKE "%${itemsCustormer.name}%"
		</if>
	</if>

</sql>


<!-- 商品列表查询 
parameterType传入包装对象 (包装了查询条件)
resultType 建议使用扩展对象 
-->
<select id="findItemsList" parameterType="com.pro.ssm.entity.ItemsQueryVo" resultType="com.pro.ssm.entity.ItemsCustormer">
SELECT * FROM items 
<where>
<include refid="query_items_where"></include>
</where>
</select>

</mapper>

    ItemsMapperCustom.java

package com.pro.ssm.dao;

import java.util.List;

import com.pro.ssm.entity.ItemsCustormer;
import com.pro.ssm.entity.ItemsQueryVo;

public interface ItemsMapperCustormer {
	//商品的查询列表 
	public List<ItemsCustormer> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
	
	
	
	
}

整合service

让spring管理service接口。

定义service接口以及实现类

service接口

package com.pro.ssm.service;

import java.util.List;

import com.pro.ssm.entity.ItemsCustormer;
import com.pro.ssm.entity.ItemsQueryVo;

public interface ItemsService {
	
	//查询商品列表 
	public List<ItemsCustormer> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
	

}

service实现类 

package com.pro.ssm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.pro.ssm.dao.ItemsMapperCustormer;
import com.pro.ssm.entity.ItemsCustormer;
import com.pro.ssm.entity.ItemsQueryVo;
import com.pro.ssm.service.ItemsService;

public class ItemsServiceImpl implements ItemsService {
	
	@Autowired
	private ItemsMapperCustormer itemsMapperCustormer;

	public List<ItemsCustormer> findItemsList(ItemsQueryVo itemsQueryVo)
			throws Exception {
		// 通过itemsmappercustormer查询数据库 
		
		
		
		return itemsMapperCustormer.findItemsList(itemsQueryVo);
	}

}

在spring容器配置service(config/springApplication-service.xml)

创建springApplication-service.xml,文件中配置service。

<?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: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.xsd 
								   http://www.springframework.org/schema/mvc 
								   http://www.springframework.org/schema/mvc/spring-mvc.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 ">
 
 
	<bean id="itemsService" class="com.pro.ssm.service.impl.ItemsServiceImpl"></bean>

 
</beans>

事务控制(config/springApplication-transaction.xml)

在springApplication-transaction.xml中使用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: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.xsd 
								   http://www.springframework.org/schema/mvc 
								   http://www.springframework.org/schema/mvc/spring-mvc.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 ">
 
 
	<!-- spring 的声明式事务控制  -->
	<!-- 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类  -->
	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<!-- 数据源 dataSource在springapplication-dao中配置了  -->
	<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="SUPPORTS" read-only="true"/>
		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/> 
		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	
	</tx:advice>
	
	<!-- aop -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution (* com.pro.ssm.service.impl.*.*(..))"/>
	</aop:config>
	
	
	
	
 
</beans>

整合springmvc

springmvc.xml

创建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.xsd 
							http://www.springframework.org/schema/mvc 
							http://www.springframework.org/schema/mvc/spring-mvc.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 ">
	<!-- 注解驱动 配置处理器映射器和处理器适配器 -->						
	<mvc:annotation-driven/>							
	<!-- handler批量扫描  -->						
	<context:component-scan base-package="com.pro.ssm.controller"/> 
<!-- 视图解析器 
 解析jsp视图  默认使用jstl标签  classpath得有jstl包-->							
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/WEB-INF/jsp/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>



配置前段控制器web.xml并加载spring容器

将mapper、service、controller加载到spring容器中。

 

建议使用通配符加载上边的配置文件。

在web.xml中,添加spring容器监听器,加载spring容器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	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_3_0.xsd">
  <display-name></display-name>	
  <!-- 加载spring容器  -->
  <context-param>
  		<param-name>contextConfigLocation</param-name>
  		<!-- <param-value>/WEB-INF/class/config/springApplication-dao.xml,/WEB-INF/class/config/springApplication-service.xml,
  		/WEB-INF/class/config/springApplication-transaction.xml
  		</param-value> -->
  		<param-value>/WEB-INF/classes/config/springApplication-*.xml</param-value>
  </context-param>
  <listener>
  		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  
  
  
  <servlet>
  <servlet-name>myspring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:config/springmvc.xml</param-value>
  </init-param>
  
  </servlet>
  
  <servlet-mapping>
  <servlet-name>myspring</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

编写handler(就是controller)

package com.pro.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.pro.ssm.entity.ItemsCustormer;
import com.pro.ssm.service.ItemsService;

//商品的controller


@Controller
public class ItemsController {
	
	@Autowired
	private ItemsService itemsService;
	
	//商品查询 
	@RequestMapping("/queryItems")
	public ModelAndView querryItems() throws Exception{
		List<ItemsCustormer> itemsList= itemsService.findItemsList(null);
		System.out.println("aaa");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("itemsList", itemsList);
		modelAndView.setViewName("items/itemsList");
		return modelAndView;
	}
}

编写jsp

发布测试 spring的简单型框架已经搭好

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值