Mybatis学习——基本增删改查(CRUD)
Eclipse+Mybatis+MySql
1.所需jar

2.项目目录

3.源代码
package com.zhengbin.entity;
public class Student {
private int id;
private String name;
private double score;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", score=" + score + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student(int id, String name, double score) {
super();
this.id = id;
this.name = name;
this.score = score;
}
// *注意这个必须加
public Student() {
super();
}
}
Student.java
<?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">
<!-- 每个映射文件的namespace应该是唯一的 -->
<mapper namespace="com.zhengbin.entity.studentMapper">
<!-- parameterType 参数表示需要参数的类型 -->
<!-- resultType 参数表示返回结果的类型,该可以写为实体包的全路径,或者在conf.xml配置文件中,声明实体的别名 -->
<select id="getStudent" parameterType="int" resultType="Student">
select * from student where id=#{id}
</select> <select id="getAllStudent" resultType="Student">
select * from student
</select> <insert id="addStudent" parameterType="Student">
insert into student(name,score) values(#{name},#{score})
</insert> <update id="updateStudent" parameterType="Student">
update student set name=#{name},score=#{score} where id=#{id}
</update> <delete id="deleteStudent" parameterType="int">
delete from student where id=#{id}
</delete>
</mapper>
studentMapper.xml
package com.zhengbin.entity2;
import org.apache.ibatis.annotations.Select;
import com.zhengbin.entity.Student;
public interface studentMapper {
@Select("select * from student where id=#{id}")
public Student testGet(int id);
}
studentMapper.java
package com.zhengbin.test; import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import com.zhengbin.entity.Student;
import com.zhengbin.util.MyBatisUtils; public class Test {
@org.junit.Test
public void testGet(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
// 参数为TRUE,相当于session.commit();
SqlSession session = sessionFactory.openSession(true);
// 读取映射文件
String statement = "com.zhengbin.entity.studentMapper" + ".getStudent";
Student s = session.selectOne(statement,5);
System.out.println(s);
session.close();
} @org.junit.Test
public void testGetAll(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
// 参数为TRUE,相当于session.commit();
SqlSession session = sessionFactory.openSession(true);
// 读取映射文件
String statement = "com.zhengbin.entity.studentMapper" + ".getAllStudent";
List<Student> list = session.selectList(statement);
System.out.println(list);
session.close();
} @org.junit.Test
public void testAdd(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
SqlSession session = sessionFactory.openSession(true);
String statement = "com.zhengbin.entity.studentMapper" + ".addStudent";
Student s = new Student();
s.setName("zhengB");
s.setScore(95.9);
int insert = session.insert(statement, s);
System.out.println(insert);
session.close();
} @org.junit.Test
public void testUpdate(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
SqlSession session = sessionFactory.openSession(true);
String statement = "com.zhengbin.entity.studentMapper" + ".updateStudent";
Student s = new Student();
s.setId(14);
s.setName("zhengbin");
s.setScore(96);
int update = session.update(statement, s);
System.out.println(update);
session.close();
} @org.junit.Test
public void testDelete(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
SqlSession session = sessionFactory.openSession(true);
String statement = "com.zhengbin.entity.studentMapper" + ".deleteStudent";
int delete = session.delete(statement,21);
System.out.println(delete);
session.close();
}
}
Test.java
package com.zhengbin.test; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory; import com.zhengbin.entity.Student;
import com.zhengbin.entity2.studentMapper;
import com.zhengbin.util.MyBatisUtils; public class Test2 {
@org.junit.Test
public void testGet(){
SqlSessionFactory sessionFactory = MyBatisUtils.getFactory();
SqlSession session = sessionFactory.openSession(true);
studentMapper mapper = session.getMapper(studentMapper.class);
Student s = mapper.testGet(14);
System.out.println(s);
session.close();
}
}
Test2.java
package com.zhengbin.util; import java.io.InputStream; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtils {
public static SqlSessionFactory getFactory(){
String resource = "conf.xml";
InputStream is = MyBatisUtils.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
return sessionFactory;
}
}
MyBatisUtils.java
<?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>
<!-- 数据库配置文件 -->
<properties resource="jdbc.properties"/>
<!-- 取别名 -->
<typeAliases>
<!-- 方式一、为类取别名 -->
<!-- <typeAlias type="com.zhengbin.entity.Student" alias="_Student"/> -->
<!-- 方式二、自动以类名为别名 -->
<package name="com.zhengbin.entity"/>
</typeAliases>
<!--
development : 开发模式
work : 工作模式
-->
<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 resource="com/zhengbin/entity/studentMapper.xml"/>
<mapper class="com.zhengbin.entity2.studentMapper"/>
</mappers>
</configuration>
conf.xml
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3307/student
username=root
password=950906
jdbc.properties
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
</layout>
</appender>
<logger name="java.sql">
<level value="debug" />
</logger>
<logger name="org.apache.ibatis">
<level value="debug" />
</logger>
<root>
<level value="debug" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
log4j.xml
4.遇到的问题
(1)奇怪的junit:如果新建一个class,名为Test则可能出现不能使用注解@Test的情况,此时用@org.junit.Test即可,或者更改class名,不以Test开头 好奇葩
(2)实体类必须要加superclass,否则报错,因为MyBatis无法通过配置文件找到实体类
Mybatis学习——基本增删改查(CRUD)的更多相关文章
- 基于SSM之Mybatis接口实现增删改查(CRUD)功能
国庆已过,要安心的学习了. SSM框架以前做过基本的了解,相比于ssh它更为优秀. 现基于JAVA应用程序用Mybatis接口简单的实现CRUD功能: 基本结构: (PS:其实这个就是用的Mapper ...
- Mybatis实现简单增删改查
Mybatis的简单应用 学习内容: 需求 环境准备 代码 总结: 学习内容: 需求 使用Mybatis实现简单增删改查(以下是在IDEA中实现的,其他开发工具中,代码一样) jar 包下载:http ...
- IDEA SpringBoot-Mybatis-plus 实现增删改查(CRUD)
上一篇: IDEA SpringBoot-Mybatis实现增删改查(CRUD) 下一篇:Intellij IDEA 高效使用教程 (插件,实用技巧) 最好用的idea插件大全 一.前言 Mybati ...
- MyBatis简单的增删改查以及简单的分页查询实现
MyBatis简单的增删改查以及简单的分页查询实现 <? xml version="1.0" encoding="UTF-8"? > <!DO ...
- Mybatis入门之增删改查
Mybatis入门之增删改查 Mybatis如果操作成功,但是数据库没有更新那就是得添加事务了.(增删改都要添加)----- 浪费了我40多分钟怀疑人生后来去百度... 导入包: 引入配置文件: sq ...
- MyBatis -- 对表进行增删改查(基于注解的实现)
1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1 首先须要定义映射sql的 ...
- Spring Boot 使用Mybatis注解开发增删改查
使用逆向工程是遇到的错误 错误描述 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): c ...
- idea+spring4+springmvc+mybatis+maven实现简单增删改查CRUD
在学习spring4+springmvc+mybatis的ssm框架,idea整合简单实现增删改查功能,在这里记录一下. 原文在这里:https://my.oschina.net/finchxu/bl ...
- ssm 框架实现增删改查CRUD操作(Spring + SpringMVC + Mybatis 实现增删改查)
ssm 框架实现增删改查 SpringBoot 项目整合 一.项目准备 1.1 ssm 框架环境搭建 1.2 项目结构图如下 1.3 数据表结构图如下 1.4 运行结果 二.项目实现 1. Emplo ...
随机推荐
- JavaScript高级---桥模式设计
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- LoadRunner 11 安装及破解(转)
前提条件: 内存:2G,硬盘空闲空间10G,安装完成后实际只占不到2G 支持winXP SP3;32位与64位win7浏览器支持IE6-8,IE9,firefox3 若以前安装过LoadRunner ...
- jstl表达式使用方法
1.jstl.jar servlet.jar支持 2.jsp引入标签库 <%@ taglib prefix="c" uri="http://java.sun.co ...
- Mac OS X 快捷键(完整篇) 转载
转载自:http://www.nooidea.com/2011/01/mac-os-x-keyboard-shortcuts.html 快捷键是通过按下键盘上的组合键来调用 Mac OS X 功能的一 ...
- hdu 2717 Catch That Cow(BFS,剪枝)
题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...
- 用 Xamarin for VS 创建 aar 文件的绑定
预备工作:相关aar文件,Xamarin for VS一份.我这里以Android中挺火的 MaterialDesignLibrary 为例. 1.首先,创建一个Xamarin Binding Lib ...
- oracle的全文索引
1.查看oracle的字符集 SQL> select userenv('language') from dual; USERENV('LANGUAGE') ------------------- ...
- cojs 自己出的题目 解题报告
省选成功成为河北B队队长QAQ 真是忧桑 所以在cojs上出了一套鬼畜的关于树的套题 黑白树: 我们先不考虑R操作 设x是u的祖先,那么fa(x)的贡献显然是 fa(x)*(sz(fa(x))-sz( ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- Delphi 7事件的多处理机制
Delphi 7事件的多处理机制Allen Tao2007-08-19 首先解释一下这个题目.在我使用Delphi 7的过程中发现,一个对象的事件只能被一个过程处理.如果多次给这个对象的事件赋给处理事 ...