SSM框架实战详细教程(七)MyBatis环境搭建与基本操作

        回顾一下JDBC,如下图,为了实现一个数据库表的查询,我们要完成JDBC六个步骤,其中对于查询有大量的工作是将二维表的数据整理成面向对象的List或实体类对象。
SSM框架实战详细教程(七)MyBatis环境搭建与基本操作_第1张图片
        MyBatis技术的推出正是为了将Java和SQL相分离,MyBatis解决了模板型代码的问题。下面搭建MyBatis环境,因为MyBatis是一个持久层框架,学习中不比搭建网站,可以建立一个Maven的quickStart类型的项目,由main方法驱动测试,方便高效。
项目结构图:
SSM框架实战详细教程(七)MyBatis环境搭建与基本操作_第2张图片
pom.xml:

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.0version>
        dependency>

mybatisConfig.xml配置数据库链接与指定Mapper文件:


DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url"
					value="jdbc:mysql://localhost:3306/school?characterEncoding=UTF-8" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			dataSource>
		environment>
	environments>

	<mappers>
		<mapper resource="mapper/StudentDao.xml" />

	mappers>
configuration>

StudentDao.xml文件是StudentDao.java映射,在里面编写SQL,实现StudentDao接口中的方法:


DOCTYPE mapper PUBLIC
        "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.StudentDao">
    <select id="searchAll" resultType="entity.Student">
    select * from student
    select>
mapper> 
    

StudentDao.java 接口中定义方法,通过方法名的形式,定义方法功能,具体实现逻辑在以上的Mapper文件中。

package dao;

import entity.Student;

import java.util.List;

public interface StudentDao {
    List<Student> searchAll();
}

MybatisSqlSession.java文件,作用是读取配置文件,生成SqlSession对象(该文件将会在SSM框架搭建后失去作用,其作用改由Spring完成):

package test;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MybatisSqlSession {

	public static SqlSession sqlSession = null;
	public static SqlSessionFactory sqlSessionFactory = null;
	static {

		String resource = "mybatisConfig.xml";
		Reader reader = null;
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);

	}

	public static SqlSession getSqlSession() {
		sqlSession = sqlSessionFactory.openSession();
		return sqlSession;
	}
}

新建一个Test类来测试:

package test;

import dao.StudentDao;
import entity.Student;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class Test {
    public static void main(String[] args) {
        SqlSession session = MybatisSqlSession.getSqlSession();
        StudentDao studentDao = session.getMapper(StudentDao.class);
        List<Student> list = studentDao.searchAll();
        for (Student stu : list) {
            System.out.println(stu.getName());
        }
    }
}

你可能感兴趣的:(SSM,SSM,MyBatis)