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>

    

猜你喜欢

转载自www.cnblogs.com/qhantime/p/10744682.html