Mybatis入门
概述:
MyBatis是一个优秀的持久层框架,它对jdbc操作数据库的过程进行封装,使开发者只需要关注SQL本是,而需要花费精力处理注册驱动、创建Connection、创建Statement、手动设置参数、结果集检索及映射等繁杂的过程代码。
与JDBC对比
入门案列
-
1.创建新项目 JAVA–>JAVA EE–>Web Application
-
2.项目命名
-
3.添加jar包WEB-INF/lib目录下(目录不存在,需手动创建)
-
4.添加jar
案列数据库和表:User
# 创建数据库
CREATE DATABASE ssm_db1;
# 使用数据库
USE ssm_db1;
# 1.1 创建用户表
CREATE TABLE `user` (
`uid` VARCHAR(32) NOT NULL,
`username` VARCHAR(20) DEFAULT NULL, #用户名
`password` VARCHAR(32) DEFAULT NULL, #密码
`name` VARCHAR(20) DEFAULT NULL, #昵称
`email` VARCHAR(30) DEFAULT NULL, #电子邮箱
`telephone` VARCHAR(20) DEFAULT NULL, #电话
`birthday` DATE DEFAULT NULL, #生日
`sex` VARCHAR(10) DEFAULT NULL, #性别
`state` INT(11) DEFAULT 0, #状态:0=未激活,1=已激活
`code` VARCHAR(64) DEFAULT NULL, #激活码
PRIMARY KEY (`uid`)
) ;
# 1.2 初始化用户默认数据
INSERT INTO `user` VALUES ('u001','jack','1234','杰克','jack@czxy.com','13612345678','2015-11-04','男',0,NULL);
INSERT INTO `user` VALUES ('u002','rose','1234','肉丝','rose@czxy.com','13612345679','2015-11-05','女',0,NULL);
INSERT INTO `user` VALUES ('373eb242933b4f5ca3bd43503c34668b','ccc','ccc','aaa','bbb@store.com','15723689921','2015-11-04','男',0,'9782f3e837ff422b9aee8b6381ccf927bdd9d2ced10d48f4ba4b9f187edf7738'),('3ca76a75e4f64db2bacd0974acc7c897','bb','bb','张三','bbb@store.com','15723689921','1990-02-01','男',0,'1258e96181a9457987928954825189000bae305094a042d6bd9d2d35674684e6'),('62145f6e66ea4f5cbe7b6f6b954917d3','cc','cc','张三','bbb@store.com','15723689921','2015-11-03','男',0,'19f100aa81184c03951c4b840a725b6a98097aa1106a4a38ba1c29f1a496c231'),('c95b15a864334adab3d5bb6604c6e1fc','bbb','bbb','老王','bbb@store.com','15712344823','2000-02-01','男',0,'71a3a933353347a4bcacff699e6baa9c950a02f6b84e4f6fb8404ca06febfd6f'),('f55b7d3a352a4f0782c910b2c70f1ea4','aaa','aaa','小王','aaa@store.com','15712344823','2000-02-01','男',1,NULL);
入门案列:查询所有
JavaBean: User
package com.czxy.ssm.domain;
import java.util.Date;
/**
*
Create Table
CREATE TABLE `user` (
`uid` varchar(32) NOT NULL,
`username` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`sex` varchar(10) DEFAULT NULL,
`state` int(11) DEFAULT NULL,
`code` varchar(64) DEFAULT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
*/
public class User {
private String uid;
private String username;
private String password;
private String name;
private String email;
private Date birthday;
private String sex;
private Integer state;
private String code;
@Override
public String toString() {
return "User{" +
"uid='" + uid + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", email='" + email + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", state=" + state +
", code='" + code + '\'' +
'}';
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
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 Integer getState() {
return state;
}
public void setState(Integer state) {
this.state = state;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public User(String uid, String username, String password, String name, String email, Date birthday, String sex, Integer state, String code) {
this.uid = uid;
this.username = username;
this.password = password;
this.name = name;
this.email = email;
this.birthday = birthday;
this.sex = sex;
this.state = state;
this.code = code;
}
public User() {
}
}
编写Dao:UserMapper
在MyBatis只需要编写接口即可,实现类由MyBatis自动生成,并在测试程序时自动执行。
package com.czxy.ssm.mapper;
import com.czxy.ssm.domain.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
/**
* 查询所有
* @return
*/
@Select("select * from user")
public List<User> selectAll();
}
编写核心配置文件:SqlMapConfig.xml
- 配置文件名称:SqlMapConfig.xml
- 配置文件位置:src
<?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>
<!-- 环境: default的值引用什么id,就表示使用什么数据库了-->
<environments default="db1">
<!-- 配置数据库连接信息及事务 -->
<environment id="db1">
<!-- 表示使用事务:默认是自动开启事务 -->
<transactionManager type="JDBC" />
<!-- 使用连接池 -->
<dataSource type="POOLED">
<!-- 数据库连接基本信息 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssm_db1" />
<property name="username" value="root" />
<property name="password" value="1234" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 表示加载此包下的所有dao接口-->
<package name="com.czxy.ssm.mapper"/>
</mappers>
</configuration>
测试类
package com.czxy.ssm.test;
import com.czxy.ssm.domain.User;
import com.czxy.ssm.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test01_SelectAll {
public static void main(String[] args) throws IOException {
//1 加载配置文件
// 1.1 获得资源流
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
// 1.2 获得工厂
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//2 获得会话(连接)
SqlSession session = factory.openSession();
//3获得功能接口
UserMapper userMapper = session.getMapper(UserMapper.class);
//4 调用功能
List<User> userList = userMapper.selectAll();
//5 打印查询结果
for (User user : userList) {
System.out.println(user);
}
}
}