您的位置:首页 > 其它

SSM-1(jar)整合开发(简单的商品增删改查)

2020-02-02 15:01 627 查看

SSM-1整合开发(简单的商品增删改查)

1、简介

​ ssm讲的是Spring MVC + Spring +MyBatis整合开发

Spring+ SpringMVC + MyBatis + Mybatis-spring整合包

AOP联盟+织入 + c3p0 数据库连接池 + MySQL连接驱动 + jstl

2、项目目录

3、项目具体

3.1、MyBatis的逆向工程技术

​ 使用MyBatis的逆向工程技术生成Model的实体对象和Mapper方法

​ 数据库模型

3.2、配置文件:

web.xml

WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置项目启动的时候加载spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 3.0的springmvc 默认加载WEB-INF下的dispatcher-servlet.xml文件 3.2的springmvc
加载DispatcherServlet-servlet.xml文件 -->
<init-param>
<!-- 修改黑底springmvc加载的配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 配置编码过滤器  -->
<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>

</web-app>

springMVC.xml

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: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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!--使用配置控制器-->

<!--1、配置一个扫描包-->
<context:component-scan base-package="com.shaoyayu.web.controller"></context:component-scan>

<!--2、配置处理映射-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>

<!--3、配置适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!--配置一个json转换器-->
<property name="messageConverters">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</property>
</bean>

<!--配置一个资源视图解析器
[用来查找JSP的]-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/">
</property>
<!--后缀-->
<property name="suffix" value=".jsp">
</property>
</bean>
<!--配置默认的视图(备注:视图指的是响应的数据格式xml或json数据)-->
</beans>

myBatis.xml

myBatis.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.shaoyayu.model"/>
</typeAliases>
<mappers>
<!-- 批量加载映射文件 -->
<package name="com.shaoyayu.mapper"/>
</mappers>
</configuration>

applicationContext.xml

applicationContext.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: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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

<!-- 1.加载db配置文件  -->
<!--    <context:property-placeholder location="classpath:db.properties"/>-->

<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/db_jdx?useUnicode=true&amp;useSSL=false&amp;characterEncoding=UTF-8"/>
<property name="user" value="root"/>
<property name="password" value="123"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>

<!--配置sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 指定配置文件位置 -->
<property name="configLocation" value="classpath:myBatis.xml"/>
</bean>

<!--配置自动生成Mapper的bean对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.shaoyayu.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
</bean>

<!-- 配置扫描注解 -->
<context:component-scan base-package="com.shaoyayu"/>

<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>

</beans>

log4j.properties

log4j.properties

# Global logging configuration
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

ItemsMapper

com.shaoyayu.mapper.ItemsMapper

package com.shaoyayu.mapper;

import com.shaoyayu.model.Items;
import com.shaoyayu.model.ItemsExample;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface ItemsMapper {

/**
* 返回所有的商品
* @return
*/
List<Items> findAllItems();
其他方法自动生成
......
}

ItemsMapper.xml

com/shaoyayu/mapper/ItemsMapper.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.shaoyayu.mapper.ItemsMapper" >
<select id="findAllItems" resultType="items">
SELECT * FROM items;
</select>

其他方法自动生成
......
</mapper>

IItemsService

com.shaoyayu.service.IItemsService

package com.shaoyayu.service;

import com.shaoyayu.model.Items;

import java.util.List;

public interface IItemsService {

/**
* 查询所有商品
* @return
*/
public List<Items> findAllItems();

/**
* 根据id查询商品
* @param id
* @return
*/
public Items findById(Integer id);

/**
* 上传和修改商品
* @param item
*/
public void saveOrUpdate(Items item);

/**
* 根据商品的id删除商品
* @param id
*/
public void deleteById(Integer id);
}

ItemsServiceImpl

com.shaoyayu.service.impl.ItemsServiceImpl

package com.shaoyayu.service.impl;

import com.shaoyayu.mapper.ItemsMapper;
import com.shaoyayu.model.Items;
import com.shaoyayu.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
//注解一个bean对象
@Service
//注解事务处理
@Transactional
public class ItemsServiceImpl implements IItemsService {
//自动注入对象
@Autowired
private ItemsMapper itemsMapper;

@Override
public List<Items> findAllItems() {
return itemsMapper.findAllItems();
}

@Override
public Items findById(Integer id) {
return itemsMapper.selectByPrimaryKey(id);
}

@Override
public void saveOrUpdate(Items item) {
if (item.getId()==null){
itemsMapper.insert(item);
/**
* 当事务开启的时候出现异常的时候不会执行
*/
//            int i =10/0;
}else {
itemsMapper.updateByPrimaryKeySelective(item);
}
}

@Override
public void deleteById(Integer id) {
itemsMapper.deleteByPrimaryKey(id);
}
}

ItemsController

com.shaoyayu.web.controller.ItemsController

package com.shaoyayu.web.controller;

import com.shaoyayu.model.Items;
import com.shaoyayu.service.IItemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("items")
public class ItemsController {

@Autowired
private IItemsService itemsService;

@RequestMapping("list")
public String list(Model model){
List<Items> items = itemsService.findAllItems();
model.addAttribute("items",items);
return "items/list";
}

@RequestMapping("save")
public String save(){
//保存数据到数据库中
//        itemsService.saveOrUpdate(items);
return "redirect:list.do";
}

@RequestMapping("delete.do")
//方法必须传值id
public String delete(@RequestParam(value = "id",required = true) Integer id){
itemsService.deleteById(id);
System.out.println(id+"的商品已经删除......");
return "redirect:list.do";
}

@RequestMapping("edit.do")
//方法必须传值id
public String edit(@RequestParam(value = "id",required = true) Integer id,Model model){
//将查询到的商品展示在页面上编辑
Items item = itemsService.findById(id);
model.addAttribute("item",item);
return "items/edit";
}
@RequestMapping("update.do")
//方法必须传值id
public String update(Items items){
//将查询到的商品展示在页面上编辑
items.setCreatetime(new Date());
System.out.println(items.toString());
itemsService.saveOrUpdate(items);
return "redirect:list.do";
}
}

list.jsp

WEB-INF/views/items/list.jsp

<%--
Created by IntelliJ IDEA.
User: shaoyayu
Date: 2019/8/14
Time: 22:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>商品列表</title>
</head>
<body>
<h1>商品列表</h1>
<hr/>
<table border="1">
<tr>
<td>名字</td>
<td>价格</td>
<td>介绍</td>
<td>日期</td>
<td>操作</td>
</tr>
<c:forEach items="${items}" var="item">
<tr>
<td><c:out value="${item.name}"/></td>
<td><c:out value="${item.price}"/></td>
<td><c:out value="${item.detail}"/></td>
<td><c:out value="${item.createtime}"/></td>
<td><a href="${pageContext.request.contextPath}/items/delete.do?id=${item.id}">删除</a>
&nbsp;&nbsp;
<a href="${pageContext.request.contextPath}/items/edit.do?id=${item.id}">编辑</a></td>
</tr>
</c:forEach>
</table>
<hr/>
<h1><a href="#"> 添加商品</a></h1>
</body>
</html>

edit.jsp

WEB-INF/views/items/edit.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: shaoyayu
Date: 2019/8/15
Time: 0:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>编辑商品</title>
</head>
<body>
<h1>编辑:${item.id}号商品</h1>
<hr/>
<form action="${pageContext.request.contextPath}/items/update.do" method="post">
<input type="hidden" name="id" value="${item.id}">
<table border="1">
<tr>
<td>名字</td>
<td><input type="text" name="name" value="${item.name}"></td>
</tr>
<tr>
<td>价格</td>
<td><input type="text" name="price" value="${item.price}"></td>
</tr>
<tr>
<td>描述</td>
<td><textarea cols="25" rows="5"  name="detail" >
<c:out value="${item.detail}"/>
</textarea></td>
</tr>
<tr>
<td>图片</td>
<td>
<img src="${item.pic}"/>
<input type="file" name="pic">
</td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td>点击提交即可</td>
</tr>
</table>
</form>
</body>
</html>

项目效果

  • 点赞
  • 收藏
  • 分享
  • 文章举报
邵涯语 发布了9 篇原创文章 · 获赞 4 · 访问量 494 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: