1、导入MyBatis相关包坐标
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2、创建user数据表
3、编写User实体类
public class User {
private int id;
private String username;
private String password;
//以下省略:get、set和toString方法
}
4、编写映射文件UserMapper.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="userMapper">
<!-- 查询操作-->
<select id="select" resultType="domain.User">
select * from user
</select>
<!-- 增加操作-->
<insert id="insert" parameterType="domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
<!-- 修改操作-->
<update id="update" parameterType="domain.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<!-- 删除操作-->
<!-- 注意这里的:parameterType="java.lang.Integer"-->
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
</mapper>
5、编写SqlMapConfig.xml
<?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="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///test"/>
<property name="username" value="root"/>
<property name="password" value="00000000"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
6、编写测试类进行测试MyBatisTest.java
import domain.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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyBatisTest {
/*
* -查询数据
* */
@Test
public void select() throws IOException {
// 获取核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("mapper/SqlMapConfig.xml");
// 获得sql会话工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 获得sql会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行sql
List<User> userList = sqlSession.selectList("userMapper.select");
System.out.println(userList);
sqlSession.close();
}
/*
* -增加数据
* */
@Test
public void insert() throws IOException {
User user = new User();
user.setUsername("zhangsan");
user.setPassword("123456");
// 获取核心配置文件SqlMapConfig
InputStream resourceAsStream = Resources.getResourceAsStream("mapper/SqlMapConfig.xml");
// 获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行sql
int insert = sqlSession.insert("userMapper.insert", user);
System.out.println(insert);
// 提交事务
sqlSession.commit();//MyBatis执行更新操作需要提交事务
// 释放资源
sqlSession.close();
}
// ------原理一样 以下不在注释
/*
* -修改数据
* */
@Test
public void update() throws IOException {
User user = new User();
user.setId(1);
user.setUsername("lisi");
user.setPassword("12121");
InputStream resourceAsStream = Resources.getResourceAsStream("mapper/SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int update = sqlSession.update("userMapper.update", user);
System.out.println(update);
sqlSession.commit();
sqlSession.close();
}
/*
* -删除操作
* */
@Test
public void delete() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("mapper/SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
int delete = sqlSession.delete("userMapper.delete", 1);//删除id为1的数据
System.out.println(delete);
sqlSession.commit();
sqlSession.close();
}
}