下面是使用Mybatis实现基础的增删改查的几个步骤:
1. 添加依赖:在pom.xml文件中添加mybatis依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
2. 创建主配置文件:在src 的resource目录下,创建主配置文件mybatis.xml(初次创建可以在idea中创建模板)
<?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>
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/day7.10"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
3. 创建工具类,读取主配置文件,创建获得sqlSession的方法
package com.zxp.util;
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.Reader;
/*
* 读取mybatis主配置文件
* */
public class SqlSessionFactoryUtil {
private static SqlSessionFactory factory;
private SqlSessionFactoryUtil() {
}
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis.xml");
factory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
public static SqlSession getSqlSession() {
return factory.openSession(true);//自动提交事务
}
}
4. 创建实体类
package com.zxp.entity;
import lombok.Data;
import java.io.Serializable;
@Data
public class Emp implements Serializable {
private int eid;
private String ename;
private String esex;
private String hirdate;
private String sal;
}
5. 创建接口
package com.zxp.dao;
import com.zxp.entity.Emp;
import java.util.List;
public interface EmpDao {
List<Emp> getAllEmps();
Emp getEmpById(int n);
int addEmp(Emp emp);
int deleteEmp(int n);
int updateEmp(Emp emp);
int delEmp(int eid);
}
6. 在resource文件夹中创建mapper文件夹,创建接口对应的xml文件,并编写sql语句
<?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.zxp.dao.EmpDao">
<select id="getAllEmps" resultType="com.zxp.entity.Emp">
select *
from emp
</select>
<select id="getEmpById" parameterType="int" resultType="com.zxp.entity.Emp">
select *
from emp
where eid = #{eid}
</select>
<insert id="addEmp" parameterType="com.zxp.entity.Emp">
insert into emp(ename, esex, sal)
values (#{ename}, #{esex}, #{sal})
</insert>
<update id="updateEmp" parameterType="com.zxp.entity.Emp">
update emp
set ename=#{ename},
sal=#{sal}
where eid = #{eid}
</update>
<delete id="delEmp" parameterType="int">
delete
from emp
where eid = #{eid}
</delete>
</mapper>
创建对应的xml文件后需要再主配置文件mybatis中注册:
<mapper resource="mapper/EmpDao.xml"></mapper>
7. 创建测试类并运行
import com.zxp.dao.AdminDao;
import com.zxp.dao.EmpDao;
import com.zxp.entity.Admin;
import com.zxp.entity.Emp;
import com.zxp.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class test {
SqlSession session = null;
EmpDao empDao = null;
AdminDao adminDao = null;
@Before
public void init() {
session = SqlSessionFactoryUtil.getSqlSession();
empDao = session.getMapper(EmpDao.class);
adminDao = session.getMapper(AdminDao.class);
}
@Test
public void testQuery() {
List<Emp> list = empDao.getAllEmps();
for (Emp e : list) {
System.out.println(e.getEid() + "," + e.getEname() + "," + e.getEsex() + "," + e.getSal());
}
}
@Test
public void testQueryById() {
Emp emp = empDao.getEmpById(1001);
System.out.println(emp.getEid() + "," + emp.getEname() + "," + emp.getEsex() + "," + emp.getSal());
}
@Test
public void testAddEmp() {
Emp e = new Emp();
e.setEname("唐三");
e.setEsex("男");
e.setSal("5000");
int result = empDao.addEmp(e);
System.out.println(result);
}
@Test
public void testUpdate() {
Emp emp = empDao.getEmpById(1040);
emp.setEname("李四44");
emp.setSal("2000");
int result = empDao.updateEmp(emp);
System.out.println(result);
}
@Test
public void testDel() {
int result = empDao.delEmp(1040);
System.out.println(result);
}
@After
public void destory() {
session.close();
}
}