PS:此程序由上一个程序改写,代码的实现参考动力节点网课
步骤分别为:
1.Dao接口的修改
2.表和学术类的修改
3.映射文件的修改
4.Dao实现类的修改
5.在测试类进行测试
1.Dao接口
package rodisland;
import java.util.List;
import java.util.Map;
public interface Dao
{
void insertStudent(Student student); //插入学生信息
void insertStudentCatchId(Student student); //插入学生信息并获取ID
void deleteStudentById(int id); //删除
void updateStudent(Student student,int id); //改写
List<Student> selectAllStudents(); //获取信息列表
Map<String,Student> selectStudentMap(); //获取信息映射图
Student selectStudentById(int id); //通过ID获取学生信息
Student selectStudentByMap(Map<String,Object> map); //通过表中信息获取学生信息
List<Student> selectStudentByName(String name); //通过姓名获取一组学生信息
}
2.表的修改
在第一个程序的时候,没有使用id作为主键,需要对表进行修改操作
在mysqlWokrBench中对表student进行修改
在altertable中,添加di栏,长度自定义,在PK(主键) NN(非空) AI(自动递增)打上勾
同时记得修改student类的成员变量,否则报错。
3.mapper.xml 映射文件的修改
<?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="test">
<!--insert Student info -->
<insert id="insertStu">
insert into student(name,age,num)
values(#{name},#{age},#{num})
</insert>
<!-- insert student info and get id -->
<insert id="insertStudentCatchId">
insert into student(name,age,num)
values(#{name},#{age},#{num})
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@identity
</selectKey>
</insert>
<!-- delete student info -->
<delete id="deleteStudentById">
delete from student where id=#{id}
</delete>
<!-- Update student info by id -->
<update id="updateStudent">
update student
set name=#{name},age=#{age},num=#{num}
where id=#{id}
</update>
<!-- selectAllStudents by List or Map-->
<select id="selectAllStudents" resultType="Student">
select * from student
</select>
<!--select only one student by id -->
<select id="selectStudentById" resultType="Student">
select * from student where id=#{id}
</select>
<!-- select one by name [fuzzy query] -->
<select id="selectStudentByName" resultType="Student">
select * from student where name like concat('%',#{name},'%')
<!--
don't use
[select * from student where name like '%${value}%']
could cause injection attacks
-->
</select>
<!-- form a map select a student -->
<select id="selectStudentByMap" resultType="Student">
select * from student where id=#{StudentId}
</select>
<select id="selectStudentByMap2" resultType="Student">
select * from student where id=#{student.id}
</select>
</mapper>
说明:每一个语句的功能看上方的英文注释
resultType为查询结果的强制类型转化,可以使用全包名,也可以使用别名(在配置文件中声明别名)
此处使用了别名,配置片段如下:
<typeAliases>
<typeAlias type="rodisland.Student" alias="Student"/>
</typeAliases>
特殊情况,若类的属性名与表的字段名不同,可以在mapper.xml文件中使用resultMap,也可以使用sql语句起别名的方法进行查询
<!-- no same name example -->
<select id="selectStudentById2" resultType="Student">
select tid id,tname name,tage age,num from student where tid=#{id}
</select>
<!-- use resultMap -->
<resultMap type="Student" id="studentMapper">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
<result column="tage" property="age"/>
</resultMap>
<select id="selectStudentById3" resultMap="studentMapper">
select tid,tname,tage,num
from student where tid=#{id}
</select>
4.实现Dao接口
package rodisland;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
public class StudentDaoImp implements Dao
{
private SqlSession session;
@Override
public void insertStudent(Student student)
{
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
session.insert("insertStu",student);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
}
public void insertStudentCatchId(Student student)
{
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
session.insert("insertStudentCatchId",student);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
};
public void deleteStudentById(int id)
{
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
session.delete("deleteStudentById",id);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
};
public void updateStudent(Student student,int id)
{
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
student.setId(id);
session.update("updateStudent",student);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
};
public List<Student> selectAllStudents()
{
List<Student> studentList=null;
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
studentList=session.selectList("selectAllStudents");
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
return studentList;
};
public Map<String,Student> selectStudentMap()
{
Map<String,Student> studentMap=null;
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
studentMap=session.selectMap("selectAllStudents","num");
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
return studentMap;
};
public Student selectStudentById(int id)
{
Student student=null;
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
student=session.selectOne("selectStudentById",id);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
return student;
};
public List<Student> selectStudentByName(String name)
{
List<Student> students=null;
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
students=session.selectList("selectStudentByName",name);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
return students;
};
public Student selectStudentByMap(Map<String,Object> map)
{
Student student=null;
try
{
session=MyBatisUtil.getSqlSession();
//4.操作
student=session.selectOne("selectStudentByMap2",map);
//5.提交事务
session.commit();
}finally
{
//6.关闭session
if(session!=null)
session.close();
}
return student;
};
}
5.在Junit测试类进行测试
错误分析:
在使用Junit进行测试的时候,发生了两次java.lang.Exception: No tests found matching的错误
第一次是没有使用@Test标签
第二次是因为没有及时保存MyTest类