MyBatis.3.CRUD

举报
tea_year 发表于 2021/12/29 22:36:04 2021/12/29
【摘要】 <?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...

  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  4. <mapper namespace="org.mybatis.example.dao.DeptMapper">
  5. <select id="selectOne" parameterType="int"
  6. resultType="org.mybatis.example.dao.Dept">
  7. select * from dept where deptno=#{id}
  8. </select>
  9. <insert id="insertDept" parameterType="org.mybatis.example.dao.Dept"
  10. useGeneratedKeys="true" keyProperty="dept">
  11. insert into dept(dname,loc)
  12. values(
  13. #{dname,jdbcType=VARCHAR},
  14. #{loc,jdbcType=VARCHAR})
  15. </insert>
  16. <update id="updateDept" parameterType="org.mybatis.example.dao.Dept">
  17. update dept set dname=#{dname},loc=#{loc}
  18. where deptno=#{deptno}
  19. </update>
  20. <delete id="deleteDept" parameterType="java.lang.Integer">
  21. delete from dept where deptno=#{id}
  22. </delete>
  23. </mapper>

JAVA就业套餐课:https://edu.csdn.net/combo/detail/1230


  
  1. package org.mybatis.example.dao;
  2. public interface DeptMapper {
  3. public Dept selectOne(int id);
  4. public int insertDept(Dept dept);
  5. public int updateDept(Dept dept);
  6. public int deleteDept(int id);
  7. }

 


  
  1. import java.io.IOException;
  2. import java.io.Reader;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. import org.mybatis.example.dao.Dept;
  8. import org.mybatis.example.dao.DeptMapper;
  9. public class Test2 {
  10. public static void main(String[] args) throws IOException {
  11. String resource="mybatisConfig.xml";
  12. Reader reader=Resources.getResourceAsReader(resource);
  13. SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader);
  14. SqlSession session=sqlMapper.openSession();
  15. try {
  16. DeptMapper mapper=session.getMapper(DeptMapper.class);
  17. //1.查询
  18. // Dept dept=mapper.selectOne(2);
  19. // System.out.println(dept.getDname());
  20. //2.插入操作
  21. Dept dept=new Dept();
  22. dept.setDname("张沅湲");
  23. dept.setLoc("郑州");
  24. mapper.insertDept(dept);
  25. session.commit();//实现增加 修改 删除
  26. //3.更新操作;
  27. // Dept dept=new Dept();
  28. // dept.setDeptno(3);
  29. // dept.setDname("张沅湲");
  30. // dept.setLoc("湖南");
  31. // mapper.updateDept(dept);
  32. // session.commit();
  33. //4.删除操作;
  34. // int i=mapper.deleteDept(3);
  35. // session.commit();
  36. // System.out.println(i);
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }finally{
  40. session.close();
  41. }
  42. }
  43. }

复杂的查询,在项目开发中,可以使用一个工具类来封装这些复杂的步骤:

 


  
  1. package org.mybatis.example.dao;
  2. import java.io.Reader;
  3. import org.apache.ibatis.io.Resources;
  4. import org.apache.ibatis.session.SqlSession;
  5. import org.apache.ibatis.session.SqlSessionFactory;
  6. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
  7. public class SqlSessionFactoryUtil {
  8. private static SqlSessionFactory factory;
  9. public SqlSessionFactoryUtil() {}
  10. static{
  11. Reader reader=null;
  12. try {
  13. reader=Resources.getResourceAsReader("mybatisConfig.xml");
  14. factory=new SqlSessionFactoryBuilder().build(reader);
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }finally{
  18. try {
  19. if(reader!=null)reader.close();
  20. } catch (Exception e2) {
  21. e2.printStackTrace();
  22. }
  23. }
  24. }
  25. public static SqlSession getSqlSession(){
  26. return factory.openSession();
  27. }
  28. }

DeptMapper接口的完全代码:

 

 


  
  1. package org.mybatis.example.dao;
  2. import java.util.List;
  3. public interface DeptMapper {
  4. public Dept selectOne(int id);
  5. public int insertDept(Dept dept);
  6. public int updateDept(Dept dept);
  7. public int deleteDept(int id);
  8. public List<Dept>selectAllDepts();
  9. public List<Dept>selectByCriteria(String deptName);
  10. }

1.查询所有部门信息参考代码;

 

 


  
  1. <select id="selectAllDepts" resultType="org.mybatis.example.dao.Dept" >
  2. select deptno,dname,loc from dept
  3. </select>


测试代码;

 

 


  
  1. import java.util.List;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.mybatis.example.dao.Dept;
  4. import org.mybatis.example.dao.DeptMapper;
  5. import org.mybatis.example.dao.SqlSessionFactoryUtil;
  6. public class Test3 {
  7. public static void main(String[] args) {
  8. SqlSession session=SqlSessionFactoryUtil.getSqlSession();
  9. try {
  10. DeptMapper mapper=session.getMapper(DeptMapper.class);
  11. List<Dept>depts=mapper.selectAllDepts();
  12. for(Dept dept:depts){
  13. System.out.println("部门名称:"+dept.getDname());
  14. }
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }finally{
  18. session.close();
  19. }
  20. }
  21. }


2.按条件查询,映射文件做以下修改,新增条件为模糊查询方法:

 

 


  
  1. <!-- 按条件查询 -->
  2. <select id="selectByCriteria" parameterType="java.lang.String"
  3. resultType="org.mybatis.example.dao.Dept">
  4. select deptno,dname,loc from dept
  5. where dname like '%${_parameter}%'
  6. </select>


测试代码

 

 


  
  1. import java.util.List;
  2. import org.apache.ibatis.session.SqlSession;
  3. import org.mybatis.example.dao.Dept;
  4. import org.mybatis.example.dao.DeptMapper;
  5. import org.mybatis.example.dao.SqlSessionFactoryUtil;
  6. //按照条件进行查询;
  7. public class Test4 {
  8. public static void main(String[] args) {
  9. SqlSession session=SqlSessionFactoryUtil.getSqlSession();
  10. try {
  11. DeptMapper mapper=session.getMapper(DeptMapper.class);
  12. List<Dept>depts=mapper.selectByCriteria("网络部");
  13. for(Dept dept:depts){
  14. System.out.println("部门名称:"+dept.getDname());
  15. }
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }finally{
  19. session.close();
  20. }
  21. }
  22. }

 

 

 

 

 

 

 

文章来源: aaaedu.blog.csdn.net,作者:tea_year,版权归原作者所有,如需转载,请联系作者。

原文链接:aaaedu.blog.csdn.net/article/details/54910360

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。