首页 > 其他分享 >maven高级21_基于maven构建SSM工程3

maven高级21_基于maven构建SSM工程3

时间:2024-03-15 17:25:34浏览次数:19  
标签:SSM 21 private id maven item 编写 public

一、需求描述

本案例基于maven构建 SSM(Spring+SpringMVC+Mybatis)工程,通过maven坐标进行依赖管理。最终实现根据 id 查询商品信息的功能。

二、实现步骤分析

 1、数据库环境搭建

​ ①创建数据库ssmtest

 

​ ②创建商品表item

CREATE TABLE `item` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `price` float default NULL,
  `createtime` datetime default NULL,
  `detail` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

 

 2、maven项目构建

①创建maven web项目

②配置pom.xml文件

③实现spring+mybatis整合

创建POJO类

public class Item {
private Integer id;
private String name;
private Float price;
private Date createtime;
private String detail;
//省略setter、getter
}

持久层DAO接口编写

public interface ItemMapper {
       public Item findById(int id);
}

Mapper映射文件编写

<?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.itheima.ssm.dao.ItemMapper">
<select id="findById" parameterType="int" resultType="item">
         select * from item where id=#{id}</select>
</mapper>

业务层Service编写

package com.itheima.ssm.service;
import com.itheima.ssm.pojo.Item;
public interface ItemService {
    public Items findById(int id);
} 

 

@Service
@Transactional
public class ItemServiceImpl implements ItemService {
@Autowired
private ItemMapper itemMapper;
public Item findById(int id) {
return itemMapper.findById(id);
}
}

spring配置文件applicationContext-dao.xml编写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/bean       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd      http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<!-- url -->
<property name="url" value="jdbc:mysql://localhost:3306/ssmtest"/>
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/></bean>
<!-- mapper配置 --> <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!--为指定包下的所有实体类创建别名-->
<property name="typeAliasesPackage" value="com.itheima.ssm.pojo"/></bean>
<!-- mapper扫描器 :用来产生代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.ssm.dao"></property>  
</bean>
</beans>

spring配置文件applicationContext-service.xml编写

 

 


④加入springmvc相关配置

表现层Controller编写

@Controller
@RequestMapping("/item")
public class ItemController {
   @Autowired
   private ItemService itemService;
  @RequestMapping("/showItem/{id}")
  public String showItem(@PathVariable("id") int id, Model model){
        Item item = itemService.findById(id);
        model.addAttribute("item",item);
        return "item";   
  }
}

springmvc.xml文件编写

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd">    <context:component-scan base-package="com.itheima.ssm.controller"/>
<!--  配置视图解析器的前缀和后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">           <property name="prefix“ value="/WEB-INF/jsp/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
</beans>

jsp页面编写

配置web.xml文件

 3、数据库环境搭建

 

标签:SSM,21,private,id,maven,item,编写,public
From: https://www.cnblogs.com/ajing2018/p/18075847

相关文章

  • maven高级21_maven的依赖冲突2
    一、什么是maven的依赖传递在maven中,依赖是可以传递的,假设存在三个项目,分别是项目A,项目B以及项目C。假设C依赖B,B依赖A,那么我们可以根据maven项目依赖的特征不难推出项目C也依赖A。 通过上面的图可以看到,我们的web项目直接依赖了spring-webmvc,而spring-webmvc依赖了......
  • Linux 安装IntelAx211无线网卡
    https://blog.csdn.net/shikaiaixuexi/article/details/131565396sudoaptinstallgitmakesudoaptinstallflexbisongitclonehttps://git.kernel.org/pub/scm/linux/kernel/git/iwlwifi/backport-iwlwifi.gitcdbackport-iwlwifisudomakedefconfig-iwlwifi-......
  • 代码随想录算法训练营第四十七天| ● 198.打家劫舍 ● 213.打家劫舍II ● 337.打家
    打家劫舍 题目链接:198.打家劫舍-力扣(LeetCode)思路:每一家的最大收益来源只有两个,一个是这家不偷,那么最大收益等于从上一家出来的最大收益,另一个是偷这一个家,因此最大收益等于从上上一家出来的最大收益加这一家的收益。classSolution{public:introb(vector<int>&nu......
  • SAP FI模块PA认证模拟题-中英文对译(C_TS4FI_2021最新版)
    NO.1/95AssetAccounting资产会计Whichofthefollowingarevalidsettlementreceiverswhenyouperformsettlementforanassetunderconstructiononalineitembasis?当您按行项目对在建资产进行结算时,以下哪项是有效的结算接收方?Note:Thereare2correct......
  • 代码随想录算法训练营第四十七天 | 337.打家劫舍III,213.打家劫舍II ,198.打家劫舍
     198.打家劫舍 已解答中等 相关标签相关企业 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一......
  • 21_迭代器模式
    迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不需要暴露集合对象的内部结构。迭代器模式将遍历操作封装在迭代器对象中,使得客户端可以通过迭代器对象依次访问集合中的元素。迭代器模式有三个主要角色:迭代器(Iterator):定义了访问和遍历集合对......
  • 稳定可靠:PW2163降压芯片,实现5V至3.3V/3V高效转换,3A电流稳定输出
    在现代电子设备中,电源管理芯片发挥着至关重要的作用。PW2163作为一款高效稳定的500kHz同步降压DC-DC转换器,凭借其出色的性能和广泛的应用领域,已成为众多电子设备中的电源管理新选择。 一、PW2163的显著特点与优势PW2163具有内部集成低RDS(ON)的主开关和同步开关,这一设计有助于最......
  • 授之以渔不是授之以鱼-docker maven 构建java工程
    原始的maven构建语句mvncleaninstall-Dmaven.test.skip=true现在需要在一台新的安装有docker的机子上构建,要求用jdk17和maven3.9,这台机子较旧,只有jdk8和jdk11,但是有安装docker,在不动构建机器现有软件的情况下构建。dockerrun\--rm\-v$(pwd):$(pwd)......
  • 常见问题解决 --- idea与maven使用常识
    1.拿到项目代码后先要知道使用了哪些技术和工具。比如使用的是idea、eclipse还是maven创建的项目,使用什么编程语言,使用什么项目目录结构等等2.如何用maven创建的项目必然有pom.xml,每次修改pom文件后必须重新加载。3.如果修改代码后还是报错,尝试使用clean清除编译缓存再同步maven......
  • Maven中optional标签详解(转)
    原文:https://blog.csdn.net/weixin_43888891/article/details/130510971作者:怪咖@来源:CSDN 一、前言<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.18</version><optio......