编写接口
import com.shandx.pojo.User;
import java.util.List;
public interface UserMapper {
//获取全部的用户
List<User> selectUser();
//通过id查找用户
User selectUserById(int id);
//增加用户
int addUser(User user);
//通过id删除用户
int deleteUserById(int id);
//更新用户
int updateUser(User user);
}
对应的mapper语句编写
<?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对应Mapper接口的类-->
<mapper namespace="com.shandx.dao.UserMapper">
<!--select标签的id对应映射接口的方法名字 resultType:返回结果的类型 中间就编写sql语句-->
<!--获取全部的用户-->
<select id="selectUser" resultType="com.shandx.pojo.User">
select * from user
</select>
<!--通过id查找用户-->
<select id="selectUserById" resultType="com.shandx.pojo.User">
select *from user where id=#{id}
</select>
<!--增加用户-->
<insert id="addUser" parameterType="com.shandx.pojo.User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<!--通过id删除用户-->
<delete id="deleteUserById" parameterType="com.shandx.pojo.User">
delete from user where id=#{id}
</delete>
<!--更新用户-->
<update id="updateUser" parameterType="com.shandx.pojo.User">
update user set name=#{name},pwd=#{pwd} where id=#{id}
</update>
</mapper>
测试类
import com.shandx.dao.UserMapper;
import com.shandx.pojo.User;
import com.shandx.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import java.util.List;
public class UserMapperTest {
@Test //获取全部的用户
public void selectUser() {
//1.拿到sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
//2.通过sqlSessionFactory对象openSession()创建一个sqlSession。
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.通过sqlSession获得mapper对象 , 参数为映射文件对应的接口类的class对象
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//4.通过mapper对象来执行操作;
List<User> users = mapper.selectUser();
//获得结果集
for (User user : users) {
System.out.println(user);
}
}
@Test //通过id查找用户
public void selectUserById(){
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectUserById(2);
System.out.println(user);
}
@Test //增加用户
public void addUser(){
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(5, "shandianxia", "521");
int i = mapper.addUser(user);
if(i>0){
System.out.println("插入成功!!!");
}
sqlSession.commit();//提交事务
sqlSession.close();//关闭
}
@Test //通过id删除用户
public void deleteUserById(){
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int i = mapper.deleteUserById(5);
if(i>0){
System.out.println("删除成功!!!");
}
sqlSession.commit();//提交事务
sqlSession.close();//关闭
}
@Test //更新用户
public void updateUser(){
SqlSessionFactory sqlSessionFactory = MyBatisUtils.getSqlSessionFactory();
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = new User(3, "雷神", "456");
int i = mapper.updateUser(user);
if(i>0){
System.out.println("更新成功!!!");
}
sqlSession.commit();//提交事务
sqlSession.close();
}
}
【总结CRUD注意点】
1,增删改查需要提交事务
2,如果出现乱码,先在sql中进行测试,sql没问题,就检查配置文件
jdbc:mysql://localhost:3306/mybatis?
useSSL=true&useUnicode=true&characterEncoding=utf-8"
3、如果是基本数据类型,可以省略,但建议写上,引用类型必须写指定的 包名+类名