闲来无事,复习一下mybatis
博客专区 > __HuWei 的博客 > 博客详情
闲来无事,复习一下mybatis
__HuWei 发表于3个月前
闲来无事,复习一下mybatis
  • 发表于 3个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

一、Mybatis介绍

  

  MyBatis是一个支持普通SQL查询存储过程高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装。MyBatis可以使用简单的XML或注解用于配置和原始映射,将接口和Java的POJO(Plain Old Java Objects,普通的Java对象)映射成数据库中的记录。

二、mybatis快速入门

2.1、准备开发环境

 1、创建测试项目,普通java项目或者是JavaWeb项目均可,如下图所示:

2、以下是jar包

    

表的话就用之前项目用的表。

CREATE TABLE `tb_unit_task_template` (
  `unit_id` varchar(20) COLLATE utf8_bin NOT NULL,
  `parent_id` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `context` text COLLATE utf8_bin,
  `type` int(1) DEFAULT NULL,
  `level` int(1) DEFAULT NULL,
  `score` float DEFAULT NULL,
  `is_use` int(1) DEFAULT NULL,
  `rule` int(1) DEFAULT NULL COMMENT '0 自动得分项 1 得分项 2 扣分项 3 为加重扣分项 发现一台扣评分标准总分 ,累加 全部取出来 4 直接询问项',
  `flag` int(1) DEFAULT NULL,
  `uint_important` int(1) DEFAULT NULL,
  `unit_ordinary` int(1) DEFAULT NULL,
  `unit_qualified` int(1) DEFAULT NULL,
  `problem` text COLLATE utf8_bin,
  `solution` text COLLATE utf8_bin,
  `depend_id` varchar(4000) COLLATE utf8_bin DEFAULT NULL,
  `ask` text COLLATE utf8_bin,
  `allow_edit` int(2) DEFAULT NULL,
  `special_rule` text COLLATE utf8_bin,
  `check_method` int(1) DEFAULT NULL,
  PRIMARY KEY (`unit_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

conf.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://localhost:3306/unit_check" />
                 <property name="username" value="root" />
                 <property name="password" value="tq26556570" />
             </dataSource>
         </environment>
     </environments>
     
     <!-- 在conf.xml中注册TemplateMapper.xml文件 -->
     <mappers>
     	<mapper resource="com/hwcode/mapper/TemplateMapper.xml"/>
     </mappers>
 </configuration>

bean中的内容:

package com.hwcode.bean;
/**
 * 
 * @author HuWei
 *	tb_unit_task_template对象
 */
public class TemplateBean {
	
	private String unitId;
	
	private String parentId;
	
	private String context;
	
	private Integer type;
	
	private Integer level;
	
	private Float score;
	
	private Integer isUse;
	
	private Integer rule;
	
	private Integer flag;
	
	private Integer uintImportant;
	
	private Integer unitOrdinary;
	
	private Integer unitQualified;
	
	private String problem;
	
	private String solution;
	
	private String dependId;
	
	private String ask;
	
	private Integer allowEdit;
	
	private String specialRule;
	
	private Integer checkMethod;

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getContext() {
		return context;
	}

	public void setContext(String context) {
		this.context = context;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public Integer getLevel() {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level;
	}

	public Float getScore() {
		return score;
	}

	public void setScore(Float score) {
		this.score = score;
	}

	public Integer getIsUse() {
		return isUse;
	}

	public void setIsUse(Integer isUse) {
		this.isUse = isUse;
	}

	public Integer getRule() {
		return rule;
	}

	public void setRule(Integer rule) {
		this.rule = rule;
	}

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public Integer getUintImportant() {
		return uintImportant;
	}

	public void setUintImportant(Integer uintImportant) {
		this.uintImportant = uintImportant;
	}

	public Integer getUnitOrdinary() {
		return unitOrdinary;
	}

	public void setUnitOrdinary(Integer unitOrdinary) {
		this.unitOrdinary = unitOrdinary;
	}

	public Integer getUnitQualified() {
		return unitQualified;
	}

	public void setUnitQualified(Integer unitQualified) {
		this.unitQualified = unitQualified;
	}

	public String getProblem() {
		return problem;
	}

	public void setProblem(String problem) {
		this.problem = problem;
	}

	public String getSolution() {
		return solution;
	}

	public void setSolution(String solution) {
		this.solution = solution;
	}

	public String getDependId() {
		return dependId;
	}

	public void setDependId(String dependId) {
		this.dependId = dependId;
	}

	public String getAsk() {
		return ask;
	}

	public void setAsk(String ask) {
		this.ask = ask;
	}

	public Integer getAllowEdit() {
		return allowEdit;
	}

	public void setAllowEdit(Integer allowEdit) {
		this.allowEdit = allowEdit;
	}

	public String getSpecialRule() {
		return specialRule;
	}

	public void setSpecialRule(String specialRule) {
		this.specialRule = specialRule;
	}

	public Integer getCheckMethod() {
		return checkMethod;
	}

	public void setCheckMethod(Integer checkMethod) {
		this.checkMethod = checkMethod;
	}

	@Override
	public String toString() {
		return "TemplateBean [unitId=" + unitId + ", parentId=" + parentId + ", context=" + context + ", type=" + type
				+ ", level=" + level + ", score=" + score + ", isUse=" + isUse + ", rule=" + rule + ", flag=" + flag
				+ ", uintImportant=" + uintImportant + ", unitOrdinary=" + unitOrdinary + ", unitQualified="
				+ unitQualified + ", problem=" + problem + ", solution=" + solution + ", dependId=" + dependId
				+ ", ask=" + ask + ", allowEdit=" + allowEdit + ", specialRule=" + specialRule + ", checkMethod="
				+ checkMethod + "]";
	}
	
	
}

TemplateMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 
	例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) -->
<mapper namespace="com.hwcode.mapper.TemplateMapper">
	<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 
		resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 -->
	<!-- 根据unitId查询得到一个Template对象 -->
	<select id="getTemplate" parameterType="int"
		resultType="com.hwcode.bean.TemplateBean">
		select * from tb_unit_task_template where unit_id=#{unitId}
	</select>
</mapper>

测试类中的内容:

package com.hwcode.test;

import java.io.InputStream;

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

import com.hwcode.bean.TemplateBean;

public class Test {

	public static void main(String[] args) {
		//mybatis的配置文件
        String resource = "conf.xml";
        //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
        InputStream is = Test.class.getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
        //Reader reader = Resources.getResourceAsReader(resource); 
        //构建sqlSession的工厂
        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        /**
         * 映射sql的标识字符串,
         * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
         * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
         */
        String statement = "com.hwcode.mapper.TemplateMapper.getTemplate";//映射sql的标识字符串
        //执行查询返回一个唯一Template对象的sql
        TemplateBean template = session.selectOne(statement, 001);
        System.out.println(template);
	}
}

END

共有 人打赏支持
粉丝 0
博文 10
码字总数 17381
×
__HuWei
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: