MyBatis 是 apache 的一个开源项目 iBatis,2010年这个项目由 apache software founfation 迁移到 google code,并且改名为 MyBatis,2013年11月迁移到 Github。
MyBatis 是一个实现数据持久化的开源框架
(ORMapping:Object Relationship Mapping 对象关系映射),简单理解就是对 JDBC 进行封装
。
<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.11version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>RELEASEversion>
<scope>compilescope>
dependency>
dependencies>
USE mybatis;
CREATE TABLE t_account(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(11),
PASSWORD VARCHAR(11),
age INT
);
package com.southwind.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account {
private long id;
private String username;
private String password;
private int age;
}
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">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="111111"/>
dataSource>
environment>
environments>
configuration>
MyBatis 框架需要开发者自定义SQL语句,写在Mapper.xml
文件中。实际开发中,会为每个实体类创建对应的Mapper.xml
,定义管理该对象数据的SQL。
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.mapper.AccountMapper">
<insert id="save" parameterType="com.southwind.entity.Account">
insert into t_account(username,password,age) values(#{username},#{password},#{age})
insert>
mapper>
在全局配置文件 config.xml 中注册 AccountMapper.xml
<mappers>
<mapper resource="com/southwind/mapper/AccountMapper.xml">mapper>
mappers>
package com.southwind.test;
import com.southwind.entity.Account;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
public class Test {
public static void main(String[] args) {
// 加载MyBatis配置文件
// 当前 inputStream 就是 inputstream 对应的一个数据流
InputStream inputStream = Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlsession = sqlSessionFactory.openSession();
String statement = "com.southwind.mapper.AccountMapper.save";
Account account = new Account(1L,"张三","123123",12);
sqlsession.insert(statement,account);
sqlsession.commit();
sqlsession.close();
}
}
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.xmlinclude>
includes>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>*.xmlinclude>
<include>*.propertiesinclude>
includes>
resource>
resources>
build>
若出现以下错误,请参考:https://www.cnblogs.com/smiler/p/9983146.html
package com.southwind.repository;
import com.southwind.entity.Account;
import java.util.List;
public interface AccountRepository {
public int save(Account account);
public int update(Account account);
public int deleteById(long id);
public List<Account> findAll();
public Account findById(long id);
}
创建接口对应的 Mapper.xml,定义接口方法对应的SQL语句。
Statement 标签会根据 SQL 执行的业务选择 insert、delete、update、select。
MyBatis 框架会根据规则字段创建接口实现类的代理对象。规则如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.southwind.repository.AccountRepository">
<insert id="save" parameterType="com.southwind.entity.Account">
insert into t_account(username,password,age) values(#{username},#{password},#{age})
</insert>
<update id="update" parameterType="com.southwind.entity.Account">
update t_account set username = #{username},password=#{password},age=#{age}
</update>
<delete id="deleteById" parameterType="long">
delete from t_account where id = #{id}
</delete>
<select id="findAll" resultType="com.southwind.entity.Account">
select * from t_account
</select>
<select id="findById" parameterType="long" resultType="com.southwind.entity.Account">
select * from t_account where id = #{id}
</select>
</mapper>
在 config.xml 中注册 AccountRepository.xml
<mapper resource="com/southwind/repository/AccountRepository.xml">mapper>
调用接口的代理对象完成相关的业务操作
package com.southwind.test;
import com.southwind.entity.Account;
import com.southwind.repository.AccountRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
// 加载MyBatis配置文件
// 当前 inputStream 就是 inputstream 对应的一个数据流
InputStream inputStream = Test2.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlsession = sqlSessionFactory.openSession();
// 获取实现接口的代理对象
AccountRepository accountRepository = sqlsession.getMapper(AccountRepository.class);
// 添加对象
Account account = new Account(2L,"李四","123123",13);
accountRepository.save(account);
sqlsession.commit();
List<Account> list = accountRepository.findAll();
for(Account account1:list){
System.out.println(account1);
}
sqlsession.close();
}
}