先上成功运行结果:

1.新建项目 ,这里新建一个普通的JavaSE项目,当然你也可以新建web项目

然后点Next,然后再点Finish就好了。

2.创建resources文件夹,该文件夹和java文件夹同一级

创建完以后,在文件夹上点右键:

3.在Java文件夹中新建学生实体类,对应数据库中的学生表:

package com.leo;import lombok.Data;@Data
public class Student {/****/private Integer id;/****/private String name;/****/private String sex;/****/private String specialty;/****/private String grade;
}

然后在类名上按Alt+Insert,会出来这个,没有的话应该是没装插件,先去装mybatis的插件:

然后在studentmapper.xml 中写sql语句:

<?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.leo.StudentMapper"><!--auto generated Code--><resultMap id="BaseResultMap" type="com.leo.Student"><result column="id" property="id" jdbcType="INTEGER"/><result column="name" property="name" jdbcType="VARCHAR"/><result column="sex" property="sex" jdbcType="VARCHAR"/><result column="specialty" property="specialty" jdbcType="VARCHAR"/><result column="grade" property="grade" jdbcType="VARCHAR"/></resultMap><!--auto generated Code--><sql id="Base_Column_List">id,`name`,sex,specialty,grade</sql><!--auto generated Code--><insert id="insert" useGeneratedKeys="true" keyProperty="student.id">INSERT INTO student (id,`name`,sex,specialty,grade) VALUES (#{student.id,jdbcType=INTEGER},#{student.name,jdbcType=VARCHAR},#{student.sex,jdbcType=VARCHAR},#{student.specialty,jdbcType=VARCHAR},#{student.grade,jdbcType=VARCHAR})</insert><!--auto generated Code--><insert id="insertSelective" useGeneratedKeys="true" keyProperty="student.id">INSERT INTO student<trim prefix="(" suffix=")" suffixOverrides=","><if test="student.id!=null">id,</if><if test="student.name!=null">`name`,</if><if test="student.sex!=null">sex,</if><if test="student.specialty!=null">specialty,</if><if test="student.grade!=null">grade,</if></trim>VALUES<trim prefix="(" suffix=")" suffixOverrides=","><if test="student.id!=null">#{student.id,jdbcType=INTEGER},</if><if test="student.name!=null">#{student.name,jdbcType=VARCHAR},</if><if test="student.sex!=null">#{student.sex,jdbcType=VARCHAR},</if><if test="student.specialty!=null">#{student.specialty,jdbcType=VARCHAR},</if><if test="student.grade!=null">#{student.grade,jdbcType=VARCHAR},</if></trim></insert><!--auto generated Code--><insert id="insertList">INSERT INTO student (id,`name`,sex,specialty,grade)VALUES<foreach collection="students" item="student" index="index" separator=",">(#{student.id,jdbcType=INTEGER},#{student.name,jdbcType=VARCHAR},#{student.sex,jdbcType=VARCHAR},#{student.specialty,jdbcType=VARCHAR},#{student.grade,jdbcType=VARCHAR})</foreach></insert><!--auto generated Code--><update id="updateByPrimaryKeySelective">UPDATE student<set><if test="student.name != null">`name`= #{student.name,jdbcType=VARCHAR},</if><if test="student.sex != null">sex= #{student.sex,jdbcType=VARCHAR},</if><if test="student.specialty != null">specialty= #{student.specialty,jdbcType=VARCHAR},</if><if test="student.grade != null">grade= #{student.grade,jdbcType=VARCHAR}</if></set>WHERE id = #{student.id,jdbcType=INTEGER}</update><select id="selectOne" resultType="com.leo.Student">select * from student where id = #{id};</select></mapper>

然后新建xml文件,命名为config.xml,这是mybatis的配置文件:

<?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><settings><!-- Globally enables or disables any caches configured in any mapper under this configuration --><setting name="cacheEnabled" value="false"/><!-- Sets the number of seconds the driver will wait for a response from the database --><setting name="defaultStatementTimeout" value="5"/><!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn --><setting name="mapUnderscoreToCamelCase" value="true"/><!-- Allows JDBC support for generated keys. A compatible driver is required.This setting forces generated keys to be used if set to true,as some drivers deny compatibility but still work --><setting name="useGeneratedKeys" value="true"/></settings><!-- Continue editing here --><!-- 数据库环境 --><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/stumanage?characterEncoding=UTF-8"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><!-- 映射文件 --><mappers><mapper resource="StudentMapper.xml"/></mappers></configuration>

最后写一个含有主函数的类测试一下:

public static void main(String[] args) {// 根据 config.xml 配置的信息得到 sqlSessionFactoryString resource = "config.xml";InputStream inputStream = null;try {inputStream = Resources.getResourceAsStream(resource);} catch (IOException e) {e.printStackTrace();}SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 然后根据 sqlSessionFactory 得到 sessionSqlSession session = sqlSessionFactory.openSession();// 最后通过 session 的 selectList() 方法调用 sql 语句 listStudentStudent student = session.selectOne("selectOne", 6);System.out.println(student.toString());}

IDEA使用MyBatis【超级详细,绝对能运行】相关推荐

  1. 手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)

    手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版) SSM(Spring+SpringMVC+Mybatis),目前较为主流的企业级架构方案.标准的MVC设计模式, ...

  2. 超级详细配置SSM (Intellij idea + Maven + Spring + SpringMVC + MyBatis + c3p0 )

    时间2018/12/31,使用的包都是最新的和用的人最多的QAQ. Maven仓库查找包的地址 https://mvnrepository.com/ 在经历了70多个小时的奋斗后(花了60个小时学习了 ...

  3. 超级详细的Spring Boot 注解总结

    日常编程中我相信大家肯定都用过spring,也用过spring的注解,哪怕面试的时候也经常会被问到一些spring和spring boot注解的作用和含义等,那么这篇就带大家来看看超级详细的Sprin ...

  4. SpringBoot整合Mybatis超详细流程

    SpringBoot整合Mybatis超详细流程 文章目录 SpringBoot整合Mybatis超详细流程 前言 详细流程 0.引入Mybatis 1.创建数据 2.创建程序目录 3.理解后台访问流 ...

  5. import的用法python_Python导入模块,Python import用法(超级详细)

    Python导入模块,Python import用法(超级详细) 使用 Python 进行编程时,有些功能没必须自己实现,可以借助 Python 现有的标准库或者其他人提供的第三方库.比如说,在前面章 ...

  6. linux下Oracle 10g安装(超级详细图解教程)

    linux下Oracle 10g安装(超级详细图解教程) 一,基本配置: 1.以root登录,挂载linux iso文件 [root@oracle ~]# hostnameoracle.junjie. ...

  7. 超级详细的手写webpack4配置来启动vue2项目(附配置作用)

    基础目录结构以及各个文件的作用 初始npm项目 npm init 一路回车,一律使用默认的npm项目配置 package.json修改scripts 如下: {"name": &q ...

  8. 超级详细备注的代码:Python帮助您高效通过英语六级考试

    超级详细备注的代码:Python帮助您高效通过各种英语考试 标题:限时免费|领取大学英语六级考试葵花宝典 联系小编,获取源码和30份六级真题. # -*- coding:utf-8 -*-#作者:公众 ...

  9. 网络管理:超级详细Tcpdump 的用法

    第一种是关于类型的要害字,主要包括host,net,port, 例如 host 210.27.48.2,指明 210.27.48.2是一台主机,net 202.0.0.0 指明 202.0.0.0是一 ...

  10. 基于STM32F4的CANOpen移植教程(超级详细)

    CANopen移植到STM32F4平台 前言 1 物品准备 2 相关软件安装 2.1 CAN上位机 2.2 对象字典生成工具objdictedit环境配置 3 将CANopen移植到STM32F407 ...

最新文章

  1. 微软回应 CIA 漏洞攻击 Win10 问题:正在研究维基解密报告
  2. Matlab Robotic Toolbox V9.10工具箱(四):常用函数
  3. G4Sui老师的pair project(197)
  4. JVM锁和分布式锁是什么关系
  5. Redis简介和Redis Template用法整理
  6. 面了三次字节,他的一些感悟
  7. [数据结构-严蔚敏版]P42多项式Polynomial的实现
  8. leetcode 3Sum C++
  9. 鱼骨图分析法实际案例_会用“鱼骨图”的项目经理无难题
  10. “已使用指定的进程(“Web Management Service”)连接到远程计算机,但未能验证服务器的证书”的解决方案
  11. 使用xadmin覆盖Django的admin
  12. 审计 6 SSRF和任意文件读取
  13. 注册csdn博客步骤
  14. 调通sina33m下的AP6212A0版本(分色排版)V1.0版本
  15. python高级用法使用手册(收藏)
  16. windows xp 下载老版本的chrome浏览器
  17. 密钥可以永久激活吗?
  18. 洛谷——P1296 奶牛的耳语(java实现)
  19. BAT机器学习面试1000题系列(详细版)
  20. 教你如何利用php.exe运行php文件

热门文章

  1. win10系统下安装Linux系统
  2. 一、SpringCloud五大神兽之Eureka(eurekaServer集群)
  3. 飞飞cms添加广告html,飞飞影视cms-飞飞cms免费影视站程序
  4. QML_ToolSeparator、ToolTip和Tumbler
  5. Export metadata for 'Type' is missing and no defau
  6. day07 pyecharts制图
  7. 互联网促进全民公益时代的到来
  8. 走出软件作坊:三五个人十来条枪 如何成为开发正规军 链接[收藏]
  9. 搭建一个电商app系统软件要多少钱
  10. 云南贵州地区市场知名的调查研究咨询公司