JavaEE之Mybatis简单应用
- 创建Maven项目
- Mybatis环境搭建
- MyBatis具体步骤
- 创建数据库和数据表
- 定义实体类User
- 定义DAO接口
- 编写Mapper.xml
- 注册Mapper
- 测试代码
创建Maven项目
Mybatis环境搭建
pom.xml中引入MyBatis核心依赖
<!--依赖-->
<dependencies>
<!--MyBatis核心依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MySql驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
在resources中,创建MyBatis配置文件
配置文件,内容如下
<?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">
<!--MyBatis配置-->
<configuration>
<!--JDBC环境配置、选中默认环境-->
<environments default="edu_config">
<!--MySql数据库环境配置-->
<environment id="edu_config">
<!--事务管理-->
<transactionManager type="jdbc"></transactionManager>
<!--连接池-->
<dataSource type="org.apache.ibatis.datasource.pooled.PooledDataSourceFactory">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<!-- &转义& -->
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_edu?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="****"/>
<property name="password" value="****"/>
</dataSource>
</environment>
</environments>
<!--Mapper注册-->
<mappers>
<!--注册Mapper文件的所在位置-->
<mapper resource="UserDAOMapper.xml"/>
</mappers>
</configuration>
MyBatis具体步骤
创建数据库和数据表
console内容如下
create database mybatis_edu default charset =utf8;
create table t_user(
id int primary key auto_increment,
username varchar(50),
password varchar(50),
gender tinyint,
regist_time datetime
)default charset =utf8
自己添加一点数据,方便最后测试
定义实体类User
package com.edu.entity;
import java.util.Date;
public class User{
private Integer id;
private String username;
private String password;
private Boolean gender;
private Date registTime;
public User(){}
public User(Integer id, String username, String password, Boolean gender, Date registTime) {
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.registTime = registTime;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender=" + gender +
", registTime=" + registTime +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public Date getRegistTime() {
return registTime;
}
public void setRegistTime(Date registTime) {
this.registTime = registTime;
}
}
定义DAO接口
package com.edu.dao;
import com.edu.entity.User;
public interface UserDAO {
User queryUserByID(Integer id);
}
编写Mapper.xml
在resources目录中,创建UserDaoMapper.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapperg 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace = 所需实现的接口全限定名-->
<mapper namespace="com.edu.dao.UserDAO">
<!--id = 所需重写的接口抽象方法,resultType = 查询后所需返回的对象类型-->
<select id="queryUserByID" resultType="com.edu.entity.User">
select id,username,password,gender,regist_time
from t_user
<!--#{arg0} = 方法的第一个形参-->
where id=#{arg0}
</select>
</mapper>
注册Mapper
<!--Mapper注册-->
<mappers>
<!--注册Mapper文件的所在位置-->
<mapper resource="UserDAOMapper.xml"/>
</mappers>
测试代码
import com.edu.dao.UserDAO;
import com.edu.entity.User;
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;
public class MybatisTest {
public static void main(String[] args) throws IOException {
//1.获得读取MyBatis配置文件的流对象
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//2.构建sqlSessionfactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.通过sqlsessionFactory创建sqlSession
SqlSession sqlSession =sqlSessionFactory.openSession();
//4。通过sqlsession获得DAO实现类的对象
//UserDAO mapper = sqlSession.getMapper(UserDAO.class);//获取UserDAO对应的实现类的对象
UserDAO mapper =sqlSession.getMapper(UserDAO.class);
//5.测试查询方法是否好用
User user1 = mapper.queryUserByID(1);
User user2 = mapper.queryUserByID(2);
System.out.println(user1);
System.out.println(user2);
}
}