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 ...
随机推荐
- 【Hibernate总结系列】....hbm.xml配置
在Hibernate中,各表的映射文件….hbm.xml可以通过工具生成,例如在使用MyEclipse开发时,它提供了自动生成映射文件的工具.本节简单的讲述一下这些配置文件的配置. 配置文件的基本结构 ...
- 集合上的动态规划---最优配对问题(推荐:*****) // uva 10911
/* 提醒推荐:五星 刘汝佳<算法竞赛入门经典>,集合上的动态规划---最优配对问题 题意:空间里有n个点P0,P1,...,Pn-1,你的任务是把它们配成n/2对(n是偶数),使得每个点 ...
- Oracle composite index column ordering
Question: I have a SQL with multiple columns in my where clause. I know that Oracle can only choos ...
- Intent(二)
以Android高级编程一书中的一个例子为例: 1, 创建一个ContactPicker项目,其中包含一个ContactPicker Activity package com.paad.contact ...
- lintcode :二叉树的最大深度
题目: 二叉树的最大深度 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的距离. 样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3. 解 ...
- 用JUnit4进行单元测试
转载:http://tonl.iteye.com/blog/1948869 参考: http://thihy.iteye.com/blog/1771826 http://developer.51cto ...
- Lumina将是基于 Qt工具箱,旨在取代KDE成为PC-BSD默认的桌面环境
Lumina Desktop 1.1.0 发布了,该版本是重要更新,包括全新的以及完全重新编写的utilities,并对底层基础架构进行改进. Lumina将是基于 Qt工具箱,旨在取代KDE成为PC ...
- 1401 - Remember the Word
注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...
- PCL—低层次视觉—点云分割(邻近信息)
分割给人最直观的影响大概就是邻居和我不一样.比如某条界线这边是中华文明,界线那边是西方文,最简单的分割方式就是在边界上找些居民问:"小伙子,你到底能不能上油管啊?”.然后把能上油管的居民坐标 ...
- MyEclipse 2014 + JSP+ Servlet
来自:http://blog.csdn.net/21aspnet/article/details/21867241 1.安装准备 1).下载安装MyEclipse2014,这已经是最新版本. 2).下 ...