zoukankan      html  css  js  c++  java
  • 第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

                                     将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数据库连接池的配置信息。

     1 <!-- 数据库连接池 -->
     2     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
     3         destroy-method="close">
     4         <property name="url" value="${jdbc.url}" />
     5         <property name="username" value="${jdbc.username}" />
     6         <property name="password" value="${jdbc.password}" />
     7         <property name="driverClassName" value="${jdbc.driver}" />
     8         <property name="maxActive" value="20" />
     9         <property name="initialSize" value="1" />
    10         <property name="maxWait" value="60000" />
    11         <property name="minIdle" value="5" />
    12     </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

    部分内容来自于学习编程期间收集于网络的免费分享资源和工作后购买的付费内容。 如需获取教程配套的资源文件和一对一专属答疑支持,请加vx:kangmf24联系作者。
  • 相关阅读:
    css-深入理解margin和padding
    js的自定义事件
    jcFlexible.js的小Demo
    volatile关键字回顾
    threadLocalMap理解
    常用SQL笔记
    MyISAM和innoDB对比,覆盖索引简单回顾
    经典算法回顾:两个队列生成一个栈,两个栈生成一个队列
    Memcached与Redis对比,Redis基础笔记回顾
    ES(ElasticSearch)学习总结
  • 原文地址:https://www.cnblogs.com/MarlonKang/p/11342081.html
Copyright © 2011-2022 走看看