Mybatis概述: 一个持久层框架
主体架构:
SqlMapperConfig.xml(核心配置文件) : sqlSessionFactory 和 sqlsession
↓
Mapper.xml(动态sql的语句实现)
↓
MappedStatement(方便动态加载Sql,用于绑定参数和返回值的封装)
↓
DB(数据库)
一: 首先我们先进行导包:
二: 进行核心配置文件的编写:
<?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>
<!-- 声明数据库连接配置 -->
<environments default="development">
<environment id="development">
<!-- 事务管理 :默认的jdbc事务,取消了单条sql的自动提交 -->
<transactionManager type="JDBC"/>
<!-- 数据源的配置 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_gm"/>
<property name="username" value="root"/>
<property name="password" value="000000"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--
单独采用mapper文件实现数据库操作
<mapper resource="com/zhongruan/mapper/StudentMapper.xml"/>-->
<!--需要根据引用接口的形式进行调用-->
<mapper class="com.zhongruan.dao.StudentDao"/>
<mapper class="com.zhongruan.dao.UserDao"/>
</mappers>
</configuration>
三: bean层的编写(与mysql数据库的数据进行对应)
package com.zhongruan.bean;
public class Student {
private int id;
private String stuName;
private String stuSex;
private String stuAge;
public Student(int id, String stuName, String stuSex, String stuAge) {
this.id = id;
this.stuName = stuName;