原则:约定优于配置。
- 硬编码方式:*.java
Configuration conf = new Configuration(); conf.setName("myProject");
- 配置方式:*.xml
<name>myProject</name>
- 约定方式:默认值name为myProject
实现步骤
约定的目标:省略掉statement,即根据约定可以直接定位出SQL语句。
- 新建一个接口,用于操作MyBits,接口中的方法必须遵循以下约定:
- 方法名和mapper.xml文件中标签的id值一样
- 方法的输入参数与mapper.xml文件中标签的parameterType类型一致
- 方法的返回值和mapper.xml文件中标签的resultType类型一致
- 除了以上约定,要实现接口中方法和Mapper.xml中SQL标签----对应,还需要以下1点:
- namespace的值,就是接口的全类名(接口-----------mapper.xml一一对应)
- 匹配(约定)的过程:
- 根据接口名找到mapper.xml文件(根据的是namespace=接口全类名)
- 根据接口的方法名找到mapper.xml文件中的SQL标签(方法名=SQL标签的ID值)
- 以上两点可以保证:当我们掉用接口时,程序能自动定位到某一个Mapper.xml文件中的SQL标签
习惯中将mapper.xml文件和接口放在同一个包下
StudentMapper.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="com.gub.mapper.StudentMapper">
<select id="queryStudentById" resultType="com.gub.vo.Student" parameterType="int">
SELECT * FROM student WHERE sid = #{sid}
</select>
<insert id="insertStudent" parameterType="com.gub.vo.Student" >
INSERT INTO student (name,age,graname) VALUES (#{name},#{age},#{graname});
</insert>
<delete id="deleteStudentById" parameterType="int">
DELETE FROM student WHERE sid=#{sid}
</delete>
<update id="updateStudentById" parameterType="com.gub.vo.Student" >
UPDATE student SET name=#{name},age=#{age},graname=#{graname} WHERE sid=#{sid}
</update>
<select id="querySelectAll" resultType="com.gub.vo.Student">
SELECT * FROM Student
</select>
</mapper>
StudentMapper接口:
package com.gub.mapper;
import com.gub.vo.Student;
import java.util.List;
//操作MyBits的接口
public interface StudentMapper {
public Student queryStudentById(int id);
public void insertStudent(Student student);
public void deleteStudentById(int sid);
public void updateStudentById(Student student);
public List<Student> querySelectAll();
}
测试根据ID查询:
package com.gub.test;
import com.gub.mapper.StudentMapper;
import com.gub.vo.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
public class TestStudent {
public static void main(String[] args) throws IOException {
Reader reader = Resources.getResourceAsReader("conf.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
//取得mapper接口
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//根据ID查询
Student student = studentMapper.queryStudentById(1);
System.out.println(student);
sqlSession.commit();
sqlSession.close();
}
}
测试查询全部:
//取得mapper接口
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//查询全部数据
List<Student> students = studentMapper.querySelectAll();
测试增加:
Student student = new Student();
student.setName("hasaka");
student.setAge(18);
student.setGraname("six six six");
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//增加数据
studentMapper.insertStudent(student);
sqlSession.commit();
测试修改:
//取得mapper接口
Student student = new Student();
student.setSid(5);
student.setName("CN");
student.setAge(18);
student.setGraname("six six six");
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//修改数据
studentMapper.updateStudentById(student);
sqlSession.commit();
测试删除:
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
//删除数据
studentMapper.deleteStudentById(5);
sqlSession.commit();