【狂神说Mybatis29道练习题】

2023-11-09

Mybatis动态SQL(狂神说学习笔记)29道练习题

以下代码分为工具类、几个配置文件(mybatis-config.xml)、实体类、持久层(mapper映射文件)、测试类,代码我自己写完全部运行一遍,没有BUG。

一、 工具类
MybatisUtils

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 获得 SqlSession 的实例
 */
public class MybatisUtils {
    //既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例.
    // SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。
    public static SqlSession getSqlSession(){
        InputStream inputStream = null;
        try {
            String resource = "mybatis-config.xml";
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory.openSession(true);//默认提交事务,这样可以省略sqlSession.commit()该提交事务的代码
    }
}

二、 配置文件:
配置文件和Mapper映射文件需要注意一点:如果出现中文注释引起程序出现异常的问题,我是把文件中的第一行的代码改成了“encoding=“utf8””,原本代码是设置UTF-8。
mybatis-config.xml

<?xml version="1.0" encoding="utf8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="db.properties"/>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>

    </settings>
    <typeAliases>
    <!--     这里需要根据每个人创建的包名去修改   -->
        <package name="com.hwx.pojo"/>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
       <!--     这里也需要根据每个人创建的包名去修改   -->
        <mapper class="com.hwx.dao.user.UserMapper"/>
        <mapper class="com.hwx.dao.role.RoleMapper"/>
        <mapper class="com.hwx.dao.provider.ProviderMapper"/>
        <mapper class="com.hwx.dao.bill.BillMapper"/>
    </mappers>

</configuration>

db.properties:这里根据自己设置的账号密码数据库名字去修改

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456

log4j.properties:

#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
​
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/rzp.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sq1.PreparedStatement=DEBUG

三、 实体类:
User

package com.hwx.pojo;

import java.io.Serializable;
import java.util.Date;
/*
 *用户
 */
public class User implements Serializable {
    //id
    private Integer id;
    //用户编码
    private String userCode;
    //用户名
    private String userName;
    //用户密码
    private String userPassword;
    //性别
    private Integer gender;
    //出生日期
    private Date birthday;
    //电话
    private String phone;
    //地址
    private String address;
    //用户角色
    private Integer userRole;
    //创建者
    private Integer createdBy;
    //创建时间
    private Date creationDate;
    //更新者
    private Integer modifyBy;
    //更新时间
    private Date modifyDate;
    //年龄
    private Integer age;
    //用户角色名称
    private String userRoleName;
    public User() {
    }
    public User(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy, Date modifyDate, Integer age, String userRoleName) {
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
        this.age = age;
        this.userRoleName = userRoleName;
    }

    public User(Integer id, String userCode, String userName, String userPassword,
                Integer gender, Date birthday, String phone, String address,
                Integer userRole, Integer createdBy, Date creationDate) {
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
    }
    public Integer getAge() {
        /*Long time = System.currentTimeMillis( ) -birthday. getTime();
        Integer age = Long.valueof(time/365/24/60/60/1ee0).IntegerVaLue();*/
        Date date = new Date();
        Integer age = date.getYear()-birthday.getYear();
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getUserRoleName() {
        return userRoleName;
    }
    public void setUserRoleName(String userRoleName) {
        this.userRoleName = userRoleName;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserCode() {
        return userCode;
    }
    public void setUserCode(String userCode) {
        this.userCode = userCode;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserPassword() {
        return userPassword;
    }
    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public Integer getUserRole() {
        return userRole;
    }
    public void setUserRole(Integer userRole) {
        this.userRole = userRole;
    }
    public Integer getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }
    public Date getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
    public Integer getModifyBy() {
        return modifyBy;
    }
    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }
    public Date getModifyDate() {
        return modifyDate;
    }
    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }
}

Role

package com.hwx.pojo;
import java.util.Date;
/*
 *角色
 */
public class Role {
    //id
    private Integer id;
    //角色编码
    private String roleCode;
    //角色名称
    private String roleName;
    //创建者
    private Integer createdBy;
    //创建时间
    private Date creationDate;
    //更新者
    private Integer modifyBy;
    //更新时间
    private Date modifyDate;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getRoleCode() {
        return roleCode;
    }
    public void setRoleCode(String roleCode) {
        this.roleCode = roleCode;
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public Integer getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }
    public Date getCreationDate() {
        return creationDate;
    }
    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }
    public Integer getModifyBy() {
        return modifyBy;
    }
    public void setModifyBy(Integer modifyBy) {
        this.modifyBy = modifyBy;
    }
    public Date getModifyDate() {
        return modifyDate;
    }
    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }
}

Provider

package com.hwx.pojo;

import java.util.Date;
/*
 *供应商
 */
public class Provider {
    private Integer id;   //id
    private String proCode; //供应商编码
    private String proName; //供应商名称
    private String proDesc; //供应商描述
    private String proContact; //供应商联系人
    private String proPhone; //供应商电话
    private String proAddress; //供应商地址
    private String proFax; //供应商传真
    private Integer createdBy; //创建者
    private Date creationDate; //创建时间
    private Integer modifyBy; //更新者
    private Date modifyDate;//更新时间

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getProCode() {
        return proCode;
    }
    public void setProCode(String proCode) {
        this.proCode = proCode;
    }
    public String getProName() {
        return proName;
    }
    public void setProName(String proName) {
        this.proName = proName;
    }
    public String getProDesc() {
        return proDesc;
    }
    public void setProDesc(String proDesc) {
        this.proDesc = proDesc;
    }
    public String getProContact() {
        return proContact;
    }
    public void setProContact(String proContact) {
        this.proContact = proContact;
    }
    public String getProPhone() {
        return proPhon
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【狂神说Mybatis29道练习题】 的相关文章

随机推荐

  • Journal of Proteome Research

    期刊名 Journal of Proteome Research 发表时间 2019年9月 IF 3 78 2018 单位 巴塞尔大学 瑞士 物种 人细胞系 技术 冷冻电子显微镜 Cryo EM 单粒子电子显微镜 一 概述 本文描述了一种
  • C语言的字符串查找函数

    C C string库 string h 提供了几个字符串查找函数 如下 memchr 在指定内存里定位给定字符 strchr 在指定字符串里定位给定字符 strcspn 返回在字符串str1里找到字符串str2里的任意一个字符之前已查找的
  • arma模型matlab代码_时间序列分析ARMA模型(金融计量一)

    以下内容为原创 如有错误请联系纠正 联系作者方式 微信公众号 计量文学 公众号会发布计量学相关文章 软件安装教程 公众号刚起步 希望多多支持 作业说明 1 给出原序列折线图 并加以文字描述 2 给出原序列或差分序列 如果有需要 的自相关函数
  • 深入剖析Kubernetes之声明式 API

    文章目录 声明式 API 编写自定义控制器 声明式 API 到底什么才是 声明式 API 呢 kubectl apply 命令 kubectl replace 的执行过程 是使用新的 YAML 文件中的 API 对象 替换原有的 API 对
  • Vue-Router总结

    路由三大组成部分 router link 导航 link 标签 router view 路由视图 路由页面呈现的地方 new VueRouter 路由配置 routes router link属性 1 to进行页面跳转 更改路径 2 tag
  • 着手MQTT.fx软件应用,利于深入了解MQTT协议数据连接、传递、订阅/发布流程

    第一步 Extras gt Edit connection 第二步 添加连接信息 第三步 选择连接的服务器 第四步 点击连接 成功就变为绿色 第五步 订阅 第六步 发布 第七步 查看订阅数据 这大致就是一个基本的使用过程 MQTT fx的下
  • 软件工程毕业设计题目合集【含源码+论文】

    文章目录 前言 题目1 基于SSM的房屋出租出售系统 br 题目2 基于SSM的房屋租赁系统 br 题目3 基于SSM的个人健康信息管理系统 br 题目4 基于SSM的共享充电宝管理系统 br 题目5 基于SSM的即动运动网站 br 前言
  • 操作系统期末整理!!!重要!!

    操作系统 期末可以过啦 第一章 操作系统引论 1 操作系统的定义 2 操作系统的作用 3 操作系统发展过程 4 各类型操作系统的特点 5 操作系统的基本特征 6 操作系统的基本功能 7 异常和中断的区别 8 为什么说中断是操作系统的核心技术
  • Keil报: warning: #223-D: function “某某某“ declared implicitly 的警告,三个解决方法

    原因 找不到 某某某 函数 解决 看有没有 include 相关头文件 看函数定义有没有出错 函数定义有一点不同就会出现上述原因 我个人遇到的比较奇葩的原因 emmm 人比较奇葩吧 在两个不同的 h文件中写了相同的 ifndef INA H
  • led灯条串联图_三分钟学会DIY个性LED灯

    上篇文章介绍了LED光源 主要介绍LED结构 常用参数 型号 常见品牌等内容 回看的小伙伴请点击照明灯饰专栏 今天主要介绍LED灯的工作原理与常见的LED灯 明白之后DIY自己的个性LED毫无压力 先别跳过下面有干货 一 LED驱动 上篇文
  • GoogLeNet论文阅读笔记

    目录 前言 GoogLeNet论文阅读笔记 Abstract 1 Introduction 2 Related Work 3 Motivation and High Level Considerations 4 Architectural
  • 上升沿_输入输出的上升沿和下降沿是怎么来的,一起看看

    高电平 低电平 上升沿和下降沿的区别 数字电路中 电平从低电平 逻辑信号为0 变为高电平 逻辑信号为1 的那一瞬间叫作上升沿 电平从高电平 逻辑信号为1 变为低电平 逻辑信号为0 的那一瞬间叫作下降沿 高电平触发 是指I O口电平为高电平时
  • Java实现给定两个 int 变量, 交换变量的值

    给定两个 int 变量 交换变量的值 1 创建变量i实现交换 2 不创建临时变量利用加减法实现 public class Solution public static void main String args int a 10 int b
  • C++ C2460 error

    关于该错误的官方说明 https msdn microsoft com en us library 1kf0205c aspx 结构形如 identifier1 uses identifier2 类或结构 identifier2 被声明为其
  • django中的跨域问题以及解决策略

    目录 跨域请求 同源策略 CORS 跨域资源共享 简介 CORS基本流程 解决跨域问题的方法 CORS两种请求详解 预检 解决跨域问题 服务端 简单请求 非简单请求 解决跨域问题 第三方 后端配置 解决跨域问题 前端 跨域请求 跨域是指浏览
  • Object Detection网络框架学习:Faster-RCNN

    经过RCNN和Fast RCNN的积淀 Ross B Girshick在2016年提出了新的Faster RCNN 在结构上 Faster RCN已经将特征抽取 feature extraction proposal提取 bounding
  • Linux找回root密码(Centos 7)

    首先 启动系统 进入开机界面 在界面中按 e 进入编辑界面 手速一定要快 进入编辑界面 使用键盘上的上下键把光标往下移动 找到以 Linux16 开头内容所在的行数 把光标移动到最尾部 在行的最后面输入 init bin sh 接着 输入完
  • vue实现点击两个按钮互相切换背景色

    首先准备两个按钮
  • HTTP中Put和Post的区别

    解释HTTP中Put和Post 它们有什么区别 哪个使用时更加安全 Put和Post都是浏览器向网页服务器提交数据的方法 Put把要提交的数据编码在url中 比如 http hi baidu com mianshiti key1 value
  • 【狂神说Mybatis29道练习题】

    Mybatis Mybatis动态SQL 狂神说学习笔记 29道练习题 Mybatis动态SQL 狂神说学习笔记 29道练习题 以下代码分为工具类 几个配置文件 mybatis config xml 实体类 持久层 mapper映射文件 测