3. mybatis的修改、删除和查询
大约 2 分钟
修改、删除和查询操作
1 映射文件
<?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.bobo.pojo.User">
<!-- 添加用户 -->
<insert id="addUser" parameterType="com.bobo.pojo.User" >
insert into t_user (username,address,gender)values(#{username},#{address},#{gender})
</insert>
<!-- 更新用户信息 -->
<update id="updateUser" parameterType="com.bobo.pojo.User">
update t_user set username=#{username} ,address=#{address},gender=#{gender} where id =#{id}
</update>
<!-- 删除用户信息 -->
<delete id="deleteUser" >
delete from t_user where id=#{id}
</delete>
<!-- 查询所有用户信息 -->
<select id="queryAll" resultType="com.bobo.pojo.User">
select * from t_user
</select>
<!-- 根据用户编号查询用户信息 -->
<select id="queryById" parameterType="java.lang.Integer" resultType="com.bobo.pojo.User">
select * from t_user where id = #{id}
</select>
</mapper>
2 测试代码
package com.bobo.test;
import com.bobo.pojo.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.jupiter.api.Test;
import java.io.InputStream;
import java.util.List;
public class TestDemo01 {
/**
* MyBatis案例
*/
@Test
public void test01() throws Exception{
// 1.读取核心配置文件
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
// 2.获取一个SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.获取一个SqlSession对象 基于SqlSessionFactory获取
SqlSession sqlSession = factory.openSession();
// 添加数据 ORM框架 面试对象操作
User user = new User("翠花","深圳福田","2");
// 4.执行数据库操作 statement= namespace+"."+id
int count = sqlSession.insert("com.bobo.pojo.User.addUser", user);
System.out.println("影响的行数..."+count);
// 5.MyBatis中默认是不会自动提交DML操作的数据的
sqlSession.commit();
// 6.关闭会话
sqlSession.commit();
}
@Test
public void test02() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
User user = new User(19,"翠花","北京海淀","女");
int i = sqlSession.update("com.bobo.pojo.User.updateUser", user);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void deleteUser() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int i = sqlSession.delete("com.bobo.pojo.User.deleteUser", 20);
System.out.println("影响的行数:" + i);
sqlSession.commit();
sqlSession.close();
}
@Test
public void queryAll() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List<User> lists = sqlSession.selectList("com.bobo.pojo.User.queryAll");
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
@Test
public void queryById() throws Exception{
InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List<User> lists = sqlSession.selectList("com.bobo.pojo.User.queryById",19);
for (User user : lists) {
System.out.println(user);
}
sqlSession.close();
}
}