第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第二天】
淘淘商城(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
五、由于配置加载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>
七、在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) 的学习实践总结【第二天】的更多相关文章
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第一天】
本人做过一年的MATLAB编程和简单维护过VB和C++的项目.是跟着网上获得的黑马的Java双元视频课来自学入门Java知识和常用框架的使用. 淘淘商城(SpringMVC+Spring+Mybati ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第五天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第四天】
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMvc+Spring+Mybatis) 的学习实践总结【第三天】
淘淘商城(SpringMVC+Spring+Mybatis) 是传智播客在2015年9月份录制的,几年过去了.由于视频里课上老师敲的代码和项目笔记有些细节上存在出入,只有根据日志报错信息作出适当的调 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第十天】(单点登录系统实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第九天】(商品详情页面实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第八天】(solr服务器搭建、搜索功能实现)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
- 第04项目:淘淘商城(SpringMVC+Spring+Mybatis)【第七天】(redis缓存)
https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...
随机推荐
- mitmproxy(TLS错误)
一.原来的基础上添加代码 """ This inline script allows conditional TLS Interception based on a us ...
- 056-for循环中continue的使用
<?php ; $i <= ; $i++) { //for循环输出数值 == ) //判断变量是否为3的整数倍 { continue;//跳过本次循环剩余语句 } echo "$ ...
- 浅谈Python 中 __getattr__与__getattribute__的区别
__getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getatt ...
- es6 part 1 //const let
1. let 命令 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } a // ...
- UVA - 10384 The Wall Pusher(推门游戏)(IDA*)
题意:从起点出发,可向东南西北4个方向走,如果前面没有墙则可走:如果前面只有一堵墙,则可将墙向前推一格,其余情况不可推动,且不能推动游戏区域边界上的墙.问走出迷宫的最少步数,输出任意一个移动序列. 分 ...
- BZOJ:2242: [SDOI2011]计算器
题解:BSGS 问题:map空间 BSGS判无解 a%p!=0 0与最小非负整数 有区别 函数传参类型转换int->long long long long ->int; 费马小定理充分必要 ...
- 04-String——课后作业1:字串加密
题目:请编写一个程序,加密或解密用户输入的英文字串要求设计思想.程序流程图.源代码.结果截图. 程序设计思想:首先由用户选择是加密还是解密,利用String类中的charAt函数依次取出字串中的字符, ...
- 关于ThinkCMF后台验证码不显示
最近小凯在群里看到好多同学们遇到一个头疼的问题,程序在本地调试好了之后上传服务器,后台的验证码就会不显示,今天三群的 [江苏 冰点零度 php] 同学遇到了这个问题. 开始小凯以为是GB库出来问题 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 变量类型
变量其实只不过是程序可操作的存储区的名称.C++ 中每个变量都有指定的类型,类型决定了变量存储的大小和布局,该范围内的值都可以存储在内存中,运算符可应用于变量上. 变量的名称可以由字母.数字和下划线字 ...
- Flink与Spark Streaming在与kafka结合的区别!
本文主要是想聊聊flink与kafka结合.当然,单纯的介绍flink与kafka的结合呢,比较单调,也没有可对比性,所以的准备顺便帮大家简单回顾一下Spark Streaming与kafka的结合. ...