mybatis 实现增删改查(CRUD)
如何创建项目,注入依赖,编写核心配置文件、工具类、实体类这里就不详细说了,具体可以参考下边这条博文
https://www.cnblogs.com/bear7/p/12491937.html
这里将详细说一下有关mybatis操作数据库实现增删盖查的步骤:
*编写接口
*编写对应映射文件中mapper
*编写测试类
1.select语句:选择,查询数据
1.1 查询User表中所有信息
*编写接口
package com.kuang.dao;
import com.kuang.pojo.User;
import java.util.List;
public interface UserMapper {
List<User> getUserList();
}
*编写对应映射文件中mapper中的sql语句
<?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.kuang.dao.UserMapper"> <select id="getUserList" resultType="com.kuang.pojo.User"> select * from mybatis.user </select> </mapper>
//select * from mybatis.user 这里的mybatis.user的意思是:mybatis数据库中user表
*编写测试类
package com.kuang.dao;
import com.kuang.pojo.User;
import com.kuang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
@Test
public void test(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
List<User> userList = userDao.getUserList();
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
}
1.2 查询User表满足条件的数据
*编写接口
User getUserById(int id);
*编写对应映射文件中mapper
<select id="getUserById" parameterType="int" resultType="com.kuang.pojo.User">
select * from mybatis.user WHERE id = #{id}
</select>
*编写测试类
@Test
public void getUserById(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById(1);
System.out.println(user);
sqlSession.close();
}
2.insert语句:插入数据
*编写接口
int addUser(User user);
*编写对应映射文件中mapper
<insert id="addUser" parameterType="com.kuang.pojo.User" >
INSERT INTO mybatis.user(id, name, pwd) VALUES (#{id},#{name},#{pwd})
</insert>
*编写测试类
@Test
public void addUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.addUser(new User(6, "王五", "789456"));
System.out.println("res:"+res);
sqlSession.commit();
sqlSession.close();
}
3.update语句:更新数据
*编写接口
int updateUser(User user);
*编写对应映射文件中mapper
<update id="updateUser" parameterType="com.kuang.pojo.User">
UPDATE mybatis.user SET name=#{name},pwd=#{pwd} WHERE id = #{id}
</update>
*编写测试类
@Test
public void updateUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.updateUser(new User(4, "赵六", "123456"));
System.out.println(res);
sqlSession.commit();
sqlSession.close();
}
4.delete语句:删除数据
*编写接口
int deleteUser(int id);
*编写对应映射文件中mapper
<delete id="deleteUser" parameterType="int" >
DELETE FROM mybatis.user WHERE id=#{id}
</delete>
*编写测试类
@Test
public void deleteUser(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.deleteUser(6);
System.out.println(res);
sqlSession.commit();
sqlSession.close();
}