美文网首页
H_02mybatis入门

H_02mybatis入门

作者: ZzTNT | 来源:发表于2017-10-11 08:50 被阅读0次

mybatis的概念

概念:一个持久层框架

  • 作用:ORM将sql语句映射成实体类

  • 特点:巧灵活

  • 半自动化,面向sql

  • 使用与中小型项目的开发

1.创建mybatis-config.xml文件
配置文件(xml,全局设置、数据库连接信息等)

<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/bank?useUnicode=true&characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value=""/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="UserMapper.xml"/>//映射对应的文件路径名
  </mappers>
</configuration>

2.创建映射文件UserMapper.xml


<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

  <mapper namespace="user">
  //查
    <select id="selectUser" resultType="com.zyk.entity.User">
        select * from user where uid=1;
    </select>

    //改
    <update id="updateUserById" parameterType="com.zyk.entity.User">
        UPDATE user set username="张三" where uid=1;
    </update>
    //增
    <insert id="insertUser" parameterType="com.zyk.entity.User">
        insert into user (username,password)value("赵六","123");
    </insert>
    //删
    <delete id="deleteUserById" parameterType="com.zyk.entity.User">
        DELETE from user where uid = 10;
    </delete>

        //当类属性与数据库字段完全一致时可以不写
        //不一致时就在column属性中写上数据库中对应得字段名
    <resultMap id="userResultMap" type="com.zyk.entity.User">
        <result property="uid" column="uid"></result>
        <result property="username" column="username"></result>
        <result property="password" column="password"></result>
        <result property="utypeId" column="utypeId"></result>
        <result property="money" column="money"></result>

    </resultMap>
  </mapper>

其他一些属性:

       <!-- 连接初始值,连接池启动时创建的连接数量的初始值 -->
       <property name="initialSize" value="10" />
       <!-- 连接池的最大值,同一时间可以从池分配的最多连接数量,0时无限制 -->
       <property name="maxActive" value="100" />
       <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 ,0时无限制-->
       <property name="maxIdle" value="0" />
       <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
       <property name="minIdle" value="${minIdle}" />
       <!-- 是否对已备语句进行池管理(布尔值),是否对PreparedStatement进行缓存 -->
       <property name="poolPreparedStatements" value="true" />
       <!-- 是否对sql进行自动提交 -->
       <property name="defaultAutoCommit" value="true" />

实例:

public class MyTest {
    public static void main(String[] args) throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//获取主配置文件
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);//创建工厂对象
        //SqlSessionFactiory 作用域最好在应用作用域---一旦创建就应该在应用的运行期间一直存在
        //SqlSessionFactoryBuilder 作用域最好在方法作用域(局部方法变量)--一但创建了SqlSessionFactiory之后就再也用不倒了

        SqlSession sqlSession = factory.openSession();//获取SqlSession(相当于Connection)
        int updateUserById = sqlSession.update("updateUserById");//修改
        Object obj = sqlSession.selectOne("selectUserById");//查
        int insertUser = sqlSession.insert("insertUser");//增
        int del = sqlSession.delete("deleteUserById");//删
        sqlSession.commit();//提交 不提交数据库数据不会发生改变
        sqlSession.close();//使用完一次必须关闭,一次请求
        //sqlsession是有生命周期,可以理解对应一次数据库事务,基本可以理解为一次事务对应一个sqlsession。
    }
}

相关文章

网友评论

      本文标题:H_02mybatis入门

      本文链接:https://www.haomeiwen.com/subject/nepbyxtx.html