Mybatis实现CURD代码篇
来源:互联网 发布:网络小胖学生表情包 编辑:程序博客网 时间:2024/06/05 22:59
public class Student {
}创建学生信息类 我这里省略了很多
private Integer id;
private String name;
private int age;
private double score;
public interface IStudentDao { 创建
void insertStudent(Student student);增加学生void insertStudentCatchId(Student student);增加学生id
void deleteById(int id); 删除id
void updateStudent(Student student); 更新学生信息
List<Student> selectAllStudent(); 查询学生信息list
Map<Object, Student> selectAllStudentMap(); 查询学生信息map
Student selectStudentById(int id); 通过id 查询学生
List<Student> selectStudentsByName(String name) 通过名字查询;
List<Student> selectStudentsByCondition(Student student); 封装学生查询模式
List<Student> selectStudentsByCondition2(Map<String, Object> map);封装map查询模式
List<Student> selectStudentsByCondition3(String name, int age);通过name age属性查询
}
实现接口
package com.abc.dao;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import com.abc.beans.Student;
import com.abc.utils.MybatisUtil;
public class StudentDaoImpl implements IStudentDao {
@Override
public void insertStudent(Student student) {
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
session.insert("insertStudent", student);
// 3. SqlSession的提交
session.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. SqlSession的关闭
if (session != null) {
session.close();
}
}
}
@Override
public void insertStudentCatchId(Student student) {
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
session.insert("insertStudentCatchId", student);
// 3. SqlSession的提交
session.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. SqlSession的关闭
if (session != null) {
session.close();
}
}
}
@Override
public void deleteById(int id) {
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
session.delete("deleteById", id);
// 3. SqlSession的提交
session.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. SqlSession的关闭
if (session != null) {
session.close();
}
}
}
@Override
public void updateStudent(Student student) {
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
session.update("updateStudent", student);
// 3. SqlSession的提交
session.commit();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4. SqlSession的关闭
if (session != null) {
session.close();
}
}
}
@Override
public List<Student> selectAllStudent() {
List<Student> students = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
students = session.selectList("selectAllStudent");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return students;
}
@Override
public Map<Object, Student> selectAllStudentMap() {
Map<Object,Student> map = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
map = session.selectMap("selectAllStudent", "name");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return map;
}
@Override
public Student selectStudentById(int id) {
Student student = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
student = session.selectOne("selectStudentById", id);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return student;
}
@Override
public List<Student> selectStudentsByName(String name) {
List<Student> students = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
students = session.selectList("selectStudentsByName", name);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return students;
}
@Override
public List<Student> selectStudentsByCondition(Student student) {
List<Student> students = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
students = session.selectList("selectStudentsByCondition", student);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return students;
}
@Override
public List<Student> selectStudentsByCondition2(Map<String, Object> map) {
List<Student> students = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
students = session.selectList("selectStudentsByCondition2", map);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return students;
}
@Override
public List<Student> selectStudentsByCondition3(String name, int age) {
List<Student> students = null;
SqlSession session = null;
try {
// 1. 创建SqlSession对象
session = MybatisUtil.getSession();
// 2. 执行命令
// students = session.selectList("selectStudentsByCondition3", map);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 3. SqlSession的关闭
if (session != null) {
session.close();
}
}
return students;
}
}
} 个别方法相同
<?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="xxx">
<insert id="insertStudent">
insert into student(name,age,score) values(#{name}, #{age}, #{score})
</insert>
<insert id="insertStudentCatchId">
insert into student(name,age,score) values(#{name}, #{age}, #{score})
<selectKey resultType="int" keyProperty="id" order="AFTER">
<!-- select @@identity -->
select last_insert_id()
</selectKey>
</insert>
<delete id="deleteById">
delete from student where id=#{xxx}
</delete>
<update id="updateStudent">
update student set name=#{name}, age=#{age}, score=#{score} where id=#{id}
</update>
<!-- resultType与resultMap指的是查询出的每一条记录所要封闭的对象类型,并非查询的结果集类型 -->
<select id="selectAllStudent" resultType="Student">
select id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age,score from student where id=#{ooo}
</select>
<select id="selectStudentsByName" resultType="Student">
<!-- select * from student where name like '%张%' -->
select id,name,age,score from student where name like '%' #{jjj} '%'
<!-- select id,name,age,score from student where name like concat('%', #{jjj}, '%') -->
<!-- select id,name,age,score from student where name like '%${value}%' -->
</select>
<select id="selectStudentsByCondition" resultType="Student">
select id,name,age,score from student where name like '%' #{name} '%' and age < #{age}
</select>
<select id="selectStudentsByCondition2" resultType="Student">
<!-- select id,name,age,score from student where name like '%' #{cname} '%' and age < #{cage} -->
select id,name,age,score from student where name like '%' #{stu1.name} '%' and age < #{stu2.age}
</select>
</mapper>
ps 很多sql语句得会
package com.abc.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import com.abc.beans.Student;
import com.abc.dao.IStudentDao;
import com.abc.dao.StudentDaoImpl;
public class MyTest {
IStudentDao dao = new StudentDaoImpl();
@Before
public void before() {
dao = new StudentDaoImpl();
}
@Test
public void test01() {
for (int i = 1; i < 11; i++) {
Student student = new Student("学生" + i, 15 + i, 85 + i);
dao.insertStudent(student);
}
}
@Test
public void test02() {
Student student = new Student("p", 10, 22);
System.out.print("插入前" + student);
dao.insertStudentCacheId(student);
System.out.print("插入后" + student);
}
@Test
public void test03() {
dao.deleteById(44);
}
@Test
public void test04() {
Student student = new Student("zz", 15, 88);
student.setId(33);
dao.updateStudent(student);
}
@Test
public void test05() {
List<Student> students = dao.selectAllstudent();
for (Student student : students) {
System.out.println(student + "/t" + "么么");
}
}
@Test
public void test06() {
Student student = dao.selectById(400);
System.out.println(student);
}
@Test
public void test07() {
List<Student> name = dao.selectStudentByName("p");
for (Student student : name) {
System.out.println(student + "t" + "么么");
}
}
@Test
public void test08() {
Student students = new Student("p",11,1);
List<Student> name = dao.selectStudentCondition(students);
for (Student student : name) {
System.out.println(student + "t" + "么么");
}
}
@Test
public void test09() {
Student students1 = new Student("p",12,1);
Student students2 = new Student(null,11,0);
Map<String, Object> map = new HashMap<String, Object>();
map.put("students1", students1);
map.put("students2", students2);
List<Student> name = dao.selectStudentCondition2(map);
for (Student student : name) {
System.out.println(student + "t" + "么么");
}
}
}
Student [id=857, name=p, age=10, score=22.0]t么么
Student [id=868, name=p, age=10, score=22.0]t么么
Student [id=879, name=p, age=10, score=22.0]t么么
Student [id=890, name=p, age=10, score=22.0]t么么
Student [id=901, name=p, age=10, score=22.0]t么么
ps 结果太多,新手表示csdn编辑体验好差,直接上不了截图???
- Mybatis实现CURD代码篇
- mybatis 实现 curd总结
- Mybatis注解方式实现CURD
- MyBatis配置及实现CURD操作
- MyBatis 配置及实现 CURD 操作
- Mybatis实现数据CURD操作实例
- Mybatis实现数据CURD操作实例--续
- mybatis curd
- 一行代码实现FMDB的CURD操作
- MyBatis实现单表增删改查(CURD)--新增
- MyBatis实现单表增删改查(CURD)--删除
- MyBatis实现单表增删改查(CURD)--修改
- mybatis入门之CURD
- Mybatis curd操作
- Maven 构建 Struts2 实现 基本 CURD Controller层代码
- mybatis的基础curd操作
- mybatis进行简单的CURD
- MyBatis 分页代码实现
- JavaScript高级程序设计笔记(2)_在HTML中使用js
- 第八周作业 ——猫狗叫(继承)
- Linux操作系统与实训教程实验二
- SVN
- JDBC操作数据库的步骤
- Mybatis实现CURD代码篇
- 从零开始实现朴素贝叶斯分类算法(离散特征情形)
- CF878C,图论与数据结构
- Machine Learning 之 LOF离群点检验
- python什么是高阶函数
- HDU 4307:Matrix
- Linux查看系统开机时间
- 基于Hadoop的第二个分布式算法Day4
- ps 阔值抠图 帮助一个爱好ps的妹子