电商项目-配置文件

application-prod.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: E94Z3M74kkKA
    url: jdbc:mysql://rm-uf6lgnfx2zmto0546.mysql.rds.aliyuncs.com:3306/mall?characterEncoding=utf-8&useSSL=false
  redis:
    host: 127.0.0.1
    port: 6379
  rabbitmq:
    addresses: 127.0.0.1
    port: 5672
    username: guest
    password: guest
mybatis:
  configuration:
    map-underscore-to-camel-case: true
    # 控制台日志配置
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mappers/*.xml
logging:
  pattern:
    console: "[%thread] %-5level %logger{36} - %msg%n"
server:
  servlet:
    session:
      timeout: 120

这段配置信息是用于Spring Boot应用程序的,它配置了数据源、Redis、RabbitMQ、MyBatis以及日志和服务器会话的相关设置。下面是对这些配置项的简单解析:

数据源(DataSource)

  • spring.datasource.driver-class-name: 指定数据库驱动类名,这里是MySQL的连接驱动。
  • spring.datasource.usernamespring.datasource.password: 分别是数据库的用户名和密码。
  • spring.datasource.url: 数据库的URL,包括主机地址、端口号、数据库名以及连接参数(如字符编码和SSL设置)。例子中使用的是阿里云RDS上的MySQL服务。

Redis

  • spring.redis.hostspring.redis.port: 设置Redis服务器的地址和端口。这里配置的是本地默认的Redis服务。

RabbitMQ

  • spring.rabbitmq.addresses, spring.rabbitmq.port, spring.rabbitmq.username, spring.rabbitmq.password: 分别配置了RabbitMQ服务器的地址、端口、用户名和密码。此例中也是配置的本地环境。

MyBatis

  • mybatis.configuration.map-underscore-to-camel-case: 设为true表示开启下划线到驼峰命名的自动转换,适用于字段名与Java属性名之间的映射。
  • mybatis.configuration.log-impl: 指定MyBatis使用的日志实现类,这里配置的是输出到控制台的日志实现。
  • mybatis.mapper-locations: 指定MyBatis映射文件的位置,这里是classpath下的mappers目录中的所有.xml文件。

日志(Logging)

  • logging.pattern.console: 定义控制台日志输出的格式,包括线程名、日志级别、logger名称及消息内容。

服务器(Server)

  • server.servlet.session.timeout: 会话超时时间设置,单位是分钟,在这个例子中设置为120分钟。

这些配置帮助Spring Boot应用连接到数据库、缓存、消息队列,并调整了ORM映射、日志记录和会话管理的行为。请确保根据你的实际部署环境调整相应的配置值,特别是数据库、Redis、RabbitMQ的地址和凭证。

generatorConfig.xml

<?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>
<!--    windows下路径, D:\downloads\xxx.jar-->
    <classPathEntry location="/Users/admin/Desktop/mysql-connector-java-5.1.6.jar" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

<!--        不再追加xml内容-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />

        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/mall?characterEncoding=utf-8"
                        userId="root"
                        password="123456">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.imooc.mall.pojo" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
<!--            <property name="trimStrings" value="true" />-->
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mappers"  targetProject="src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.imooc.mall.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

<!--        <table tableName="mall_order" domainObjectName="Order" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_user" domainObjectName="User" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_category" domainObjectName="Category" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>-->
<!--        <table tableName="mall_product" domainObjectName="Product" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">-->
<!--            <columnOverride column="detail" jdbcType="VARCHAR" />-->
<!--            <columnOverride column="sub_images" jdbcType="VARCHAR" />-->
<!--        </table>-->
        <table tableName="mall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>

    </context>
</generatorConfiguration>

这是一个MyBatis Generator的配置文件,主要用于自动生成MyBatis的Java模型对象、SQL映射文件及Mapper接口等。以下是该配置文件的详细解析:

  1. XML声明与DOCTYPE定义

    • 声明了XML文件的版本为1.0,编码为UTF-8。
    • 引入了MyBatis Generator配置的DTD定义,确保配置文件格式正确。
  2. <classPathEntry>

    • 指定了MyBatis Generator需要的类路径入口,这里是MySQL的JDBC驱动包位置。注意路径应根据实际情况进行修改,示例中为Mac或Linux系统的路径。
  3. <context>

    • 定义了一个生成上下文,ID为"DB2Tables",指定目标运行时为MyBatis3。
    • 包含了一系列的配置来指导生成过程。
  4. 插件配置

    • <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />:配置了一个插件,用于防止生成的XML映射文件被合并,保持每次生成的独立性。
  5. <commentGenerator>

    • 配置了注释生成器,其中<property name="suppressAllComments" value="true"/>表示抑制所有自动生成的注释。
  6. <jdbcConnection>

    • 定义了数据库连接信息,包括驱动类名、数据库URL、用户名和密码。
  7. <javaTypeResolver>

    • 配置了Java类型解析器,<property name="forceBigDecimals" value="false"/>指示不强制将所有的数值类型映射为java.math.BigDecimal
  8. 代码生成器配置

    • <javaModelGenerator>:用于生成实体类,指定了目标包和项目路径。
    • <sqlMapGenerator>:生成SQL映射文件的配置,同样指定了目标包和路径。
    • <javaClientGenerator>:生成Mapper接口和映射文件的配置,类型设为"XMLMAPPER",并指定了目标包和路径。
  9. <table>元素

    • 虽然大部分表的配置被注释掉了,但以<table tableName="mall_shipping">为例,它指定了一个待生成的表名及其对应的Java领域对象名,并通过一系列enableXXXByExample属性关闭了基于Example的方法生成。

这个配置文件展示了如何通过MyBatis Generator来自动生成MyBatis框架所需的一系列代码文件,极大地简化了开发工作。根据实际需求,可以调整数据库连接、生成路径、是否生成特定方法等配置。

UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.mall.dao.UserMapper">
  <resultMap id="BaseResultMap" type="com.imooc.mall.pojo.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="question" jdbcType="VARCHAR" property="question" />
    <result column="answer" jdbcType="VARCHAR" property="answer" />
    <result column="role" jdbcType="INTEGER" property="role" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id, username, password, email, phone, question, answer, role, create_time, update_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from mall_user
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from mall_user
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.imooc.mall.pojo.User">
    insert into mall_user (id, username, password, 
      email, phone, question, 
      answer, role, create_time, 
      update_time)
    values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR}, 
      #{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="insertSelective" parameterType="com.imooc.mall.pojo.User">
    insert into mall_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="username != null">
        username,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="question != null">
        question,
      </if>
      <if test="answer != null">
        answer,
      </if>
      <if test="role != null">
        role,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="username != null">
        #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="question != null">
        #{question,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        #{answer,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        #{role,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.imooc.mall.pojo.User">
    update mall_user
    <set>
      <if test="username != null">
        username = #{username,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="question != null">
        question = #{question,jdbcType=VARCHAR},
      </if>
      <if test="answer != null">
        answer = #{answer,jdbcType=VARCHAR},
      </if>
      <if test="role != null">
        role = #{role,jdbcType=INTEGER},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.imooc.mall.pojo.User">
    update mall_user
    set username = #{username,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      email = #{email,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      question = #{question,jdbcType=VARCHAR},
      answer = #{answer,jdbcType=VARCHAR},
      role = #{role,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = #{updateTime,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="countByUsername" parameterType="java.lang.String" resultType="java.lang.Integer">
    select
    count(1)
    from mall_user
    where username = #{username,jdbcType=VARCHAR}
  </select>

  <select id="countByEmail" parameterType="java.lang.String" resultType="java.lang.Integer">
    select
    count(1)
    from mall_user
    where email = #{email,jdbcType=VARCHAR}
  </select>

  <select id="selectByUsername" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from mall_user
    where username = #{username,jdbcType=VARCHAR}
  </select>
</mapper>

这段代码展示的是一个MyBatis的Mapper配置文件,主要负责定义针对com.imooc.mall.dao.UserMapper接口的操作,包括查询、插入、更新和删除用户数据的方法。下面是各个部分的简要说明:

  1. 命名空间(namespace):

    • namespace="com.imooc.mall.dao.UserMapper"定义了此Mapper文件对应Java接口的全限定名。
  2. 结果映射(resultMap):

    • <resultMap id="BaseResultMap" type="com.imooc.mall.pojo.User">定义了从数据库结果集到Java对象User的映射规则,包括每个字段的列名(column)、JDBC类型(jdbcType)和对象属性(property)。
  3. SQL片段(Base_Column_List):

    • <sql id="Base_Column_List">...定义了一个可重用的SQL片段,列出所有需要查询的列名,方便在多个SQL语句中复用。
  4. 查询方法(select):

    • selectByPrimaryKey, countByUsername, countByEmail, 和 selectByUsername分别根据主键、用户名计数、邮箱计数和用户名获取用户详情。
  5. 插入方法(insert):

    • insertinsertSelective提供了两种插入方式,前者插入所有字段,后者仅插入非空字段。
  6. 更新方法(update):

    • updateByPrimaryKeySelectiveupdateByPrimaryKey分别按主键更新部分字段(非空字段)和全部字段。
  7. 删除方法(delete):

    • deleteByPrimaryKey按主键删除用户记录。

每个操作都使用了MyBatis动态SQL标签,如<if>用于条件判断,<include>用于包含SQL片段,以及<trim>用于动态构造INSERT和UPDATE语句中的列名和值列表,这些都提高了SQL的灵活性和代码的可维护性。此外,通过#{}语法引用参数,确保了SQL注入的安全防护。

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值