欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis动态代理方式配置(接口方式)

程序员文章站 2022-06-17 19:51:54
...

Mybatis配置流程

所需要用到的jar包   mybatis-3.5.1.jar 和 mysql-connector-java-5.1.0-bin.jar

1.配置mybatis-config.xml

创建mybatis-config.xml

这里需要配置几个基本配置

  • database.properties资源配置
  • 类型别名配置
  • mapper映射配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <!--配置properties资源文件-->
   <properties resource="database.properties"></properties>
    
    <!--配置类型别名-->
    <typeAliases>
        <!--单个别名-->
        <!--<typeAlias type="com.zy.Student" alias="student"></typeAlias>-->
        <!--通过包名批量定义别名,使用直接用类名即别名-->
        <package name="com.zy.pojo" />
    </typeAliases>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="${driver}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>

  <!--配置mapper映射 StudentMapper.xml全路径 -->
  <mappers>
    <mapper resource="com/zy/mapper/StudentMapper.xml"/>
  </mappers>
</configuration>

database.properties配置信息

创建database.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms
username=root
password=root

2.创建类--表的映射

创建Student.java实体类

数据库的字段和类中的属性一一对应

package com.zy.pojo;

public class Student {
	private Integer stuNo;
	private String stuName;
	private String stuClass;
	private int stuAge;
	
	
	public Student() {
		super();
	}
	public Student(Integer stuNo, String stuName, String stuClass, int stuAge) {
		super();
		this.stuNo = stuNo;
		this.stuName = stuName;
		this.stuClass = stuClass;
		this.stuAge = stuAge;
	}
	public Integer getStuNo() {
		return stuNo;
	}
	public void setStuNo(Integer stuNo) {
		this.stuNo = stuNo;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getStuClass() {
		return stuClass;
	}
	public void setStuClass(String stuClass) {
		this.stuClass = stuClass;
	}
	public int getStuAge() {
		return stuAge;
	}
	public void setStuAge(int stuAge) {
		this.stuAge = stuAge;
	}
	
	@Override
	public String toString() {
		return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", stuClass=" + stuClass + ", stuAge=" + stuAge
				+ "]";
	}
	
}

3.创建mapper接口

创建StudentMapper.java接口

package com.zy.mapper;

import java.util.List;

import com.zy.pojo.Student;

public interface StudentMapper {
    List<Student> getStudent();
}

4.创建mapper映射

创建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">
<!--
    namespace:mapper的位置
    id:接口的方法名
    resultType:返回值类型
-->
<mapper namespace="com.zy.mapper.StudentMapper">
  <select id="getStudent" resultType="student"  >
    select * from Student 
  </select>
</mapper>

5.测试类

创建StudentTest.java测试类

package com.zy.test;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

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 com.zy.mapper.StudentMapper;
import com.zy.pojo.Student;

public class StudentTest {
	public static void main(String[] args) throws IOException {
//		读取mybatis-config.xml配置信息
		Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
//		创建SqlSessionFactory对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
//		获得SqlSession对象
		SqlSession sqlSession = factory.openSession();
//		通过getMapper方法找到接口,然后调用接口方法-->Sql
		List<Student> students = sqlSession.getMapper(StudentMapper.class).getStudent();
//		打印结果
		for(Student studentList:students){
			System.out.println(studentList);
		}
//		关闭sqlSession的对象
		sqlSession.close();
	}
}

6.运行结果

Mybatis动态代理方式配置(接口方式)

7.项目结构

Mybatis动态代理方式配置(接口方式)

解析一下:

  • Mapper接口方法名和Mapper配置文件的id名相同,这样就可以通过接口找到相应的SQL语句

  • 测试类中通过getMapper方法找到接口,然后调用接口方法-->Sql