当前位置: 首页 > news >正文

10 mybatis 日志

文章目录

    • product.sql
    • pom.xml
    • logback.xml
    • mybatis-config.xml
    • ProductsMapper.xml
    • Products
    • ProductsMapper.java

product.sql


create table products
(product_id       int auto_increment comment '产品ID'primary key,product_name     varchar(100)   null comment '产品名称',brand            varchar(50)    null comment '品牌',price            decimal(10, 2) null comment '价格',color            varchar(20)    null comment '颜色',storage_capacity varchar(10)    null comment '存储容量',description      text           null comment '描述'
)comment '手机产品表';

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.aistat</groupId><artifactId>mybatis_tech</artifactId><version>1.0-SNAPSHOT</version><!--打包类型--><packaging>jar</packaging><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><!--所有依赖--><dependencies><!--        MySQL驱动--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><version>8.0.31</version></dependency><!--        mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.15</version></dependency><!--测试环境--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
<!--Lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.14.8</version><scope>provided</scope></dependency><!--        logback日志 slf规范-->
<!--        配置文件也是在resouce目录下,且只能以logback.xml/logback-test.xml命名--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.11</version></dependency></dependencies></project>

logback.xml


<?xml version="1.0" encoding="UTF-8"?>
<configuration ><!--0. 日志格式和颜色渲染 --><!-- 彩色日志依赖的渲染类 --><conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /><conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /><conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /><!-- 彩色日志格式 --><property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><!--CONSOLE :表示当前的日志信息是可以输出到控制台的。--><appender name="Console" class="ch.qos.logback.core.ConsoleAppender"><encoder><pattern>[%level] %blue(%d{HH:mm:ss.SSS}) %cyan([%thread]) %boldGreen(%logger{15}) - %msg %n</pattern></encoder></appender><!--触动到定义部分,就输出对应级别日志--><logger name="org.apache.ibatis" level="TRACE"/><logger name ="java.sql.Connection" level="DEBUG"/><logger name="java.sql.PreparedStatement" level="DEBUG"/><logger name="java.sql.Statement" level="DEBUG"/><logger name="com.aistart.tech.mapper" level="DEBUG"/><!--level:用来设置打印级别,大小写无关:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF, 默认debugINFO类似于sout<root>可以包含零个或多个<appender-ref>元素,标识这个输出位置将会被本日志级别控制。--><root level="DEBUG"><appender-ref ref="Console"/></root>
</configuration>

mybatis-config.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings><typeAliases><package name="com.aistart.tech.pojo"/></typeAliases><!--    <typeAliases>-->
<!--        <typeAlias alias="Products" type="com.aistart.tech.pojo.Products"/>--><!--    </typeAliases>--><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mybatisdb"/><property name="username" value="root"/><property name="password" value="root"/><property name="poolMaximumActiveConnections" value="1"/></dataSource></environment><environment id="testdevelopment"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/test"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper class="com.aistart.tech.mapper.ProductsMapper"/><!--        <mapper resource="com/aistart/tech/mapper/ProductsMapper.xml"></mapper>--></mappers>
</configuration>

ProductsMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aistart.tech.mapper.ProductsMapper"><sql id="all">product_id, product_name, brand, price, color, storage_capacity, description</sql><resultMap id="selectOneMap" type="com.aistart.tech.dto.PhoneDto"><result column="product_name" property="name"/><result column="price" property="jg"/></resultMap><sql id="select">select <include refid="all"/> from productswhere</sql><!--XML tag has empty body一个标签中resultmap和resultType只能存在一个用到了resultmap就写resultmap--><select id="selectOneMap" resultMap="selectOneMap"><include refid="select"/>product_id = 1;</select><select id="selectOneByNameAndPrice" resultType="Products"><include refid="select"/>product_name = #{productName}andprice > #{price}</select><!--调用    sqlSession.selectOne("com.aistart.tech.mapper.ProductsMapper.selectOne",products)--><!--id作为这个sql的唯一id,调用也是通过它--><insert id="insertOne" parameterType="Products" ><selectKey keyProperty="productId" keyColumn="product_id" resultType="int" order="AFTER">select last_insert_id();</selectKey>insert into products (product_name,color,price)values (#{productName},#{color},#{price})</insert><delete id="deleteOneById">delete from products where product_id = #{askdksad}</delete><update id="updateProductName" parameterType="Products">update products set product_name = #{productName} where product_id = #{productId}</update><select id="selectOneById" parameterType="int" resultType="com.aistart.tech.pojo.Products">/*参数名一个时虽然可以随便写,但是不推荐*/select<include refid="all"/>from products where product_id = #{productId}/*默认调用了get函数,利用反射,根据get+param()的形式找函数并且执行*/</select><!--JDBC>>>>sql>>>>resultSet>>>获取列并遍历>>>>>>--><select id="selectAll" resultType="com.aistart.tech.pojo.Products">select<include refid="all"></include>from products</select><select id="selectOneBynName" resultType="Products"><include refid="select"></include>product_name = #{name}</select>
</mapper>

Products


package com.aistart.tech.pojo;import lombok.*;@Data
@AllArgsConstructor
@NoArgsConstructor
public class Products {private Integer productId;private String productName;private String brand;private Double price;private String color;private String storageCapacity;private String description;}

ProductsMapper.java


package com.aistart.tech.mapper;import com.aistart.tech.dto.PhoneDto;
import com.aistart.tech.pojo.Products;
import org.apache.ibatis.annotations.*;public interface ProductsMapper {/** 三种传参数方式** 1.直接对象传参* 2.map传参* 3.参数传参*        a.单一传值,不用在乎变量名*        b.多值时,必须指明参数*** */public int insertOne(Products products);public Products selectOneById(int num);/*** todo:*  重要字段:  id(函数名)  传入类型(参数)  返回类型(返回类型)*           sql语句 select标签 @Select   sql语句("")**** 跟之前用mapper语法一致,传参数有以下几个条件* 1.单一值时,随便,自动匹配* 2.多参数时*       a.多个参数@Param("映射的#{}中的`字段")*       b.对象/map,注意映射字段名* @param name(mapper中的paramType)* @return (resultType)*/@Select("select * from products where product_name = #{name}")public Products selectOneByName(String name);//在参数前指定映射
//    @Select("select product_name from products where product_name = #{productName} and price > #{price}")public Products selectOneByNameAndPrice(@Param("productName") String productName,@Param("price") double price);@Results({@Result(property = "name", column = "product_name"),@Result(property = "jg", column = "price")})@Select("select product_name,price from products where product_name = #{productName}")public PhoneDto selectDtoByName(@Param("productName") String productName);/** <select id = "selectDtoByName" paramType ="String" resultType = "PhoneDto" >*       select product_name from products where product_name = #{productName}** </select>* */
}
http://www.lryc.cn/news/326181.html

相关文章:

  • AJAX介绍使用案例
  • 【echart】数据可视化
  • 排序(冒泡/快速/归并)
  • jq中的跨域
  • CUDA学习笔记08: 原子规约/向量求和
  • PointNet++论文复现(一)【PontNet网络模型代码详解 - 分类部分】
  • AI渣土车监测报警摄像机
  • Spring框架介绍及详细使用
  • 【论文速读】| 对大语言模型解决攻击性安全挑战的实证评估
  • 小迪安全48WEB 攻防-通用漏洞Py 反序列化链构造自动审计 bandit魔术方法
  • 微服务:解放软件开发的神器,引领企业级应用的未来(二)
  • easyexcel与vue配合下载excel
  • Vue.js 模板语法
  • 信号处理--基于DEAP数据集的情绪分类的典型深度学习模型构建
  • Spring设计模式-实战篇之模板方法模式
  • PTA天梯赛习题 L2-006 树的遍历
  • js相关的dom方法
  • Django——Ajax请求
  • 基于java多角色学生管理系统论文
  • python(django)之单一接口管理功能后台开发
  • 教程1_图像视频入门
  • MQTT.fx和MQTTX 链接ONENET物联网提示账户或者密码错误
  • Svn添加用户、添加用户组、配置项目权限等自动化配置脚本
  • Spring事务-两种开启事务管理的方式:基于注解的声明式事务管理、基于编程式的事务管理
  • OC 技术 苹果内购
  • 云原生周刊:Kubernetes v1.30 一瞥 | 2024.3.25
  • 2016年认证杯SPSSPRO杯数学建模D题(第一阶段)NBA是否有必要设立四分线解题全过程文档及程序
  • EdgeGallery开发指南
  • ubuntu arm qt 读取execl xls表格数据
  • STM32 使用gcc编译介绍