在MyBatis项目中编写代码实现对MySql数据库的增删查改
查询User表所有用户的信息
因为查询的内容返回的数据不止一条,因此我们用List集合作为返回值
- public interface SelectMapper {
- /*
- 若查询多条,不能用实体类对象为返回值,否则报错
- */
-
- //查询所有信息
- List
getAllUser(); -
- }
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.mybatis.mapper.SelectMapper"> -
-
- select *from user
-
-
注意:mapper的namespace必须对应接口名,select的id必须是我们写的方法名,两个必须要一样。查询结果需要返回,因此有resultType为实体类名
SelectMapperText类开始测试
- 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后为:
成功查到所有用户信息!!!
2、增加功能
向User表增加一个用户,用户信息为:
用户名:sb,密码:123456,年龄:20,性别:男,邮件:123456@qq.com
SelectMapeer接口下定义方法名:void insertUser(User user);
- 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);
- }
SelectMapper的xml文件下写sql语句
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.mybatis.mapper.SelectMapper"> -
-
- select *from user
-
-
-
"insertUser"> - insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
-
-
SelectMapperText类开始测试
- 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,"男","123456@qq.com");
- mapper.insertUser(user);
- }
-
- }
成功添加用户信息!!!
3、修改功能
修改用户名为admin的信息,将性别改为女
SelectMapeer接口下定义方法名: void updateUser(String username)
- 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名
- }
SelectMapper的xml文件下写sql语句
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.mybatis.mapper.SelectMapper"> -
-
- select *from user
-
-
-
"insertUser"> - insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
-
-
-
"updateUser"> - update user set sex = '女' where username=#{username}
-
-
SelectMapperText类开始测试
- 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,"男","123456@qq.com");
- 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");
- }
-
- }
成功修改用户信息!!!
4、删除功能
删除用户为“root”的所有信息
SelectMapeer接口下定义方法名:void deleteUser(String username)
- 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);
- }
SelectMapper的xml文件下写sql语句
- "1.0" encoding="UTF-8" ?>
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
"com.mybatis.mapper.SelectMapper"> -
-
- select *from user
-
-
-
"insertUser"> - insert into user values (null,#{username},#{password},#{age},#{sex},#{email})
-
-
-
"updateUser"> - update user set sex = '女' where username=#{username}
-
-
-
"deleteUser"> - delete from user where username = #{username}
-
SelectMapperText类开始测试
- 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,"男","123456@qq.com");
- 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");
- }
- }
成功删除用户信息!!!
三、总结
首先创造好接口写入要进行的方法名,然后创建xml文件专门写sql语句,值得注意的是:namespace要和接口名相同,id名要和接口里面的方法名相同,在测试类中:先获取映射文件,然后创建数据会议工厂生成数据会议,最后创建mapper使用接口的函数。
总之流程为:
1、创建接口写方法名。
2、创建xml文件写sql语句。
3、测试类进行测试
如果本篇文章对你有用的话,请一键三连支持一下博主,一起共同学习共同进步!!!
-
相关阅读:
Draw Your Cards
Hadoop Hbase
基于CentOS 7.9离线安装CM6.3.1和CDH6.3.2大数据平台
SkeyeVSS H5无插件视频云直播点播系统协助药店实现远程监控系统全覆盖
c++ 常用STL 之set
互融云农产品追溯系统:区块链存证技术实现双向可追溯
MySQL My.cnf参数梳理与延伸 (MYSQL 8 INNODB 类)
SpringMVC(三)获取请求参数
第一篇-专题说明及当前进度
Spring Boot(二)
-
原文地址:https://blog.csdn.net/m0_62476845/article/details/132998876