您的位置:首页 > 其它

[知了堂学习笔记]_mybatis_02如何快速搭建mybatis框架之一

2017-11-22 19:46 791 查看
1.MyBatis的环境配置

导入MyBatis包

导入MyBatis包, mybatis-3.2.8.jar

导入MySQL驱动包, mysql-connector-java-5.1.24-bin.jar

创建表的实体类, 例如: MyUser(id, name, password)

编写MyBatis的核心配置文件, SqlMapConfig.xml

编写映射文件, 例如: MyUserMapper.xml

编写测试来完成数据库CRUD操作

2.MyBatis编码原理



3.MyBatis配置环境代码

工具:eclipse、mysql、jdk8、tomcat9.0

测试目录结构:



映射文件方式

Dtd/xsd:xml 文档格式验证

引入jar包,一个包

4000
核心配置文件:

环境:支持多个环境

事务支持方式:JDBC

数据源:url/driver/username/password

关系映射

映射类:属性和列名对应

映射文件:

Namespace

Sql语句

#{}ognl表达式 ${}el表达式

测试

读取核心配置文件

通过SqlSessionFactoryBuilder创建SqlSessionFactory(连接工厂)

得到SqlSession:真正执行sql的对象

根据namespace和sql映射的id得到sql语句statement

SqlSession中的方法执行sql

引入log4j

引入相关jar包

引入Log4j的配置文件

接口mapper代理方法

Namespace对应接口的全路径

id对应接口中的方法名

Session.getMapper()得到接口的代理对象

直接调用接口方法实现sql操作

SqlSessionFactoryBuilder类

SqlSessionFactoryBuilder(配置文件流,环境)

通过mybatis的核心配置文件创建SqlSessionFactory

SqlSessionFactory接口

一个SqlSessionFactory对应一个数据库,build就会有连接工厂诞生

Factory一定要做到单例

POOLED:factory中的连接是可以重用的

UINPOOLED:每次使用都是创建一个新的连接

JNDI:与框架容器整合的时候,数据连接配置项的共用

SqlSession:接口

每一次真正执行sql操作时的连接,相当于JDBC的connection

非线程安全

事务

request作用域

执行器:executor

Mapper实例

接口代理的形式

session.getMapper(Class实例)

Dao接口:定义了一系列的方法

mybatis通过getMapper生成了对应dao的代理

真正实现dao方法的是代理

映射文件和代理方式

代理方式最终是使用到映射文件方式的实现

1.编写jdbc.properties

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


2.编写config.xml

<?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>
<!-- 将数据库连接信息简化到java的属性文件中 -->
<properties resource="jdbc.properties"></properties>

<!-- 配置实体类的别名,配置实体类别名的目的是为了在引用实体类时,可以使用实体类的别名来代替实体类,达到简写的目的 -->
<typeAliases>
<!-- com.zhiliaotang.mybatis.model包下的所有实体类配置别名,MyBatis默认的设置别名的方式就是去除类所在的包后的简单的类名 -->
<!-- com.zhiliaotang.mybatis. model.Student这个实体类的别名就会被设置成Student-->
<package name="com.zhiliaotang.mybatis.model"/>
</typeAliases>

<!-- 配置开发模式,添加数据库信息,开启事务模式 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${mysql.driver}"/>
<property name="url" value="${mysql.url}"/>
<property name="username" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</dataSource>

</environment>
</environments>

<mappers>
<mapper resource="com/zhiliaotang/mybatis/mapper/StudentMapper.xml"/>
<mapper resource="com/zhiliaotang/mybatis/mapper/TeacherMapper.xml"/>
<mapper class="com.zhiliaotang.mybatis.dao.StudentDao"/>
</mappers>
</configuration>


3.编写实体类

什么实体类:简单说就是一个普通的JavaBean,满足JavaBean的属性对应数据库表中的字段

在mysql中创建student表

CREATE TABLE `student` (
`SNO` VARCHAR(3) COLLATE utf8_bin NOT NULL,
`SNAME` VARCHAR(4) COLLATE utf8_bin NOT NULL,
`SSEX` VARCHAR(2) COLLATE utf8_bin NOT NULL,
`SBIRTHDAY` DATETIME DEFAULT NULL,
`CLASS` VARCHAR(5) COLLATE utf8_bin DEFAULT NULL,
`TNO` VARCHAR(3) COLLATE utf8_bin NOT NULL
) ENGINE=MYISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin


编写student类

package com.zhiliaotang.mybatis.model;

import java.util.Date;

public class Student {

private String sno;//学号
private String sname;//姓名
private String ssex;//性别
private Date sBirthday;//生日
private String clas;//班级
private Teacher teacher;//老师

public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String sno, String sname, String ssex, Date sBirthday, String clas) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sBirthday = sBirthday;
this.clas = clas;
}
public Student(String sno, String sname, String ssex, Date sBirthday, String clas, Teacher teacher) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sBirthday = sBirthday;
this.clas = clas;
this.teacher = teacher;
}
public Student(String sno, Date sBirthday) {
super();
this.sno = sno;
this.sBirthday = sBirthday;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public Date getsBirthday() {
return sBirthday;
}
public void setsBirthday(Date sBirthday) {
this.sBirthday = sBirthday;
}
public String getClas() {
return clas;
}
public void setClas(String clas) {
this.clas = clas;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}

}


编写student01类

package com.zhiliaotang.mybatis.model;
import java.util.Date;
public class Student01 {
private String s_no;
private String s_name;
private String s_sex;
private Date s_birthday;
private String s_class;

public Student01(String s_no, String s_name, String s_sex, Date s_birthday, String s_class) {
super();
this.s_no = s_no;
this.s_name = s_name;
this.s_sex = s_sex;
this.s_birthday = s_birthday;
this.s_class = s_class;
}
public Student01() {
super();
// TODO Auto-generated constructor stub
}
public Student01(String s_no, Date s_birthday) {
super();
this.s_no = s_no;
this.s_birthday = s_birthday;
}
public String getS_no() {
return s_no;
}
public void setS_no(String s_no)
c895
{
this.s_no = s_no;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public String getS_sex() {
return s_sex;
}
public void setS_sex(String s_sex) {
this.s_sex = s_sex;
}
public Date getS_birthday() {
return s_birthday;
}
public void setS_birthday(Date s_birthday) {
this.s_birthday = s_birthday;
}
public String getS_class() {
return s_class;
}
public void setS_class(String s_class) {
this.s_class = s_class;
}

}


编写teacher类

package com.zhiliaotang.mybatis.model;

import java.util.Date;
import java.util.List;

public class Teacher {
private String tno;
private String tname;
private String tsex;
private Date tBirthday;
private String prof;
private String depart;
private List<Student> student;
public Teacher() {
super();
}
public Teacher(String tno, String tname, String tsex, Date tBirthday, String prof, String depart,
List<Student> student) {
super();
this.tno = tno;
this.tname = tname;
this.tsex = tsex;
this.tBirthday = tBirthday;
this.prof = prof;
this.depart = depart;
this.student = student;
}

public Teacher(String tno, String tname, String tsex, Date tBirthday, String prof, String depart) {
super();
this.tno = tno;
this.tname = tname;
this.tsex = tsex;
this.tBirthday = tBirthday;
this.prof = prof;
this.depart = depart;
}
public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getTsex() {
return tsex;
}
public void setTsex(String tsex) {
this.tsex = tsex;
}
public Date gettBirthday() {
return tBirthday;
}
public void settBirthday(Date tBirthday) {
this.tBirthday = tBirthday;
}
public String getProf() {
return prof;
}
public void setProf(String prof) {
this.prof = prof;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}

}


请关注“知了堂学习社区”,地址:http://www.zhiliaotang.com/portal.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mybatis 框架