Eclipse+Mybatis+MySql

  1.所需jar

  2.项目目录

  3.源代码

  1. package com.zhengbin.entity;
  2.  
  3. public class Student {
  4. private int id;
  5. private String name;
  6. private double score;
  7. @Override
  8. public String toString() {
  9. return "Student [id=" + id + ", name=" + name + ", score=" + score + "]";
  10. }
  11. public int getId() {
  12. return id;
  13. }
  14. public void setId(int id) {
  15. this.id = id;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23. public double getScore() {
  24. return score;
  25. }
  26. public void setScore(double score) {
  27. this.score = score;
  28. }
  29. public Student(int id, String name, double score) {
  30. super();
  31. this.id = id;
  32. this.name = name;
  33. this.score = score;
  34. }
  35. // *注意这个必须加
  36. public Student() {
  37. super();
  38. }
  39. }

Student.java

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <!-- 每个映射文件的namespace应该是唯一的 -->
  4. <mapper namespace="com.zhengbin.entity.studentMapper">
  5. <!-- parameterType 参数表示需要参数的类型 -->
  6. <!-- resultType 参数表示返回结果的类型,该可以写为实体包的全路径,或者在conf.xml配置文件中,声明实体的别名 -->
  7. <select id="getStudent" parameterType="int" resultType="Student">
  8. select * from student where id=#{id}
  9. </select>
  10.  
  11. <select id="getAllStudent" resultType="Student">
  12. select * from student
  13. </select>
  14.  
  15. <insert id="addStudent" parameterType="Student">
  16. insert into student(name,score) values(#{name},#{score})
  17. </insert>
  18.  
  19. <update id="updateStudent" parameterType="Student">
  20. update student set name=#{name},score=#{score} where id=#{id}
  21. </update>
  22.  
  23. <delete id="deleteStudent" parameterType="int">
  24. delete from student where id=#{id}
  25. </delete>
  26. </mapper>

studentMapper.xml

  1. package com.zhengbin.entity2;
  2.  
  3. import org.apache.ibatis.annotations.Select;
  4.  
  5. import com.zhengbin.entity.Student;
  6.  
  7. public interface studentMapper {
  8. @Select("select * from student where id=#{id}")
  9. public Student testGet(int id);
  10. }

studentMapper.java

  1. package com.zhengbin.test;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.ibatis.session.SqlSession;
  6. import org.apache.ibatis.session.SqlSessionFactory;
  7.  
  8. import com.zhengbin.entity.Student;
  9. import com.zhengbin.util.MyBatisUtils;
  10.  
  11. public class Test {
  12. @org.junit.Test
  13. public void testGet(){
  14. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  15. // 参数为TRUE,相当于session.commit();
  16. SqlSession session = sessionFactory.openSession(true);
  17. // 读取映射文件
  18. String statement = "com.zhengbin.entity.studentMapper" + ".getStudent";
  19. Student s = session.selectOne(statement,5);
  20. System.out.println(s);
  21. session.close();
  22. }
  23.  
  24. @org.junit.Test
  25. public void testGetAll(){
  26. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  27. // 参数为TRUE,相当于session.commit();
  28. SqlSession session = sessionFactory.openSession(true);
  29. // 读取映射文件
  30. String statement = "com.zhengbin.entity.studentMapper" + ".getAllStudent";
  31. List<Student> list = session.selectList(statement);
  32. System.out.println(list);
  33. session.close();
  34. }
  35.  
  36. @org.junit.Test
  37. public void testAdd(){
  38. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  39. SqlSession session = sessionFactory.openSession(true);
  40. String statement = "com.zhengbin.entity.studentMapper" + ".addStudent";
  41. Student s = new Student();
  42. s.setName("zhengB");
  43. s.setScore(95.9);
  44. int insert = session.insert(statement, s);
  45. System.out.println(insert);
  46. session.close();
  47. }
  48.  
  49. @org.junit.Test
  50. public void testUpdate(){
  51. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  52. SqlSession session = sessionFactory.openSession(true);
  53. String statement = "com.zhengbin.entity.studentMapper" + ".updateStudent";
  54. Student s = new Student();
  55. s.setId(14);
  56. s.setName("zhengbin");
  57. s.setScore(96);
  58. int update = session.update(statement, s);
  59. System.out.println(update);
  60. session.close();
  61. }
  62.  
  63. @org.junit.Test
  64. public void testDelete(){
  65. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  66. SqlSession session = sessionFactory.openSession(true);
  67. String statement = "com.zhengbin.entity.studentMapper" + ".deleteStudent";
  68. int delete = session.delete(statement,21);
  69. System.out.println(delete);
  70. session.close();
  71. }
  72. }

Test.java

  1. package com.zhengbin.test;
  2.  
  3. import org.apache.ibatis.session.SqlSession;
  4. import org.apache.ibatis.session.SqlSessionFactory;
  5.  
  6. import com.zhengbin.entity.Student;
  7. import com.zhengbin.entity2.studentMapper;
  8. import com.zhengbin.util.MyBatisUtils;
  9.  
  10. public class Test2 {
  11. @org.junit.Test
  12. public void testGet(){
  13. SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
  14. SqlSession session = sessionFactory.openSession(true);
  15. studentMapper mapper = session.getMapper(studentMapper.class);
  16. Student s = mapper.testGet(14);
  17. System.out.println(s);
  18. session.close();
  19. }
  20. }

Test2.java

  1. package com.zhengbin.util;
  2.  
  3. import java.io.InputStream;
  4.  
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7.  
  8. public class MyBatisUtils {
  9. public static SqlSessionFactory getFactory(){
  10. String resource = "conf.xml";
  11. InputStream is = MyBatisUtils.class.getClassLoader().getResourceAsStream(resource);
  12. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
  13. return sessionFactory;
  14. }
  15. }

MyBatisUtils.java

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <!-- 数据库配置文件 -->
  5. <properties resource="jdbc.properties"/>
  6. <!-- 取别名 -->
  7. <typeAliases>
  8. <!-- 方式一、为类取别名 -->
  9. <!-- <typeAlias type="com.zhengbin.entity.Student" alias="_Student"/> -->
  10. <!-- 方式二、自动以类名为别名 -->
  11. <package name="com.zhengbin.entity"/>
  12. </typeAliases>
  13. <!--
  14. development : 开发模式
  15. work : 工作模式
  16. -->
  17. <environments default="development">
  18. <environment id="development">
  19. <transactionManager type="JDBC" />
  20. <dataSource type="POOLED">
  21. <property name="driver" value="${driver}" />
  22. <property name="url" value="${url}" />
  23. <property name="username" value="${username}" />
  24. <property name="password" value="${password}" />
  25. </dataSource>
  26. </environment>
  27. </environments>
  28. <mappers>
  29. <!-- 这是一个路径的结构,不是包的结构 -->
  30. <mapper resource="com/zhengbin/entity/studentMapper.xml"/>
  31. <mapper class="com.zhengbin.entity2.studentMapper"/>
  32. </mappers>
  33. </configuration>

conf.xml

  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3307/student
  3. username=root
  4. password=950906

jdbc.properties

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
  3. <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  4. <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
  5. <layout class="org.apache.log4j.PatternLayout">
  6. <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
  7. </layout>
  8. </appender>
  9. <logger name="java.sql">
  10. <level value="debug" />
  11. </logger>
  12. <logger name="org.apache.ibatis">
  13. <level value="debug" />
  14. </logger>
  15. <root>
  16. <level value="debug" />
  17. <appender-ref ref="STDOUT" />
  18. </root>
  19. </log4j:configuration>

log4j.xml

  4.遇到的问题

(1)奇怪的junit:如果新建一个class,名为Test则可能出现不能使用注解@Test的情况,此时用@org.junit.Test即可,或者更改class名,不以Test开头 好奇葩

(2)实体类必须要加superclass,否则报错,因为MyBatis无法通过配置文件找到实体类

Mybatis学习——基本增删改查(CRUD)的更多相关文章

  1. 基于SSM之Mybatis接口实现增删改查(CRUD)功能

    国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...

  2. Mybatis实现简单增删改查

    Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...

  3. IDEA SpringBoot-Mybatis-plus 实现增删改查(CRUD)

    上一篇: IDEA SpringBoot-Mybatis实现增删改查(CRUD) 下一篇:Intellij IDEA 高效使用教程 (插件,实用技巧) 最好用的idea插件大全 一.前言 Mybati ...

  4. MyBatis简单的增删改查以及简单的分页查询实现

    MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...

  5. Mybatis入门之增删改查

    Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...

  6. MyBatis -- 对表进行增删改查(基于注解的实现)

    1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1  首先须要定义映射sql的 ...

  7. Spring Boot 使用Mybatis注解开发增删改查

    使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...

  8. idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD

    在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下. 原文在这里:https://my.oschina.net/finchxu/bl ...

  9. ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)

    ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...

随机推荐

  1. Sqlitekit 封装管理

    最近需要用到Sqlite数据库来做一个游戏的数据存储.网上搜了一下,两种方法,一种是自己dll搭建环境有可能还需要编译之类的,我自己是搭建出来了,不过我没采用. 还有一种就是使用sqlitekit插件 ...

  2. 深入浅出ES6(三):生成器 Generators

    作者 Jason Orendorff  github主页  https://github.com/jorendorff ES6生成器(Generators)简介 什么是生成器? 我们从一个示例开始: ...

  3. C# 计算一段代码执行的时间函数

    使用 Stopwatch 类 eg:  计算一个for循环需要的时间 Stopwatch watch = new Stopwatch(); watch.Start(); ; i < ; i++) ...

  4. Thread的第三天学习

    线程互斥 public void method1() {  synchronized(this) {  ... } } 等同于 public synchronized void method1() { ...

  5. Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)

    1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...

  6. 查看mininet交换机中的流表

    官网文档http://mininet.org/walkthrough/#xterm-display Xterms are also useful for running interactive com ...

  7. Protege A DOT error has occurred错误

    问题参生的原因:graphviz没有安装或者,没有配置好 解决方法: 1.下载graphviz,这里是百度软件下载的,在官网下载需要注册账户,麻烦 2.安装graphviz,找到下面的路径. 3.设置 ...

  8. 欧拉工程第62题:Cubic permutations

    题目链接 找出最小的立方数,它的各位数的排列能够形成五个立方数 解决关键点: 这五个数的由相同的数组成的 可以用HashMap,Key是由各位数字形成的key,value记录由这几个数组成的立方数出现 ...

  9. 我30天在Stack Overflow问答网站上回答问题的感受

    想法的萌芽 如果非要总结下我多年来是如何使用Stack Overflow的话,我的答案就是:打开网页,搜索问题,查看Stack Overflow的搜索结果,参考答案,最后再关掉网页. 我的生活已经离不 ...

  10. WCF入门(四)---WCF架构

    WCF是一个分层架构,为开发各种分布式应用的充分支持.该体系结构在下面将详细说明. 约定 约定层旁边就是应用层,并含有类似于现实世界的约定,指定服务和什么样的信息可以访问它会使操作的信息.约定基本都是 ...