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