开发

1、编写实体Bean

package test.z.mybatis.beans;
import java.io.Serializable;
public class TestBean implements Serializable{
    private Integer id;
    private String username; 
    private String password;
    private Integer sex;
    public TestBean() {
      super();
     }

  public TestBean(String username,String password,int sex) {
      this.username=username;
      this.password=password;
      this.sex=sex;
     }

}

 

并在mybatis.cfg.xml中加入相关配置

   <typeAliases >
       <!-- 别名方式1,一个一个的配置 type中放置的是类的全路径,alias中放置的是类别名
       <typeAliase type="test.z.mybatis.beans.TestBean" alias="UserBean"/> -->
       <!-- 别名方式2,自动扫描,将JAVA类的类名作为类的类别名 -->
       <package name="test.z.mybatis.beans"/>
    </typeAliases>

 

2、编写Mapper

package test.z.mybatis.mapper;
import test.z.mybatis.beans.TestBean;
public interface TestMapper {
 public int insertUser(TestBean tb) throws Exception;
}

 

并在mybatis.cfg.xml中加入相关配置

         <mappers>
           <!-- 告知映射文件方式1,一个一个的配置-->
           <mapper resource="test/z/mybatis/mapper/TestMapper.xml"/>
           <!-- 告知映射文件方式2,自动扫描包内的Mapper接口与配置文件
           <package name="test/z/mybatis/mapper"/> -->
         </mappers>

 

3、编写Mapper配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test.z.mybatis.mapper.TestMapper">
<!-- 自定义返回结果集 -->
     <resultMap id="userMap" type="TestBean">
          <id property="id" column="id" javaType="java.lang.Integer"></id>
          <result property="username" column="username" javaType="java.lang.String"></result>
          <result property="password" column="password" javaType="java.lang.String"></result>
          <result property="sex" column="sex" javaType="java.lang.Integer"></result>
     </resultMap>
 <!-- 在各种标签中的id属性必须和接口中的方法名相同 , id属性值必须是唯一的,不能够重复使用。parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型-->   
 <!-- useGeneratedKeys:( 仅 对 insert 有 用 ) 这 会 告 诉 MyBatis 使 用 JDBC 的getGeneratedKeys
             方法来取出由数据(比如:像 MySQL 和 SQLServer 这样的数据库管理系统的自动递增字段)内部生成的主键。默认值: false。 -->   
 <!--keyProperty: (仅对 insert有用)标记一个属性, MyBatis 会通过 getGeneratedKeys或者通过 insert 语句的 selectKey 子元素设置它的值。默认:不设置。 -->
 <!--#{}中的内容,为占位符,当参数为某个JavaBean时,表示放置该Bean对象的属性值  -->
 
 
     <insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
         insert into sc_user (username,password,sex) values (#{username},#{password},#{sex})
     </insert>
    
     <update id="updateUser" >
       update sc_user set username=#{username},password=#{password},sex=#{account} where id=#{id}
     </update>
    
     <delete id="deleteUser" parameterType="int">
      delete from sc_user where id=#{id} 
     </delete>
    
     <select id="selectUserById" parameterType="int" resultMap="userMap">
      select * from sc_user where id=#{id}
     </select>
    
     <select id="selectAllUser" resultMap="userMap">
      select * from sc_user
     </select>

</mapper> 

 

4、编写Model ,model 必须继承于BModel

package test.z.model;
import org.jkas.core.BModel;
public class TestBM extends BModel{

}

 

5、编写控制器

package test.z.app;
import test.z.mybatis.mapper.TestMapper;
import test.z.mybatis.beans.TestBean;
import test.z.model.TestBM;
public class Mbts extends FrontBase
{ public void index()
   { TestBM tb=new TestBM();
      TestMapper tm=tb.getMapper(TestMapper.class);
      TestBean test = new TestBean("周胡顺", "20181024", 1);
    try
    {
      tm.insertUser(test);
      tb.commit();
    }
    catch (Exception e)
    { tb.rollback();
       echo(e.toString());
    }
    finally
     {tb.close();//必须调用关闭连接
     }
    echo("ok");
  }
}

 

使用结束 必须调用.close()关闭会话,否则会导致连接没有关闭问题!

执行结果 :

????????????_20220330232329.png