淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了。由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调整变更才能跑通项目。为了方便广大自学Java同仁的共同进步,我将持续更新这个网络实战项目练习的内容,着重指出符合当下开发环境下的内容勘误修订。

https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第三天】

02.第二天(框架整合,后台系统搭建)


    @Override
public User getUserById(Long id) {
// 方法一:适用于任何字段的查询
Example example = new Example(User.class);
example.createCriteria().andEqualTo("id", id);
List<User> list = userMapper.selectByExample(example);
if (list != null && list.size() > 0) {
return list.get(0);
} else {
return null;
}
// 方法二:只适用于主键字段
return userMapper.selectByPrimaryKey(id);
}

mybatis逆向工程的根据example对象查询的使用

一、Eclipse中运行提示报错:[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

09.整合测试-ok.avi

在按照视频中老师的讲解按部就班的操作下,Eclipse中运行提示报错:[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

这是因为我全新安装的eclipse&maven没有对编译jdk版本配置导致。修正方法在我的CSDN博客  https://blog.csdn.net/qq_40993412/article/details/99322387

二、maven 编译出错 Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean

eclipse在使用maven的tomcat7插件编译java程序时,报错 Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean (default-clean) on project **-web: Failed to clean project: Failed to delete X:\**\target\tomcat\logs\access_log

出现这种错误,通常是由于您已启动了另一个tomcat 进程或者运行的javaw.exe进程。控制台先关掉之前运行的那个tomcat进程,再重新操作就行了。

三、Mybatis的逆向工程。根据数据库表生成实体类的java代码。

File—>Import—>

generatorSqlmapCustom

因为该独立项目的依赖的jar包被我更新成别的文件版本,所以显示红色叹号提示,需要我们去Properties从新配置。

把红色小奶瓶的jar包依赖Remove后,点击Apply and Close 就能解决这个问题。

 

三、 taotao商城项目视频中老师所创建的web项目文件路径和附件中笔记、教案提供的参考源代码有出入导致的报错

org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'itemController':
Could not autowire field: private com.taotao.service.ItemService com.taotao.controller.ItemController.itemService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.taotao.service.ItemService] found for dependency:

说明原理的一个技术博客链接  https://blog.csdn.net/s740556472/article/details/54974074

四、log4j日志输出配置文件未设置导致的报错解决

在视频中,老师一直没提log4j的配置文件log4j.properties的设定,导致后面完全按照演示操作也会出现Eclipse提示报错信息:

log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly

01.参考资料文件夹里面有log4j.properties文件,我们需要把它复制到项目resources目录或将log4j.properties放到 \WEB-INF\classes文件夹中即可。

参考的技术博客:   关于控制台输出 警告 log4j:WARN No appenders could be found for logger

将src文件夹设置为source目录方法

五、由于配置加载db.properties路径错误导致的cannot be resolved to URL because it does not exist异常处理

参考的技术博客 https://blog.csdn.net/qinkang1993/article/details/57626434/

淘淘商城项目照着视频做出现这个报错,原因是老师提供的教案配置信息和视频演示有些小出入,需要我们自行修改.xml配置文件。

把下图中高亮的首个properties改成视频课中的实际文件路径resource问题解决。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=UTF-8&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root

六、阿里巴巴的druid数据库连接池配置文件 db.properties 要注意格式

2019年我在复盘这个taotao商城项目的时候,可以通过taotao-parent定义依赖的版本更新到

<druid.version>1.1.10</druid.version>

//北京时间东八区
serverTimezone=GMT%2B8
//或者使用上海时间
serverTimezone=Asia/Shanghai

在taotao-manager-web项目下的 src/main/resources 有spring文件夹,里面的applicationContext-dao.xml 定义了druid数据库连接池的配置信息。

 <!-- 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="5" />
</bean>

参考的技术博客  JDBC连接数据库 mysql serverTimezone useSSL 时差

DRUID连接池的实用 配置详解

七、在taotao-manager-web项目下的pom.xml里 ,配置tomcat插件的warSourceDirectory标签。

  <build>
<plugins>
<!-- 配置Tomcat插件的warSourceDirectory -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<warSourceDirectory>webapp</warSourceDirectory>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins> </build>

八、mybatis 整合spring之mapperLocations配置的问题

结论是:如果Mapper.xml与Mapper.class在同一个包下且同名,spring扫描Mapper.class的同时会自动扫描同名的Mapper.xml并装配到Mapper.class。

如果Mapper.xml与Mapper.class不在同一个包下或者不同名,就必须使用配置mapperLocations指定mapper.xml的位置。

九、分页测试给web项目pom.xml导入分页插件的依赖坐标不能用最新版而要用附件中老师修改过的特别版本3.4.2-fix

// taotao-parent中定义的版本不能用5.X.X因为会抛出异常
<pagehelper.version>3.4.2-fix</pagehelper.version>

十、修改taotao-manager-mapper的pom.xml文件中添加如下内容:

<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

Service层的实现类 根据商品ID查询方法:

    @Override
public TbItem getItemById(long itemId) {
//方法一、根据主键查询
//TbItem item = itemMapper.selectByPrimaryKey(itemId);
//方法二、添加查询条件,适用于任何字段的查询
//1.创建一个TbItemExample实例对象example
TbItemExample example = new TbItemExample();
//2.实例对象example.createCriteria()
Criteria criteria = example.createCriteria();
//3.criteria.andIdEqualTo()
criteria.andIdEqualTo(itemId);
//4.selectByExample(example)返回结果
List<TbItem> list = itemMapper.selectByExample(example); if (list != null && list.size() > 0) {
//从List集合中取出索引为0的对象
TbItem item = list.get(0);
return item;
}
return null;
}

http://localhost:8080/item/536563

11. Service层的查询所有并分页的代码

    @Override
public EUDataGridResult getItemList(int page, int rows) {
// 查询商品列表
TbItemExample example = new TbItemExample();
// 分页处理
PageHelper.startPage(page, rows);
List<TbItem> list = itemMapper.selectByExample(example);
// 创建一个返回值对象
EUDataGridResult result = new EUDataGridResult();
result.setRows(list);
// 取记录总条数
PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(list);
result.setTotal(pageInfo.getTotal());
return result; }

12.分页查询会报错,原因是淘淘项目mybatis的版本最高支持到3.2.8版本

    <!-- 集中定义依赖版本号 -->
<properties>
<junit.version>4.12</junit.version>
<spring.version>4.3.25.RELEASE</spring.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.3.3</mybatis.spring.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<pagehelper.version>3.4.2-fix</pagehelper.version>
<mysql.version>5.1.45</mysql.version>
<slf4j.version>1.7.25</slf4j.version>
<jackson.version>2.9.9.3</jackson.version>
<druid.version>1.1.10</druid.version>
<httpclient.version>4.5.6</httpclient.version>
<jstl.version>1.2</jstl.version>
<servlet-api.version>3.1.0</servlet-api.version>
<jsp-api.version>2.2</jsp-api.version>
<joda-time.version>2.9.9</joda-time.version>
<commons-lang3.version>3.9</commons-lang3.version>
<commons-io.version>2.6</commons-io.version>
<commons-net.version>3.6</commons-net.version>
<jsqlparser.version>0.9.1</jsqlparser.version>
<commons-fileupload.version>1.4</commons-fileupload.version>
<jedis.version>2.7.2</jedis.version>
<solrj.version>4.10.3</solrj.version>
</properties>

==============================

参考资料:

SpringBoot整合MyBatis逆向工程及 MyBatis通用Mapper实例详解

okey

第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】的更多相关文章

  1. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】

    本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...

  2. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  3. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  4. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  5. 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】

    淘淘商城(SpringMVC+Spring+Mybatis)  是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...

  6. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  7. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  8. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  9. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

随机推荐

  1. windows7配置C++编译环境

    将C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin添加到path后,依然不能编译: 新建INCLUDE:C:\Program Fil ...

  2. HDU 2586 LCA-Tarjan

    还是LCA-tarjan算法,跟POJ 1330做法基本类似,只是这个题目要求输出两个点的最短距离,其实利用LCA的性质,就是 两个点分别到最近公共祖先的距离之和 一开始本来想用并查集把路径长度给找出 ...

  3. 超级简单 一分钟实现react-native屏幕适配

    今天因为react-native的style只能给width和height设置数字 没有react上的vw和vh 因为之前经常用vh vw 感觉不适应 找到了一个新的方法 使用Demension模块 ...

  4. js面试之数组的几个不low操作

    1.扁平化n维数组 1.终极篇 [1,[2,3]].flat(2) //[1, 2, 3] [1,[2,3,[4,5]]].flat(3) //[1, 2, 3, 4, 5] [1,[2,3,[4,5 ...

  5. [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error [Errno 10061] Connection refused>

    [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: URLError: <urlopen error ...

  6. 前端基础之AJAX

    AJAX 什么是AJAX,简单来说就是利用JavaScript天生异步的特性,使用异步请求后台数据,从而达到不刷新网页也能局部更新页面的效果. 原生AJAX JavaScript中的AJAX依赖于XM ...

  7. 字符输出、if判断

    1.这里学习交互性输入 #input  接受的所有数据都是字符串,即使你输入的是数字,但依然会被当成字符串来处理 #type 用来查看变量存入到内存时的属性 #int 将变量强制转化为整型 #str  ...

  8. 基础语法-其它流程控制语句break和continue

    基础语法-其它流程控制语句break和continue 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.break语句 /** * break语句 * @author 尹正杰 * ...

  9. <老古董>1962年的线性支持向量机解法

    我们说“训练”支持向量机模型,其实就是确定"最大间隔超平面". 用数学语言来说就是确定一个最优的W.好比训练一个逻辑回归模型的目的是确定最优的W和b. 输入 X,为一个n维向量 输 ...

  10. Golang---序列化和反序列化

    为什么要序列化和反序列化 我们的数据对象要在网络中传输或保存到文件,就需要对其编码和解码动作,目前存在很多编码格式:json, XML, Gob, Google Protocol Buffer 等, ...