zoukankan      html  css  js  c++  java
  • MyBatis 搭建

                      MyBatis搭建

    引入jar包

      log4j-1.2.17.jar     log4j包
      mybatis-3.2.2.jar  MyBatis的核心包
      mysql-connector-java-5.1.7-bin.jar    数据库的链接包

    设置配置文件

      引入 log4j.properties

      配置文件名 MybatisConfig.xml

        <?xml version="1.0" encoding="UTF-8" ?>
        <!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
        <configuration>
        <typeAliases>
        <typeAlias type="com.bdqn.entity.NewDetail" alias="b"/> /创建别名  

        </typeAliases>


        <environments default="development">  //jdbc的连接池
        <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/kgcnews?useUnicode=true&amp;characterEncoding=UTF-8"/>//数据库链接路径
        <property name="username" value="root"/> //用户名
        <property name="password" value="root"/> //密码
        </dataSource>
        </environment>
        </environments>
        <mappers>
        <mapper resource="com/bdqn/dao/NewMapper.xml"/> //扫描dao层的映射文件
        </mappers>
        </configuration>

    dao层的映射文件

      NewMapper.xml
      NewMapper.java

      NewMapper.java内部配置

          public interface NewMapper {
          public List<NewDetail> list();
          public int update(NewDetail detail);
          public int del(@Param("a")String b);
          public int insert(Map<String,Object> obj);
          }

      NewMapper.xml内部配置     

          <?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="com.bdqn.dao.NewMapper"> //这个地址要和NewMapper的名字相同
           //查询<select id="count" resultType="int">
          select count(id) from news_detail
          </select>
           //查询<select id="list" resultType="b">
          select * from news_detail
          </select>
           //修改<update id="update" parameterType="b">
          update news_detail set title=#{title} where id = #{id}
          </update>
          //删除<delete id="del"> 
          delete from news_detail where id=#{a}
          </delete>

           //添加<insert id="insert" parameterType="Map">
          insert into news_detail(title,author) values(#{t},#{a})
          </insert>
          </mapper>

    数据库驱动增强

    配备 database.properties文件

        内容

          driver=com.mysql.jdbc.Driver
          url=jdbc:mysql://localhost:3306/kgcnews?useUnicode=true&amp;characterEncoding=UTF-8
          name=root
          password=root

      核心配置文件 MybatisConfig.xml

          <properties resource="database.properties"></properties>

          <environments default="development">
          <environment id="development">
          <transactionManager type="JDBC"/>
          <dataSource type="POOLED">
          <property name="driver" value="${driver}"/>
          <property name="url" value="${url}"/>
          <property name="username" value="${name}"/>
          <property name="password" value="${password}"/>
          </dataSource>
          </environment>
          </environments>

        

    窗竹影摇书案上,野泉声入砚池中。 少年辛苦终身事,莫向光阴惰寸功
  • 相关阅读:
    C# DataGridView 与 datatable 之间数据传递
    C# 调用命令行命令 net use
    C# 链接 sql server 数据库字符串
    winform窗口关闭,进程没有关掉的解决办法
    select 中的逻辑判断 sql server
    C#中的abstract 类和方法!!!
    c# ComboBox绑定枚举
    C#与C++类型互转
    DllImport
    TCP三次握手四次挥手详解
  • 原文地址:https://www.cnblogs.com/qhantime/p/10744682.html
Copyright © 2011-2022 走看看