码迷,mamicode.com
首页 > 其他好文 > 详细

MyBatis 搭建

时间:2019-04-21 12:57:16      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:parameter   cal   default   nsa   tle   config   names   用户名   evel   

                  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>

    

 

MyBatis 搭建

标签:parameter   cal   default   nsa   tle   config   names   用户名   evel   

原文地址:https://www.cnblogs.com/qhantime/p/10744682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!