Skip to content

[toc]

2. 入门程序搭建

2.1 数据库脚本

sql
/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2020/12/22 10:26:43                          */
/*==============================================================*/


CREATE DATABASE /*!32312 IF NOT EXISTS*/ mybatis /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
USE mybatis;



/*==============================================================*/
/* Table: check_apply                                           */
/*==============================================================*/
CREATE TABLE check_apply
(
   id                   INT NOT NULL AUTO_INCREMENT COMMENT 'id',
   register_id          int comment '病历号',
   item_id              int comment '项目id',
   item_name            varchar(100) comment '项目名称',
   fee                  decimal(8,2) comment '检查费用',
   status               int comment '状态',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table check_apply comment '检查申请';

/*==============================================================*/
/* Table: check_item                                            */
/*==============================================================*/
create table check_item
(
   id                   int not null auto_increment comment 'id',
   name                 varchar(100) comment '检查名称',
   fee                  decimal(8,2) comment '检查费用',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table check_item comment '检查项目';

/*==============================================================*/
/* Table: constant_item                                         */
/*==============================================================*/
create table constant_item
(
   id                   int not null auto_increment comment 'id',
   type_id              int comment '类别id',
   code                 varchar(100) comment '常数项代码',
   name                 varchar(100) comment '常数项名称',
   sort                 int comment '排序',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table constant_item comment '常数项表';

/*==============================================================*/
/* Table: constant_type                                         */
/*==============================================================*/
create table constant_type
(
   id                   int not null auto_increment comment '主键id',
   code                 varchar(100) comment '代码',
   name                 varchar(100) comment '名称',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table constant_type comment '常数类别';

/*==============================================================*/
/* Table: department                                            */
/*==============================================================*/
create table department
(
   id                   int not null auto_increment comment 'id',
   name                 varchar(100) comment '名称',
   address              varchar(200) comment '办公地址',
   leader               varchar(100) comment '负责人',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table department comment '科室';

/*==============================================================*/
/* Table: inspect_apply                                         */
/*==============================================================*/
create table inspect_apply
(
   id                   int not null comment 'id',
   item_id              int comment '项目id',
   item_name            varchar(100) comment '项目名称',
   fee                  decimal(8,2) comment '检查费用',
   status               int comment '状态',
   register_id          int comment '病历号',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table inspect_apply comment '检验申请';

/*==============================================================*/
/* Table: inspect_item                                          */
/*==============================================================*/
create table inspect_item
(
   id                   int not null auto_increment comment 'id',
   name                 varchar(100) comment '检查名称',
   fee                  decimal(8,2) comment '检查费用',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table inspect_item comment '检验项目';

/*==============================================================*/
/* Table: menu                                                  */
/*==============================================================*/
create table menu
(
   menu_id              int not null auto_increment comment '菜单id',
   menu_name            varchar(100) comment '菜单名称',
   url                  varchar(100) comment '菜单url',
   parent_id            int comment '上级菜单id',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (menu_id)
);

alter table menu comment '菜单';

/*==============================================================*/
/* Table: regist_level                                          */
/*==============================================================*/
create table regist_level
(
   id                   int not null auto_increment comment 'id',
   name                 varchar(100) comment '名称',
   fee                  decimal(8,2) comment '费用',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table regist_level comment '挂号级别';

/*==============================================================*/
/* Table: register                                              */
/*==============================================================*/
create table register
(
   id                   int not null auto_increment comment 'id病历号',
   name                 varchar(100) comment '姓名',
   gender               int comment '性别',
   idno                 varchar(100) comment '身份证号',
   birthday             date comment '出生日期',
   age                  int comment '年龄',
   address              varchar(200) comment '家庭住址',
   regsit_level_id      int comment '挂号级别',
   dept_id              int comment '挂号科室',
   doctor_id            int comment '看诊医生',
   book                 int comment '是否要病历本',
   visittime            date comment '看诊时间',
   fee                  decimal(8,2) comment '挂号费用',
   readme               varchar(500) comment '主诉',
   present              varchar(500) comment '现病史',
   present_treat        varchar(500) comment '现病史治疗情况',
   history              varchar(500) comment '既往史',
   allergy              varchar(500) comment '过敏史',
   disease              varchar(500) comment '确诊疾病',
   suit                 varchar(500) comment '处置方案',
   drug                 varchar(500) comment '药品清单',
   status               int comment '状态',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (id)
);

alter table register comment '诊疗信息';

/*==============================================================*/
/* Table: role                                                  */
/*==============================================================*/
create table role
(
   role_id              int not null auto_increment comment '角色id',
   role_name            varchar(100) comment '角色名称',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (role_id)
);

alter table role comment '角色';

/*==============================================================*/
/* Table: role_menu                                             */
/*==============================================================*/
create table role_menu
(
   menu_id              int not null comment '菜单id',
   role_id              int not null comment '角色id',
   primary key (menu_id, role_id)
);

alter table role_menu comment '角色菜单信息';

/*==============================================================*/
/* Table: user                                                  */
/*==============================================================*/
create table user
(
   user_id              int not null auto_increment comment '医生id',
   username             varchar(100) comment '用户名',
   password             varchar(100) comment '密码',
   realname             varchar(100) comment '真实姓名',
   telephone            varchar(20) comment '电话号码',
   dept_id              int comment 'id',
   user_type            int comment '医生类型',
   lastlogin            datetime comment '最后登录时间',
   active               int default 1 comment '是否有效,1 有效,0 失效',
   createtime           datetime default CURRENT_TIMESTAMP comment '创建时间',
   primary key (user_id)
);

alter table user comment '医生-用户信息';

/*==============================================================*/
/* Table: user_role                                             */
/*==============================================================*/
create table user_role
(
   user_id              int not null comment '医生id',
   role_id              int not null comment '角色id',
   primary key (user_id, role_id)
);

alter table user_role comment '用户角色';

alter table role_menu add constraint FK_Reference_1 foreign key (menu_id)
      references menu (menu_id) on delete restrict on update restrict;

alter table role_menu add constraint FK_Reference_2 foreign key (role_id)
      references role (role_id) on delete restrict on update restrict;

alter table user add constraint FK_Reference_5 foreign key (dept_id)
      references department (id) on delete restrict on update restrict;

alter table user_role add constraint FK_Reference_3 foreign key (user_id)
      references user (user_id) on delete restrict on update restrict;

alter table user_role add constraint FK_Reference_4 foreign key (role_id)
      references role (role_id) on delete restrict on update restrict;

2.2 创建Maven项目

2.3 添加依赖

  1. mybaits 3.5.5
  2. mysql数据库驱动 5.1.49
  3. junit 4.12
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">
    <parent>
        <artifactId>mybatis-parent-java1</artifactId>
        <groupId>com.neuedu.mybatis</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <packaging>jar</packaging>
    <artifactId>01-mybaits-helloworld</artifactId>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>


        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.5</version>
        </dependency>


    </dependencies>





</project>

2.4 创建SqlMapConfig.xml

XML 配置文件中包含了对 MyBatis 系统的核心设置,包括获取数据库连接实例的数据源(DataSource)以及决定事务作用域和控制方式的事务管理器(TransactionManager)。后面会再探讨 XML 配置文件的详细内容,这里先给出一个简单的示例:

xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>


    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/upload"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>


    <mappers>
        <!-- 每张表的 statement-->
        <mapper resource="mapper.xml"/>
    </mappers>
</configuration>

2.5 创建映射文件mapper.xml

一个语句既可以通过 XML 定义,也可以通过注解定义。我们先看看 XML 定义语句的方式,事实上 MyBatis 提供的所有特性都可以利用基于 XML 的映射语言来实现,这使得 MyBatis 在过去的数年间得以流行。如果你用过旧版本的 MyBatis,你应该对这个概念比较熟悉。 但相比于之前的版本,新版本改进了许多 XML 的配置,后面我们会提到这些改进。这里给出一个基于 XML 映射语句的示例,它应该可以满足上个示例中 SqlSession 的调用

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.neuedu.mybaits.helloworld">


    <select id="selectUploadFiles" resultType="com.neuedu.mybaits.entity.UploadFile">
            select * from upload_files
    </select>


</mapper>

2.6 使用resource 加载Mapper配置文件

<mappers>
	<!-- 每张表的 statement-->
	<mapper resource="mapper.xml"/>
</mappers>

2.7 使用Mybaits

xml构建SQLSessionFactory并创建SQLSession对象,访问MapperStatement执行sql语句

java
package com.neuedu.mybaits.helloworld;

import com.neuedu.mybaits.entity.UploadFile;
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 org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class RunnerTest {

    @Test
    public void test() throws IOException {
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        SqlSession session = sqlSessionFactory.openSession();

        List<UploadFile> list = session.selectList("com.neuedu.mybaits.helloworld.selectUploadFiles");

        for (UploadFile uploadFile : list) {
            System.out.println(uploadFile);
        }

        session.close();

    }
}

Released under the MIT License.