zoukankan      html  css  js  c++  java
  • Mybats 实现curd(增删改查)操作

    使用Mybats 实现curd操作!
    1:加入jar包
    2:配置文件的编写(包括两部分,一部分是总的配置文件a[]),另一部分是实体的配置文件!b[]))


    a1):编写一个propertity配置文件,内容如下:

      1. driver=com.mysql.jdbc.Driver  
      2. url=jdbc:mysql://localhost:3306/mybatis  
      3. username=root  
      4. password=admin 

    a2):总的配置myBatis-config.xml

      1. <?xml version="1.0" encoding="UTF-8" ?>   
      2. <!DOCTYPE configuration   
      3.     PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   
      4.     "http://mybatis.org/dtd/mybatis-3-config.dtd">  
      5. <configuration>  
      6.     <properties resource="com/tuojin/mybatis/res/config.properties" />  
      7.   
      8.     <environments default="tayfer">  
      9.         <environment id="tayfer">  
      10.             <transactionManager type="JDBC" />  
      11.             <dataSource type="POOLED">  
      12.                 <property name="driver" value="${driver}" />  
      13.                 <property name="url" value="${url}" />  
      14.                 <property name="username" value="${username}" />  
      15.                 <property name="password" value="${password}" />  
      16.             </dataSource>  
      17.         </environment>  
      18.     </environments>  
      19.   
      20.     <mappers>  
      21.         <mapper resource="com/tuojin/mybatis/entrys/Student.xml" />  
      22.     </mappers>  
      23.   
      24.   
      25. </configuration> 

    b1):映射类接口 com.tuojin.mybatis.mapper.StuMapper

    public interface  StuMapper{

      public void addStu(Student stu);

      public int deleteStu(int id);

      public Student  selectStu(int id);

      public Student findAllStu();

      public int updateStu(Student stu);

    }

    b2):映射类(实体)配置Map.xml

      1. <?xml version="1.0" encoding="UTF-8" ?>    
      2. <!DOCTYPE mapper   
      3.     PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
      4.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
      5. <mapper namespace="com.tuojin.mybatis.mapper.StuMapper">  
      6.   
      7.     <resultMap type="com.tuojin.mybatis.entrys.Student" id="stuRes">  
      8.         <id property="id" column="id" javaType="int" jdbcType="INTEGER" />  
      9.         <result property="name" column="name" javaType="String"  
      10.             jdbcType="VARCHAR" />  
      11.         <result property="version" column="version" javaType="String"  
      12.             jdbcType="VARCHAR" />  
      13.         <result property="maker" column="maker" javaType="String"  
      14.             jdbcType="VARCHAR" />  
      15.     </resultMap>  
      16.   
      17.     <insert id="addStu" useGeneratedKeys="true" keyProperty="id">  
      18.         insert  
      19.         into tb_student(name,version,maker)  
      20.         values(#{name},#{version},#{maker})  
      21.     </insert>  
      22.   
      23.     <delete id="deleteStu" parameterType="int">  
      24.         delete from tb_student  
      25.         where  
      26.         id=#{id}  
      27.     </delete>  
      28.   
      29.     <select id="selectStu" parameterType="int" resultMap="stuRes">  
      30.         select *  
      31.         from tb_student where id=#{id}  
      32.     </select>  
      33.   
      34.     <select id="findAllStu" resultMap="stuRes">  
      35.         select * from tb_student  
      36.     </select>  
      37.   
      38.     <update id="updateStu" parameterType="com.tuojin.mybatis.entrys.Student">  
      39.         update tb_student set  
      40.         name=#{name},maker=#{maker},version=#{version} where id=#{id}  
      41.     </update>  
      42.   
      43. </mapper>  

    2:使用一个工具类,获取类似hibernate session

      1. public class MybatisUtils  
      2. {  
      3.  private static SqlSessionFactory sqlSessionFactory = null;  
      4.  private static SqlSession session = null;  
      5.  public static SqlSessionFactory getSqlSessionFactory()  
      6.  {  
      7.   return sqlSessionFactory;  
      8.  }  
      9.  public static SqlSession getSession()  
      10.  {  
      11.   return sqlSessionFactory.openSession();  
      12.  }  
      13.  static  
      14.  {  
      15.   String URL = &quot;com/tuojin/mybatis/res/sqlMapClient.xml&quot;;  
      16.   try  
      17.   {  
      18.    Reader reader = org.apache.ibatis.io.Resources  
      19.      .getResourceAsReader(URL);  
      20.    sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);  
      21.   }  
      22.   catch (IOException e)  
      23.   {  
      24.    System.err.println(e.getMessage());  
      25.   }  
      26.  }  
      27.  public static void closeSession(SqlSession session)  
      28.  {  
      29.   session.close();  
      30.  } 

    4:对数据库进行操作!

      1. public class TestMyBatis  
      2. {  
      3.  @Test  
      4.  public void testConfig()  
      5.  {  
      6.   MybatisUtils.getSession();  
      7.  }  
      8.  @Test  
      9.  public void testAdd()  
      10.  {  
      11.   Student stu = new Student();  
      12.   stu.setMaker("80");  
      13.   stu.setName("tayfer");  
      14.   stu.setVersion("100_1");  
      15.   SqlSession session = MybatisUtils.getSession();  
      16.   session.insert("addStu", stu);  //IUserInfo mapper = session.getMapper(IUserInfo.class); mapper.insertUser(ui);
      17.   session.commit();  
      18.   MybatisUtils.closeSession(session);  
      19.  }  
      20.  @Test  
      21.  public void testDel()  
      22.  {  
      23.   SqlSession session = MybatisUtils.getSession();  
      24.   int flag = session.delete("deleteStu", 17);  
      25.   session.commit();  
      26.   MybatisUtils.closeSession(session);  
      27.   System.err.println(flag);  
      28.  }  
      29.  @Test  
      30.  public void testSelect()  
      31.  {  
      32.   SqlSession session = MybatisUtils.getSession();  
      33.   Student stu = (Student) session.selectOne("selectStu", 18);  
      34.   session.commit();  
      35.   MybatisUtils.closeSession(session);  
      36.   System.err.println(stu);  
      37.  }  
      38.  @Test  
      39.  public void testUpdate()  
      40.  {  
      41.   SqlSession session = MybatisUtils.getSession();  
      42.   Student stu = (Student) session.selectOne("selectStu", 18);  
      43.   stu.setName("yuyu");  
      44.   int flag = session.update("updateStu", stu);  
      45.   session.commit();  
      46.   MybatisUtils.closeSession(session);  
      47.   System.err.println(flag);  
      48.  } 

  • 相关阅读:
    Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the
    Caused by: java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected
    jpa单向一对一关系外键映射
    jpa关联映射
    svn 插件去除已经保存的密码方法
    【前端】less学习
    【CodeForces 520E】Pluses everywhere
    费马小定理证明
    【前端】纯前端的一个‘喜欢我吗?’
    Sublime text3 插件HTML/CSS/JS prettify 格式化代码
  • 原文地址:https://www.cnblogs.com/SunDexu/p/3099231.html
Copyright © 2011-2022 走看看