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

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.运行结果

运行结果

7.项目结构

项目结构

解析一下:

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

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

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值