一、SqlSession单例模式类
package cn.mybatis.Sql;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* SqlSessionFactory,单例模式
* @author cjc
*
*/
public class MyBatisSqlSessionFactory {
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory == null){
InputStream inputStream;
try {
//使用 mybatis-config.xml 内的信息创建了 SqlSessionFactory 对象
inputStream = Resources.getResourceAsStream("config/mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return sqlSessionFactory;
}
public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}
二、实体类Student
package cn.mybatis.entity;
import java.util.Date;
public class Student {
private Integer studId;
private String name;
private String email;
private Date dob;
public Integer getStudId() {
return studId;
}
public void setStudId(Integer studId) {
this.studId = studId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
@Override
public String toString() {
return "Student [studId=" + studId + ", name=" + name + ", email="
+ email + ", dob=" + dob + "]";
}
}
三、StudentMapper.xml 配置文件
<mapper namespace="cn.mybatis.mapperInterface.StudentMapper">
<resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="dob" column="dob" />
resultMap>
<select id="findAllStudents" resultMap="StudentResult">
SELECT * FROM STUDENTS
select>
<select id="findStudentById" parameterType="int" resultType="Student">
SELECT STUD_ID AS STUDID, NAME, EMAIL, DOB
FROM STUDENTS WHERE STUD_ID=#{Id}
select>
<insert id="insertStudent" parameterType="Student">
INSERT INTO STUDENTS(STUD_ID,NAME,EMAIL,DOB)
VALUES(#{studId },#{name},#{email},#{dob})
insert>
mapper>
四、StudentMapper.java 接口
package cn.mybatis.mapperInterface;
import java.util.List;
import cn.mybatis.entity.Student;
/**
* 在ssm框架中,
* @author cjc
*
*/
public interface StudentMapper {
List findAllStudents();
Student findStudentById(Integer id);
void insertStudent(Student student);
}
第一种方法:
前提:StudentMapper.xml的命名空间namespace要等于StudentMapper.java的全类限定名;StudentMapper.xml的SQL语句的id要和StudentMapper.java的方法名一致、方法参数类型为 parameterType 对应值、方法返回值类型为 returnType 对应值
例如对findAllStudents进行对比,在第一种方法中,业务实现是这样的:
public List findAllStudents() {
SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
try{
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
return studentMapper.findAllStudents();
}finally{
sqlSession.close();
}
}
我们都知道接口是不能直接实例化的,但是注意到StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
实例化了StudentMapper,使得业务实现可以通过studentMapper.findAllStudents();
来执行StudentMapper.xml里面id为findAllStudents的SQL语句。
第二种方法:
第二种方法不通过StudentMapper.java这个接口。自然,不通过这个接口,我们的StudentMapper.xml的namespace就没有说要与StudentMapper.java这个接口的全类限定名一致,因为在第二种方法中,这个接口可以完全删去,那怎样才能执行StudentMapper.xml的对应id的SQL语句呢?
public List findAllStudents() {
SqlSession sqlSession = MyBatisSqlSessionFactory.openSession();
try{
List studentList = sqlSession.selectList("cn.mybatis.mapperInterface.StudentMapper.findAllStudents");
return studentList;
}finally{
sqlSession.close();
}
}
与第一种方法通过sqlSession.getMapper(StudentMapper.class)
方法不同的是,第二种方法是通过sqlSession.selectList("cn.mybatis.mapperInterface.StudentMapper.findAllStudents");
(其中”cn.mybatis.mapperInterface.StudentMapper.findAllStudents”是StudntMapper.xml的命名空间+SQL id)来直接执行StudentMapper.xml的SQL语句的
这里值得一提的是,双引号里面执行的是什么SQL操作,sqlSession就要调用什么方法,如图:
在进行sqlSession的insert操作的时候,要记得sqlSession.commit()操作