表现层:SpringMVC、Struts2
业务层:Spring可对service层提供统一的事务控制。
数据访问层:JDBC、自定义框架封装jdbc、JDBCTemplate(Spring)、apache DbUtils、Hibernate框架、Jpa、Spring Data Jpa、Mybatis等
频繁创建连接对象和释放,容易浪费系统资源影响性能;
sql语句的定义、参数设置、结果集处理存在硬编码,不好维护和修改
. (3) 结果集处理存在重复代码,每次都要遍历ResultSet,获取一行数据,封装为对象处理麻烦。
mybatis早期版本叫做ibatis,目前代码托管在github。
mybatis是对jdbc的封装,是一个持久层的框架。开发者只需要关注业务本身,不需要花费精力去处理其他过程代码
mybatis是通过xml或者注解进行配置,实现java对象与sql语句的对应关系(映射)。
1.准备数据库
-- 1.创建数据库
CREATE DATABASE mybatis DEFAULT CHARACTER SET utf8;
-- 2.创建用户表
DROP TABLE IF EXISTS USER;
CREATE TABLE USER (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(32) NOT NULL COMMENT '用户名称',
birthday DATETIME DEFAULT NULL COMMENT '生日',
sex CHAR(1) DEFAULT NULL COMMENT '性别',
address VARCHAR(256) DEFAULT NULL COMMENT '地址',
PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO USER(id,username,birthday,sex,address) VALUES
(41,'旺旺','2018-08-27 17:47:08','男','北京'),(42,'小二王','2018-09-02 15:09:37','女','南京'),
(43,'明明','2018-01-04 11:34:34','女','东京'),
(45,'狗蛋','2017-02-04 12:04:06','男','西安'),
(46,'隔壁老王','2015-05-07 17:37:26','男','济南'),
(48,'茱莉','2017-07-07 11:44:00','女','华盛顿');
SELECT * FROM USER;
2.创建项目
3.创建实体类
package com.azure.entity;
import java.util.Date;
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
public User() {
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
public User(int id, String username, Date birthday, String sex, String address) {
this.id = id;
this.username = username;
this.birthday = birthday;
this.sex = sex;
this.address = address;
}
public int getId() {return id;}
public void setId(int id) {this.id = id;}
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
public Date getBirthday() {return birthday;}
public void setBirthday(Date birthday) {this.birthday = birthday;}
public String getSex() {return sex;}
public void setSex(String sex) {this.sex = sex;}
public String getAddress() {return address;}
public void setAddress(String address) {this.address = address;}
}
4.添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.azuregroupId>
<artifactId>day47projects_mybatis01artifactId>
<version>1.0-SNAPSHOTversion>
<packaging>warpackaging>
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.30version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
dependencies>
project>
<configuration>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<dataSource type="pooled">
<property name="driver" value="com.mysql.jdbc.Driver">