准备
1.官网上下载MyBatis安装包
下载地址https://github.com/mybatis/mybatis-3/releases
2.准备好数据库
3.创建maven工程
删除src
4.导入依赖
在pom.xml中输入以下代码
左上角的刷新自动配置依赖
防止导出出现问题还需要配置
5.创建子模块
右键添加一个Module新建maven工程,配置如图
6.配置mybatis-config.xml 文件
在resourse中新增一个xml文件配置核心文件并按照自己的用户名和密码修改
7.创建工具实体和接口类
完整目录如下
里面的MybatisUtils代码如下
package com.robot.utils;
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 MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String source = "mybatis-config.xml";
InputStream inputStream = null;
inputStream = Resources.getResourceAsStream(source);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 获取SqlSession
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
User代码如下
package com.robot.pojo;
public class User {
private int id;
private String name;
private String pwd;
public User() {
}
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
创建接口的代码如下(实现其他功能就是修改接口方法并配置相应的xml文件并测试)
package com.robot.dao;
import com.robot.pojo.User;
import java.util.List;
public interface UserMapper {
// 查询全部用户
List<User> getUserList();
// 根据ID查询用户
User getUserById(int id);
// 插入用户
int addUser(User user);
// 修改用户
int updateUser(User user);
// 删除用户
int deleteUser(int id);
}
8.配置接口对应的xml文件
<?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.robot.dao.UserMapper">
<!-- select all-->
<select id="getUserList" resultType="com.robot.pojo.User">
select * from mybatis.user
</select>
<!-- select one-->
<select id="getUserById" parameterType="int" resultType="com.robot.pojo.User">
select * from mybatis.user WHERE id = #{id}
</select>
<!-- add user-->
<insert id="addUser" parameterType="com.robot.pojo.User">
insert into mybatis.user (id, name, pwd) values (#{id}, #{name}, #{pwd})
</insert>
<!-- update user-->
<update id="updateUser" parameterType="com.robot.pojo.User">
update mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id};
</update>
<!-- delete user-->
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
之前的xml文件中记得重新配置,修改mappers标签令他连接到此xml文件
<mappers>
<mapper resource="com/robot/dao/UserMapper.xml"/>
</mappers>
9.测试
test里面创建java类测试
package com.robot.dao;
import com.robot.pojo.User;
import com.robot.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void getUserListTest() {
// 1.获得SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
// 2.执行SQL
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = userMapper.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void getUserByIdTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
@Test
public void addUserTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.addUser(new User(4, "baby2", "123456"));
System.out.println(res);
// 提交事务
sqlSession.commit();
sqlSession.close();
}
@Test
public void updateUserTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
int res = userMapper.updateUser(new User(4, "mybaby", "125888"));
System.out.println(res);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUserTest() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
userMapper.deleteUser(4);
sqlSession.commit();
sqlSession.close();
}
}