Java之SSH项目:网络商城-项目+源代码 day02(服务中间件Dubbo连接方式,MyBatis,Mapper

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

d、dao当中定义包

在这里插入图片描述

e、dao当中定义资源文件bd.properties

在这里插入图片描述

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8

jdbc.username=root

jdbc.password=root

五、service整合


1、创建配置文件

a、applicationContext-service.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:dubbo=“http://code.alibabatech.com/schema/dubbo” 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.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

创建包结构taotao-manager-interface当中创建com.taotao.service

在这里插入图片描述

b、applicationContext-transaction.xml

在这里插入图片描述

<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/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

<bean id=“transactionManager”

class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”>

<tx:advice id=“txAdvice” transaction-manager=“transactionManager”>

tx:attributes

<tx:method name=“save*” propagation=“REQUIRED” />

<tx:method name=“insert*” propagation=“REQUIRED” />

<tx:method name=“add*” propagation=“REQUIRED” />

<tx:method name=“create*” propagation=“REQUIRED” />

<tx:method name=“delete*” propagation=“REQUIRED” />

<tx:method name=“update*” propagation=“REQUIRED” />

<tx:method name=“find*” propagation=“SUPPORTS” read-only=“true” />

<tx:method name=“select*” propagation=“SUPPORTS” read-only=“true” />

<tx:method name=“get*” propagation=“SUPPORTS” read-only=“true” />

</tx:attributes>

</tx:advice>

aop:config

<aop:advisor advice-ref=“txAdvice”

pointcut=“execution(* com.taotao.service..(…))” />

</aop:config>

c、配置web.xml

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns=“http://java.sun.com/xml/ns/javaee”

xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd”

version=“2.5”>

taotao-manager-service

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

contextConfigLocation

classpath:spring/applicationContext-*.xml

org.springframework.web.context.ContextLoaderListener

六、表现层搭建


1、相关配置文件

在这里插入图片描述

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:p=“http://www.springframework.org/schema/p”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:component-scan

base-package=“com.taotao.controller” />

<mvc:annotation-driven />

<bean

class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>

创建对应的包结构

在这里插入图片描述

创建对应的jsp文件夹

在这里插入图片描述

在这里插入图片描述

在WEB-INF当中配置Servlet

2、dao层创建对应的接口对应的映射文件

a、创建对应的接口

在这里插入图片描述

package com.taotao.mapper;

/*

  • 测试接口查询当前的时间

*/

public interface TestMapper {

public String queryNow();

}

b、创建对应的接口的映射文件TestMapper.xml

在这里插入图片描述

<?xml version="1.0" encoding="UTF-8" ?>

select NOW()

3、interface层创建对应的接口对应的映射文件

在这里插入图片描述

package com.taotao.service;

/*

  • 测试接口查询当前的时间

*/

public interface TestService {

public String queryNow();

}

4、service层TestServiceImpl实现TestService接口

在这里插入图片描述

package com.taotao.service.impl;

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

import org.springframework.stereotype.Service;

import com.taotao.mapper.TestMapper;

import com.taotao.service.TestService;

@Service

public class TestServiceImpl implements TestService {

@Autowired

private TestMapper mapper;

@Override

public String queryNow() {

// 1.注入mapper

// 2、调用mapper方法返回

return mapper.queryNow();

}

}

七、发布服务


1、配置dubbo

在这里插入图片描述

<dubbo:application name=“taotao-manager” />

<dubbo:registry protocol=“zookeeper” address=“192.168.25.128:2181” />

<dubbo:protocol name=“dubbo” port=“20880” />

<dubbo:service interface=“com.taotao.service.TestService” ref=“testServiceImpl” />

2、applicationContext-service.xml

在这里插入图片描述

<dubbo:application name=“taotao-manager” />

<dubbo:registry protocol=“zookeeper” address=“115.159.79.135:2181” />

<dubbo:protocol name=“dubbo” port=“20880” />

<dubbo:service interface=“com.taotao.service.TestService” ref=“testServiceImpl” />

3、创建控制层相关内容

a、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:p=“http://www.springframework.org/schema/p”

xmlns:context=“http://www.springframework.org/schema/context”

xmlns:dubbo=“http://code.alibabatech.com/schema/dubbo”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<context:component-scan

base-package=“com.taotao.controller” />

<mvc:annotation-driven />

<bean

class=“org.springframework.web.servlet.view.InternalResourceViewResolver”>

<dubbo:application name=“taotao-manager-web”/>

<dubbo:registry protocol=“zookeeper” address=“192.168.25.128:2181”/>

<dubbo:reference interface=“com.taotao.service.TestService” id=“testService” />

b、创建TestController

在这里插入图片描述

在这里插入图片描述

启动服务的消费者

在这里插入图片描述

访问项目

http://localhost:8081/test/queryNow

在这里插入图片描述

运行报错

在这里插入图片描述

没有找到对应的类

在这里插入图片描述

在taotao-manager-dao\pom.xml当中配置一个插件

在这里插入图片描述

src/main/java

**/*.properties

**/*.xml

false

改好后需要选择taotao-manager-dao工程执行

run as maven install

在这里插入图片描述

八、商品列表查询


(一)显示后台页面

1、引入逆向工程

逆向工程下载地址和页面下载:

链接: https://pan.baidu.com/s/1W2s0mNdEr5G-D-_WwmSztw 提取码: 3y73

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2、运行逆向工程

在这里插入图片描述

生成对应的文件

在这里插入图片描述

在这里插入图片描述

3、将生成的内容拷贝到当前自己的工程当中

在这里插入图片描述

在这里插入图片描述

4、引入页面

放置在WEB-INF当中

页面代码下载地址:

链接: https://pan.baidu.com/s/1BuzcPiNd0E1_urtmHCvJjw 提取码: 18u4

在这里插入图片描述

5、资源映射的配置

由于在web.xml中定义的url拦截形式为“/”表示拦截所有的url请求,包括静态资源例如css、js等。

所以需要在springmvc.xml中添加资源映射标签:

在这里插入图片描述

<mvc:resources location=“/WEB-INF/js/” mapping=“/js/**”/>

<mvc:resources location=“/WEB-INF/css/” mapping=“/css/**”/>

6、创建控制层PageController

在这里插入图片描述

package com.taotao.controller;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

/*

  • 显示页面

*/

@Controller

public class PageController {

@RequestMapping(“/”)

public String showIndex() {

return “index”;

}

}

在这里插入图片描述

删除创建web的时候自动生成的index.jsp

在这里插入图片描述

7、运行项目

在这里插入图片描述

8、访问项目

http://localhost:8081/

在这里插入图片描述

(二)显示商品列表

1、在PageController当中添加showPage方法

在这里插入图片描述

//显示商品的查询的页面

//url:item-list

@RequestMapping(“/{page}”)

public String showPage(@PathVariable String page) {//@PathVariable表示从URL拦截

return page;

}

2、再次运行项目

http://localhost:8081/

在这里插入图片描述

在这里插入图片描述

3、使用PageHelper实现分页功能

下载地址链接: https://pan.baidu.com/s/1BwYokfOKugVm3mpuz4-q8w 提取码: pbme

引入插件源代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

将导入的工程安装到本地,形式自己的maven工程

在这里插入图片描述

dao当中引入

在这里插入图片描述

4、使用PageHelper在Mybatis的全局文件中配置SqlMapConfig.xml中配置拦截器插件:

在这里插入图片描述

5、设置分页信息

编写测试代码

在这里插入图片描述

创建对应的类

在这里插入图片描述

在这里插入图片描述

package com.taotao.test.pagehelp;

import java.util.List;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;

import com.taotao.mapper.TbItemMapper;

import com.taotao.pojo.TbItem;

import com.taotao.pojo.TbItemExample;

public class TestPageHelper {

@Test

public void testhelper() {

//2、初始化spring容器

ApplicationContext context = new ClassPathXmlApplicationContext(“classpath:spring/applicationContext-dao.xml”);

//3、获取mapper的代理对象

TbItemMapper itemMapper = context.getBean(TbItemMapper.class);

//1、设置分页信息

PageHelper.startPage(1, 3);//3行 紧跟着的第一个查询才会被分页

//4、调用mapper符方法查询数据

TbItemExample example = new TbItemExample();//设置查询条件使用

List list = itemMapper.selectByExample(example);//select * from tb_item;

List list2 = itemMapper.selectByExample(example);//select * from tb_item;

//取分页信息

PageInfo info = new PageInfo(list);

System.out.println(“第一个分页的list的集合的长度:”+list.size());

//for (TbItem tbItem : list) {

// System.out.println(tbItem.getId()+"名称 " + tbItem.getTitle());

//}

System.out.println(“第二个分页的list的集合的长度:”+list2.size());

//5、遍历结果集 打印

System.out.println(“查询的总记录数:”+info.getTotal());

//for (TbItem tbItem : list2) {

// System.out.println(tbItem.getId()+"名称 " + tbItem.getTitle());

//}

}

}

运行结果

在这里插入图片描述

(三)Service

1、参数:int page ,int rows,业务逻辑:查询所有商品列表,要进行分页处理。返回值:EasyUIDataGridResult

在这里插入图片描述

在这里插入图片描述

package com.taotao.common.pojo;

import java.io.Serializable;

import java.util.List;

/*

  • datagrid展示数据的POJO,包括商品的POJO

*/

public class EasyUIDataGridResult implements Serializable {

private Integer total;

private List rows;

public EasyUIDataGridResult() {

}

public EasyUIDataGridResult(Integer total, List rows) {

super();

this.total = total;

this.rows = rows;

}

public Integer getTotal() {

return total;

}

public void setTotal(Integer total) {

this.total = total;

}

public List getRows() {

return rows;

}

public void setRows(List rows) {

this.rows = rows;

}

@Override

public String toString() {

return “EasyUIDataGridResult [total=” + total + “, rows=” + rows + “]”;

}

}

修改TbItem类实现序列化接口

在这里插入图片描述

在这里插入图片描述

2、service开发定义接口

a、创建接口ItemService接口

在这里插入图片描述

package com.taotao.service.impl;

import com.taotao.common.pojo.EasyUIDataGridResult;

/*

  • 商品相关的处理的Service

*/

public interface ItemService {

/*

总结

面试建议是,一定要自信,敢于表达,面试的时候我们对知识的掌握有时候很难面面俱到,把自己的思路说出来,而不是直接告诉面试官自己不懂,这也是可以加分的。

以上就是蚂蚁技术四面和HR面试题目,以下最新总结的最全,范围包含最全MySQL、Spring、Redis、JVM等最全面试题和答案,仅用于参考

一份还热乎的蚂蚁金服面经(已拿Offer)面试流程4轮技术面+1轮HR

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzU3MDM0,size_16,color_FFFFFF,t_70)

在这里插入图片描述

2、service开发定义接口

a、创建接口ItemService接口

在这里插入图片描述

package com.taotao.service.impl;

import com.taotao.common.pojo.EasyUIDataGridResult;

/*

  • 商品相关的处理的Service

*/

public interface ItemService {

/*

总结

面试建议是,一定要自信,敢于表达,面试的时候我们对知识的掌握有时候很难面面俱到,把自己的思路说出来,而不是直接告诉面试官自己不懂,这也是可以加分的。

以上就是蚂蚁技术四面和HR面试题目,以下最新总结的最全,范围包含最全MySQL、Spring、Redis、JVM等最全面试题和答案,仅用于参考

[外链图片转存中…(img-A6ZEaIu1-1713614814460)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-hglWoe8W-1713614814461)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值