继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

SpringBoot电商项目-3.3&3.4

zhouKyou
关注TA
已关注
手记 1
粉丝 3
获赞 1

3.3项目初始化-生成逆向文件

主要讲了如何使用 mybatis-generator 插件实现,根据数据库,自动生成对应的

  • ①dao层 mapper文件 

  • ②pojo层数据表对应的实体类文件


其中在pom加入的依赖是

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency

作用为加入springboot启动器???

和数据库链接jar包???


需要加入的mybatis-generator插件为

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

配合上mybatis-generator的配置文件【generatorConfig.xml】和

数据库链接jar包【mysql-connector-java-8.0.18.jar】,达到自动生成dao和pojo的效果

其中特别需要注意的是

  • ①mysql-connector-java-8.0.18.jar存放的位置

  • ②数据库链接字符串,最后要加上时区&amp;serverTimezone=Asia/Shanghai

  • ③更改数据库链接的账号和密码

  • ④dao和pojo自动生成时要存放的位置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <!-- 配置文件,放在resource目录下即可 -->
  <!--数据库驱动个人配置-->
  <classPathEntry
    location="/C:/Users/周游/IdeaProjects/test2/src/main/resources/mysql-connector-java-8.0.18.jar"/>
  <context id="MysqlTables" targetRuntime="MyBatis3">
    <property name="autoDelimitKeywords" value="true"/>
    <!--可以使用``包括字段名,避免字段名与sql保留字冲突报错-->
    <property name="beginningDelimiter" value="`"/>
    <property name="endingDelimiter" value="`"/>
    <!-- optional,旨在创建class时,对注释进行控制 -->
    <commentGenerator>
      <property name="suppressDate" value="true"/>
      <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--数据库链接地址账号密码-->
    <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
      connectionURL="jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull&amp;serverTimezone=Asia/Shanghai"
      userId="root"
      password="root">
      <property name="nullCatalogMeansCurrent" value="true"/>
    </jdbcConnection>
    <!-- 非必需,类型处理器,在数据库类型和java类型之间的转换控制-->
    <javaTypeResolver>
      <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!--生成Model类存放位置-->
    <javaModelGenerator targetPackage="com.example.model.pojo"
      targetProject="src/main/java">
      <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
      <property name="enableSubPackages" value="true"/>
      <!-- 是否对类CHAR类型的列的数据进行trim操作 -->
      <property name="trimStrings" value="true"/>
      <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
      <property name="immutable" value="false"/>
    </javaModelGenerator>
    <!--生成mapper映射文件存放位置-->
    <sqlMapGenerator targetPackage="mappers" targetProject="src/main/resources">
      <property name="enableSubPackages" value="true"/>
    </sqlMapGenerator>
    <!--生成Dao类存放位置-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.model.dao"
      targetProject="src/main/java">
      <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>
    <!--生成对应表及类名-->
    <table schema="root" tableName="imooc_mall_cart" domainObjectName="Cart"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_category" domainObjectName="Category" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order" domainObjectName="Order" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_order_item" domainObjectName="OrderItem"
      enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_product" domainObjectName="Product" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>
    <table tableName="imooc_mall_user" domainObjectName="User" enableCountByExample="false"
      enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
      selectByExampleQueryId="false">
    </table>

  </context>
</generatorConfiguration>


完成了pom依赖和插件的引入,和generatorConfig.xml配置文件的更改后,在右侧maven的plugins中找到mybatis-generator双击查看是否会自动生成dao和pojo

3.4打通数据库链路

主要讲了在逆向生成dao和pojo的基础上,创建controller和service

实现从浏览器访问到数据库数据


在application.properties中配置数据库链接信息

指出要扫描的mapper.xml文件的位置

server.port=8083

spring.datasource.name=imooc_mall_datasource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/imooc_mall?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

mybatis.mapper-locations=classpath:mappers/*.xml


创建controller层,

①controller层要记得加注解@Controller,需要自动导入service的数据要加@Autowired

②创建的接口如果使用get方式访问记得使用@GetMapping("/xxoo")标注接口的地址

③这里再加上@ResponseBody将返回的对象转化为json格式,再传递给浏览器


创建service层

service层分俩部分,一部分是作为接口的service,还有一部分是接口的实现类serviceImpl

serviceImpl要加@Service注解


最后要注意在启动类Application中加上@MapperScan(basePackages="xxoo")来说明要扫码的mapper文件位置都在哪儿

@MapperScan(basePackages = "com.example.test3.model.dao")


补充,解决serviceImpl的红色误报问题,解决办法是在自动生成的mapper接口上加上@Repository标签来向idea说明这是个资源


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP

热门评论

nice , 不愧是我!

nice , 不愧是我!

查看全部评论