ssm三大框架整合

目录

1.查询所有记录

web.xml:

 applicationContext.xml:(导入约束:aop,beans,context,mvc,tx,)

db.properties:

ItemInfo.java:

ItemController.java:

ItemMapper.java:

ItemMapper.xml:

ItemService.java:

ItemServicelmpl.java:

item_list.jsp: 

MySQL数据库数据:

运行结果:

2.参数绑定-默认参数,基本类型参数



1.查询所有记录

新建Dynamic Web Project,命名为:ssm_project_springmvc

导入jar包:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssm_project_springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:applicationContext.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

 applicationContext.xml:(导入约束:aop,beans,context,mvc,tx,)

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
		
<!-- 		读取配置文件 数据库 -->
		<context:property-placeholder location="classpath:db.properties"/>
		
<!-- 		配置数据源 -->
		<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="${jdbc.driver}"></property>
			<property name="jdbcUrl" value="${jdbc.url}"></property>
			<property name="user" value="${jdbc.username}"></property>
			<property name="password" value="${jdbc.password}"></property>
		</bean>
		
<!-- 		开启注解扫描 -->
		<context:component-scan base-package="com.sikiedu"></context:component-scan>
		
<!-- 		事务核心管理器 -->
		<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		
<!-- 		开启注解事务 -->
		<tx: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>
		
<!-- 		配置mybatis -->
		<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
			<property name="dataSource" ref="dataSource"></property>
			<property name="typeAliasesPackage" value="com.sikiedu.bean"></property>
		</bean>
		
<!-- 		mapper工厂 -->
		<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
			<property name="basePackage" value="com.sikiedu.mapper"></property>
		</bean>
		
</beans>

db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web01
jdbc.username=root
jdbc.password=root

ItemInfo.java:

/**
 * 
 */
package com.sikiedu.bean;

/**
 *
 *
 * @author Lijiang
 *2019年5月3日
 */

public class ItemInfo {

	private String item_id;
	private String item_name;
	private String item_type;
	private Double item_price;
	public String getItem_id() {
		return item_id;
	}
	public void setItem_id(String item_id) {
		this.item_id = item_id;
	}
	public String getItem_name() {
		return item_name;
	}
	public void setItem_name(String item_name) {
		this.item_name = item_name;
	}
	public String getItem_type() {
		return item_type;
	}
	public void setItem_type(String item_type) {
		this.item_type = item_type;
	}
	public Double getItem_price() {
		return item_price;
	}
	public void setItem_price(Double item_price) {
		this.item_price = item_price;
	}
	@Override
	public String toString() {
		return "ItemInfo [item_id=" + item_id + ", item_name=" + item_name + ", item_type=" + item_type
				+ ", item_price=" + item_price + "]";
	}
	

	
}

ItemController.java:

/**
 * 
 */
package com.sikiedu.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.sikiedu.bean.ItemInfo;
import com.sikiedu.service.ItemService;

/**
 *
 *
 * @author Lijiang
 *2019年5月3日
 */
@Controller
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@RequestMapping("allList.do")
	public ModelAndView list(){
		ModelAndView mav=new ModelAndView();
		
		List<ItemInfo> itemList = itemService.selectAll();
		
		mav.addObject("itemList", itemList);
		mav.setViewName("item_list");

		return mav;
	}
}

ItemMapper.java:

/**
 * 
 */
package com.sikiedu.mapper;

import java.util.List;

import com.sikiedu.bean.ItemInfo;

/**
 *
 *
 * @author Lijiang
 *2019年5月3日
 */

public interface ItemMapper {

	public List<ItemInfo> selectAll();
}

ItemMapper.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">

<mapper namespace="com.sikiedu.mapper.ItemMapper">

<!-- public List<ItemInfo> selectAll(); -->
	<select id="selectAll" resultType="ItemInfo">
		select * from item_info
	</select>

</mapper>

ItemService.java:

/**
 * 
 */
package com.sikiedu.service;

import java.util.List;

import com.sikiedu.bean.ItemInfo;

/**
 *
 *
 * @author Lijiang
 *2019年5月3日
 */

public interface ItemService {

	public List<ItemInfo> selectAll();
}

ItemServicelmpl.java:

/**
 * 
 */
package com.sikiedu.service;

import java.util.List;

import javax.annotation.Resource;

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

import com.sikiedu.bean.ItemInfo;
import com.sikiedu.mapper.ItemMapper;

/**
 *
 *
 * @author Lijiang
 *2019年5月3日
 */
@Service
public class ItemServicelmpl implements ItemService {

	@Autowired
	private ItemMapper itemMapper;
	@Override
	public List<ItemInfo> selectAll() {
		return itemMapper.selectAll();
	}

}

item_list.jsp: 

MySQL数据库数据:

运行结果:

2.参数绑定-默认参数,基本类型参数

ItemMapper.java:

	public ItemInfo selectItemInfoById(String id);

	public void deleteById(String id);

ItemMapper.xml

<!-- 	根据id查询记录。默认参数绑定(HttpServletRequest request,HttpServletResponse response,HttpSession session ,Model model) -->
<!-- public ItemInfo selectItemInfoById(String id); -->
	<select id="selectItemInfoById" parameterType="String" resultType="ItemInfo">
	 	select * from item_info where item_id=#{id}
    </select>

<!-- public void deleteById(String id); -->
	<select id="deleteById" parameterType="String">
		delete from item_info where item_id=#{id}
	</select>

ItemService.java:

	public ItemInfo selectItemInfoById(String id);

	public void deleteById(String id);

ItemServicelmpl.java:

	@Autowired
	private ItemMapper itemMapper;	

	@Override
	public ItemInfo selectItemInfoById(String id) {
		return itemMapper.selectItemInfoById(id);
	}


	@Override
	public void deleteById(String id) {
		itemMapper.deleteById(id);
	}

ItemController.java:

//	参数绑定-默认参数
	@RequestMapping("select.do")
	public String select(HttpServletRequest request,HttpServletResponse response,HttpSession session ,Model model){
		
		String id=request.getParameter("id");
		ItemInfo item= itemService.selectItemInfoById(id);
		List<ItemInfo> itemList=new ArrayList<ItemInfo>();
		itemList.add(item);
		
		model.addAttribute("itemList", itemList);

		return "item_list";
	}
	
//	参数绑定-基本类型
	@RequestMapping("delete.do")
	public String delete(String id){
		
		itemService.deleteById(id);//注意:这里的参数名“id”必须和item_list.jsp中的deleteItem(id)函数的参数名相同!!不然就不能自动映射了

		//重定向到列表页
		return "redirect:allList.do";
	}

item_list.jsp:

 //确认删除
			function deleteItem(id) {
				if(confirm('确实要删除该游戏吗?')) {
					$.post(
						"${pageContext.request.contextPath }/item/delete.do",
						{"id":id},
						function(data){
							window.location.reload();
					});
				}
			}

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="item/allList.do">To item_list.jsp</a>
</body>
</html>

数据库:

运行结果: 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值