在MyBatis项目中编写代码实现对MySql数据库的增删查改
查询User表所有用户的信息
因为查询的内容返回的数据不止一条,因此我们用List集合作为返回值
public interface SelectMapper {
/*
若查询多条,不能用实体类对象为返回值,否则报错
*/
//查询所有信息
List getAllUser();
}
import com.mybatis.Util.SqlSessionUtil;
import com.mybatis.mapper.SelectMapper;
import com.mybatis.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.Test;
import javax.crypto.spec.PSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class SelectMapperText {
@Test
public void getAllUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List allUser = mapper.getAllUser();
allUser.forEach(s-> System.out.println(s));
}
}
运行getAllUser后为:
向User表增加一个用户,用户信息为:
用户名:sb,密码:123456,年龄:20,性别:男,邮件:[email protected]
package com.mybatis.mapper;
import com.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface SelectMapper {
//查询所有信息
List getAllUser();
//插入用户信息
void insertUser(User user);
}
insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
import com.mybatis.Util.SqlSessionUtil;
import com.mybatis.mapper.SelectMapper;
import com.mybatis.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.Test;
import javax.crypto.spec.PSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class SelectMapperText {
@Test
public void getAllUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List allUser = mapper.getAllUser();
allUser.forEach(s-> System.out.println(s));
}
@Test
public void insertUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = new User(null,"sb","123456",20,"男","[email protected]");
mapper.insertUser(user);
}
}
修改用户名为admin的信息,将性别改为女
package com.mybatis.mapper;
import com.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface SelectMapper {
//查询所有信息
List getAllUser();
//插入用户信息
void insertUser(User user);
//修改用户信息
void updateUser(@Param("username") String username);//用注释当sql名
}
insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
update user set sex = '女' where username=#{username}
import com.mybatis.Util.SqlSessionUtil;
import com.mybatis.mapper.SelectMapper;
import com.mybatis.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.Test;
import javax.crypto.spec.PSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class SelectMapperText {
@Test
public void getAllUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List allUser = mapper.getAllUser();
allUser.forEach(s-> System.out.println(s));
}
@Test
public void insertUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = new User(null,"sb","123456",20,"男","[email protected]");
mapper.insertUser(user);
}
@Test
public void updateUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
mapper.updateUser("admin");
}
}
删除用户为“root”的所有信息
package com.mybatis.mapper;
import com.mybatis.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface SelectMapper {
//查询所有信息
List getAllUser();
//插入用户信息
void insertUser(User user);
//修改用户信息
void updateUser(@Param("username") String username);//用注释当sql名
//删除用户信息
void deleteUser(@Param("username") String username);
}
insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
update user set sex = '女' where username=#{username}
delete from user where username = #{username}
import com.mybatis.Util.SqlSessionUtil;
import com.mybatis.mapper.SelectMapper;
import com.mybatis.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.Test;
import javax.crypto.spec.PSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
public class SelectMapperText {
@Test
public void getAllUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
List allUser = mapper.getAllUser();
allUser.forEach(s-> System.out.println(s));
}
@Test
public void insertUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
User user = new User(null,"sb","123456",20,"男","[email protected]");
mapper.insertUser(user);
}
@Test
public void updateUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
mapper.updateUser("admin");
}
@Test
public void deleteUser() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession(true);
SelectMapper mapper = sqlSession.getMapper(SelectMapper.class);
mapper.deleteUser("root");
}
}