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 &lt; #{age}
</select>

<select id="selectStudentsByCondition2" resultType="Student">
<!-- select id,name,age,score from student where name like '%' #{cname} '%' and  age &lt; #{cage} -->
select id,name,age,score from student where name like '%' #{stu1.name} '%' and  age &lt; #{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编辑体验好差,直接上不了截图???















原创粉丝点击