mybatis环境搭建步骤

2017-08-14  本文已影响0人  Baltan

mybatis环境搭建步骤
1、导入Jar包:mybatis核心包
2、在src文件夹下新建config文件夹,在config文件夹中新建mybatis核心配置文件“mybatis.xml”
“mybatis.xml”文件模板:http://www.mybatis.org/mybatis-3/zh/getting-started.html:从 XML 中构建 SqlSessionFactory

<?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>
  <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="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml”/>//resource中填写“×××Mapper.xml”文件的文件名
  </mappers>
</configuration>

实例1:

<?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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="oracle.jdbc.driver.OracleDriver" />
                <property name="url" value="jdbc:oracle:thin:@192.168.1.188:1521:ORCL" />
                <property name="username" value="scott" />
                <property name="password" value="tiger" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- mybatis的配置文件 -->
        <mapper resource="dao/NewsMapper.xml" />
    </mappers>
</configuration>

实例2:

保留“mybatis.xml”文件默认内容,在config文件夹下新建oracle.properties文件
文件中写入:

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.1.188:1521:ORCL
username=scott
password=tiger

3、在src文件下新建dao包,新建“×××Mapper.java”接口文件
接口文件中为增删改查的抽象方法
在src文件下新建“×××Mapper.xml”文件,规范上和“×××Mapper.java”接口文件同名
“×××Mapper.xml”文件模板:http://www.mybatis.org/mybatis-3/zh/getting-started.html:探究已映射的 SQL 语句

<?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="org.mybatis.example.BlogMapper”>//namespace中填写”×××Mapper.java“文件的包名+类名

实例:

<?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="dao.NewsMapper">

    <resultMap id="news" type="model.News”>//返回对象映射:id值为映射名称(自定义),和select语句中的resultMap值相同,type值为返回对象类型的包名+类名
        <result column="newsid" property="newsID" />//column值为数据库字段名称,property值为返回对象类型的属性名称,将数据库中newsid字段的查询结果赋值给返回对象类型的newsID属性
        <result column="newstopic" property="topic" />
        <result column="newsauthor" property="author" />
        <result column="newscreatetime" property="createTime" />
        <result column="newsgrade" property="grade" />
        <result column="newscontent" property="content" />
        <result column="newspic" property="pic" />
    </resultMap>

    <insert id="addNews" parameterType="model.News”>
        insert into nmsnews
        values(#{newsID},#{author},#{createTime},#{topic},#{grade},#{content},#{pic})
    </insert>//id值为“×××.java”接口文件中的对应抽象方法名,parameterType值为传入参数对象类型的包名+类名
    <select id="queryAllOwnNews" parameterType="String" resultMap="news">
        select * from nmsnews where newsauthor=#{author}
    </select>
    <select id="queryNews" parameterType="model.News" resultMap="news">
        select * from nmsnews where newsauthor=#{author}
        <if test="newsID!= null and newsID!=''">
            and newsID like #{newsID}
        </if>
        <if test="createTime!=null and createTime!=''">
            and newscreateTime like #{createTime}
        </if>
        <if test="topic!= null and topic!=''">
            and newstopic like #{topic}
        </if>
        <if test="grade!= null and grade!=''">
            and newsgrade=#{grade}
        </if>
    </select>
    <update id="updateNews" parameterType="model.News">
        update nmsnews set
        newsauthor=#{author},newscreatetime=#{createTime},newstopic=#{topic},newsgrade=#{grade},newscontent=#{content},newspic=#{pic}
        where newsid=#{newsID}
    </update>
    <delete id="deleteNews" parameterType="String">
        delete from nmsnews
        where
        newsid=#{newsID}
    </delete>
</mapper>

4、在src文件下新建dao包util包,新建“sqlSessionUtil.java”文件
文件中写入:

package util;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionUtil {
    private static SqlSessionFactory factory;
    static {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis.xml");//参数值为mybatis核心配置文件“mybatis.xml”文件名
            factory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession() {
        return factory.openSession();
    }
}
上一篇下一篇

猜你喜欢

热点阅读